# 【BZOJ4103】[Thu Summer Camp 2015]异或运算

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

对于100%的数据，0<=Xi,Yj<2^31,

1<=u<=d<=n<=1000,
1<=l<=r<=m<=300000,
1<=k<=(d-u+1)*(r-l+1),
1<=p<=500

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n,m,tot,Q;
struct node
{
int ch[2],siz;
}s[10000010];
int v[1010],p[1010],p1[1010],p2[1010],rt[300010];
inline int rd()
{
int ret=0,f=1;	char gc=getchar();
while(gc<'0'||gc>'9')	{if(gc=='-')	f=-f;	gc=getchar();}
while(gc>='0'&&gc<='9')	ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
int insert(int x,int val)
{
int u,tmp=++tot,d,i;
for(u=tmp,i=1<<30;i;i>>=1)
{
d=(val&i)>0,s[u].ch[d]=++tot,s[u].ch[d^1]=s[x].ch[d^1];
u=s[u].ch[d],x=s[x].ch[d],s[u].siz=s[x].siz+1;
}
return tmp;
}
int query(int len,int k)
{
int i,j,ret=0,d,dd,sum;
for(i=1<<30;i;i>>=1)
{
for(sum=0,j=1;j<=len;j++)
{
d=!(p[j]&i);
sum+=s[s[p2[j]].ch[d]].siz-s[s[p1[j]].ch[d]].siz;
}
if(sum>=k)	dd=0,ret|=i;
else	dd=1,k-=sum;
for(j=1;j<=len;j++)
{
d=(!(p[j]&i))^dd;
p2[j]=s[p2[j]].ch[d],p1[j]=s[p1[j]].ch[d];
}
}
return ret;
}
int main()
{
n=rd(),m=rd();
int i,j,a,b,c,d;
for(i=1;i<=n;i++)	v[i]=rd();
for(i=1;i<=m;i++)	rt[i]=insert(rt[i-1],rd());
Q=rd();
for(i=1;i<=Q;i++)
{
a=rd(),b=rd(),c=rd(),d=rd();
for(j=a;j<=b;j++)	p[j-a+1]=v[j],p1[j-a+1]=rt[c-1],p2[j-a+1]=rt[d];
printf("%d\n",query(b-a+1,rd()));
}
return 0;
}//3 3 1 2 4 7 6 5 3 1 2 1 2 2 1 2 1 3 4 2 3 2 3 4

| 欢迎来原网站坐坐！ >原文链接<

posted @ 2017-09-10 09:39  CQzhangyu  阅读(228)  评论(0编辑  收藏  举报