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

3 3
1 2 4
7 6 5
3
1 2 1 2 2
1 2 1 3 4
2 3 2 3 4

6
5
1

## HINT

1<=u<=d<=n<=1000,

1<=l<=r<=m<=300000，

1<=k<=(d-u+1)*(r-l+1),

1<=p<=500

## Code

#include <cstdio>
int ch[10000005][2],tot,sz[10000005],rt[300005],n,m,v[300005],q,u,d,l,r,K,x,a[300005],b[300005],c[300005];
int ins(int x,int v)
{
int tp=++tot,u=tp,f;
for(int i=(1<<30);i;i>>=1) f=(v&i)>0,ch[u][f]=++tot,ch[u][f^1]=ch[x][f^1],u=ch[u][f],x=ch[x][f],sz[u]=sz[x]+1;
return tp;
}
int que(int len,int k)
{
int res=0,d,f,sum;
for(int i=(1<<30),j;i;i>>=1)
{
for(j=1,sum=0;j<=len;j++) d=!(a[j]&i),sum+=sz[ch[c[j]][d]]-sz[ch[b[j]][d]];
if(sum>=k) f=0,res|=i;else f=1,k-=sum;
for(j=1;j<=len;j++) d=(!(a[j]&i))^f,b[j]=ch[b[j]][d],c[j]=ch[c[j]][d];
}
return res;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&v[i]);
for(int i=1;i<=m;i++) scanf("%d",&x),rt[i]=ins(rt[i-1],x);
for(scanf("%d",&q);q--;printf("%d\n",que(d-u+1,K)))
{
scanf("%d%d%d%d%d",&u,&d,&l,&r,&K);
for(int j=u;j<=d;j++) a[j-u+1]=v[j],b[j-u+1]=rt[l-1],c[j-u+1]=rt[r];
}
}

posted @ 2018-08-06 14:57  CK6100LGEV2  阅读(150)  评论(0编辑  收藏  举报