BZOJ4103 [Thu Summer Camp 2015]异或运算 【可持久化trie树】

BZOJ4103

题解

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 300005,B = 30,maxm = 10000005,INF = 1000000000;
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int bin[50],n,m;
int a[1005],b[maxn],rt[maxn],cnt;
int ch[maxm][2],sum[maxm];
int ins(int x,int v){
int u,tmp;
u = tmp = ++cnt;
for (int i = B; ~i; i--){
ch[u][0] = ch[v][0];
ch[u][1] = ch[v][1];
sum[u] = sum[v] + 1;
int t = bin[i] & x; t >>= i;
v = ch[v][t];
u = ch[u][t] = ++cnt;
}
sum[u] = sum[v] + 1;
return tmp;
}
int atu[1005],atv[1005];
int query(int u,int v,int l,int r,int k){
int ans = 0;
for (int i = l; i <= r; i++) atu[i] = u,atv[i] = v;
for (int i = B; ~i; i--){
int cnt = 0;
for (int j = l; j <= r; j++){
int x = atu[j],y = atv[j],t = bin[i] & a[j]; t >>= i;
cnt += sum[ch[x][t ^ 1]] - sum[ch[y][t ^ 1]];
}
if (cnt >= k){
ans += bin[i];
for (int j = l; j <= r; j++){
int x = atu[j],y = atv[j],t = bin[i] & a[j]; t >>= i;
atu[j] = ch[x][t ^ 1];
atv[j] = ch[y][t ^ 1];
}
}
else {
k -= cnt;
for (int j = l; j <= r; j++){
int x = atu[j],y = atv[j],t = bin[i] & a[j]; t >>= i;
atu[j] = ch[x][t];
atv[j] = ch[y][t];
}
}
}
return ans;
}
int main(){
bin[0] = 1; for (int i = 1; i <= 30; i++) bin[i] = bin[i - 1] << 1;
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = 1; i <= m; i++){