P3293 [SCOI2016]美味 主席树+最大异或路径
Posted kaka0010
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P3293 [SCOI2016]美味 主席树+最大异或路径相关的知识,希望对你有一定的参考价值。
原题链接:https://www.luogu.com.cn/problem/P3293
题意
一家餐厅有n个菜,每个菜都有评价值ai。有m位顾客,他们对菜的期望值为bi,偏好值为xi。因此第i位客人认为第j样菜品的美味程度为 b i x o r ( a i + x i ) b_i xor(a_i+x_i) bixor(ai+xi),而且他们有一个选择的范围是[l,r],问他们选择的美味值最高的菜是多少。
分析
看到异或运算,我们很容易想到01字典树的最大异或路径,特就是假设有两个数a,b。当a的第i位是1时,我们去寻找第i位为0的b,反之亦然。因此这题也可以这样去想,我们让 a n s = a i + x ans=a_i+x ans=ai+x,当bi的第j位为1时,ans的范围是 [ a n s , a n s + ( 1 < < j ) − 1 ] [ans, ans + (1<<j)-1] [ans,ans+(1<<j)−1],当bi的第j位是0时,ans的范围是 [ a n s + ( 1 < < j ) , a n s + ( 1 < < ( j + 1 ) ) − 1 ] [ans+(1<<j), ans + (1<<(j+1))-1] [ans+(1<<j),ans+(1<<(j+1))−1],然后去主席树上找是否存在ans-x的值就可以了。
code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const int M = 1e5 + 10;
const int MOD = 998244353;
struct node {
int ls, rs;
int sum;
}hjt[N * 50];
int tot, a[N], rt[N];
void modify(int &now, int pre, int l, int r, int pos) {
now = ++tot;
hjt[now] = hjt[pre];
hjt[now].sum++;
if (l == r) return;
int mid = (l + r) >> 1;
if (pos <= mid) modify(hjt[now].ls, hjt[pre].ls, l, mid, pos);
else modify(hjt[now].rs, hjt[pre].rs, mid+1, r, pos);
}
int query(int now, int pre, int l, int r, int ql, int qr) {
if (ql <= l && qr >= r) return hjt[now].sum - hjt[pre].sum;
int ans = 0;
int mid = (l + r) >> 1;
if (ql <= mid) ans += query(hjt[now].ls, hjt[pre].ls, l, mid, ql, qr);
if (qr > mid) ans += query(hjt[now].rs, hjt[pre].rs, mid+1, r, ql, qr);
return ans;
}
void solve () {
int n, m; cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) modify(rt[i], rt[i-1], 0, 2e5, a[i]);
for (int i = 1; i <= m; i++) {
int b, x, l, r, ans = 0;
cin >> b >> x >> l >> r;
for (int j = 20; ~j; j--) {
if (b >> j & 1) {
int L = ans;
int R = ans + (1 << j) - 1;
if (!query(rt[r], rt[l-1], 0, 2e5, max(0,L-x), min(R-x,(int)2e5))) {
ans += (1 << j);
}
} else {
int L = ans + (1 << j);
int R = ans + (1 << (j + 1)) - 1;
if (query(rt[r], rt[l-1], 0, 2e5, max(0,L-x), min((int)2e5,R-x))) {
ans += (1 << j);
}
}
}
cout << (ans ^ b) << endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifdef ACM_LOCAL
freopen("input", "r", stdin);
freopen("output", "w", stdout);
signed test_index_for_debug = 1;
char acm_local_for_debug = 0;
do {
if (acm_local_for_debug == '$') exit(0);
if (test_index_for_debug > 20)
throw runtime_error("Check the stdin!!!");
auto start_clock_for_debug = clock();
solve();
auto end_clock_for_debug = clock();
cout << "Test " << test_index_for_debug << " successful" << endl;
cerr << "Test " << test_index_for_debug++ << " Run Time: "
<< double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
cout << "--------------------------------------------------" << endl;
} while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
solve();
#endif
return 0;
}
以上是关于P3293 [SCOI2016]美味 主席树+最大异或路径的主要内容,如果未能解决你的问题,请参考以下文章