# bzoj 4103: 异或运算 可持久化Trie

## 题解:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const int maxn = 1024;
const int maxm = 300010;
struct Node{
Node *ch[2];
int siz;
}*null,*root[maxm];
Node mem[maxm*40],*it;
inline void init(){
it = mem;null = it++;
null->ch[0] = null->ch[1] = null;
null->siz = 0;root[0] = null;
}
inline Node* insert(Node *rt,int x,int d){
Node *p = it++;*p = *rt;p->siz ++ ;
if(d == -1) return p;
int id = (x >> d)&1;
p->ch[id] = insert(p->ch[id],x,d-1);
return p;
}
int cnt;
Node *lnw[maxn],*rnw[maxn];int val[maxn];
int kth(int k,int d){
if(d == -1) return 0;
int tmp = 0;
for(int i=1,id;i<=cnt;++i){
id = (val[i] >> d) & 1;
tmp += rnw[i]->ch[id^1]->siz - lnw[i]->ch[id^1]->siz;
}
if(k <= tmp){
for(int i=1,id;i<=cnt;++i){
id = (val[i] >> d) & 1;
lnw[i] = lnw[i]->ch[id^1];
rnw[i] = rnw[i]->ch[id^1];
}return (1<<d) | kth(k,d-1);
}else{
for(int i=1,id;i<=cnt;++i){
id = (val[i] >> d) & 1;
lnw[i] = lnw[i]->ch[id];
rnw[i] = rnw[i]->ch[id];
}return kth(k - tmp,d-1);
}
}
int a[maxn],b[maxm];
int main(){
init();
int n,m;read(n);read(m);
for(int i=1;i<=n;++i) read(a[i]);
for(int i=1;i<=m;++i){
read(b[i]);
root[i] = insert(root[i-1],b[i],30);
}
int q;read(q);
while(q--){
int u,d,l,r,k;read(u);read(d);
read(l);read(r);read(k);
cnt = 0;
for(int i=u;i<=d;++i){
lnw[++cnt] = root[l-1];
rnw[cnt] = root[r];
val[cnt] = a[i];
}
printf("%d\n",kth(k,30));
}
getchar();getchar();
return 0;
}


posted @ 2017-02-28 21:21  Sky_miner  阅读(206)  评论(0编辑  收藏  举报