http://acm.hdu.edu.cn/showproblem.php?pid=6186
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2821 Accepted Submission(s): 1105
Problem Description
Little A has come to college and majored in Computer and Science.Today he has learned bit-operations in Algorithm Lessons, and he got a problem as homework.Here is the problem:You are giving n non-negative integers a1,a2,⋯,an, and some queries.
A query only contains a positive integer p, which means you are asked to answer the result of bit-operations (and, or, xor) of all the integers except ap.
Input
There are no more than 15 test cases. Each test case begins with two positive integers n and p in a line, indicate the number of positive integers and the number of queries.2≤n,q≤10^5.Then n non-negative integers a1,a2,⋯,an follows in a line, 0≤ai≤109 for each i in range[1,n].After that there are q positive integers p1,p2,⋯,pqin q lines, 1≤pi≤n for each i in range[1,q].
Output
For each query p, output three non-negative integers indicates the result of bit-operations(and, or, xor) of all non-negative integers except ap in a line.
Sample Input
3 3
1 1 1
1
2
3
Sample Output
1 1 0
1 1 0
1 1 0
Source
2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)
题意
给定n和q表示对n个数进行q次查询,每次查询输入一个整数p,输出n个数除了第p个数外其他数相互&、|、^的结果。2≤n,q≤10^5
思路
1、线段树
2、对于&、|可以用前缀和后缀数组来解决,对于^,由于x^p^p=x因此我们可以让所有的数相异或,然后再让第p个数和其异或,这样就得到了除第p个数外其他数相异或的值。
C++代码
1、线段树求解
#include
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
using namespace std;
const int N=100009;
int a[N];
int _and[N<<2],_or[N<<2],_xor[N<<2];
void pushup(int rt,int sum[],int op)
{
if(op==1)
sum[rt]=sum[rt<<1]&sum[rt<<1|1];
else if(op==2)
sum[rt]=sum[rt<<1]|sum[rt<<1|1];
else if(op==3)
sum[rt]=sum[rt<<1]^sum[rt<<1|1];
}
void build(int l,int r,int rt,int sum[],int op)
{
if(l==r)
{
sum[rt]=a[l];
return;
}
int m=(l+r)>>1;
build(ls,sum,op);
build(rs,sum,op);
pushup(rt,sum,op);
}
int query(int L,int R,int l,int r,int rt,int op)
{
if(L<=l&&r<=R)
{
if(op==1)
return _and[rt];
else if(op==2)
return _or[rt];
else if(op==3)
return _xor[rt];
}
int ans;
int m=(l+r)>>1;
bool flag=false;//记录ans是否有值
if(L<=m)
{
ans=query(L,R,ls,op);
flag=true;
}
if(R>m)
{
//如果ans还没有值,那么直接返回查询结果
if(!flag) return query(L,R,rs,op);
//如果ans已经有值了,就让ans和右子树的查询结果进行相应运算
if(op==1)
ans=ans&query(L,R,rs,op);
else if(op==2)
ans=ans|query(L,R,rs,op);
else if(op==3)
ans=ans^query(L,R,rs,op);
}
return ans;
}
int main()
{
int n,q;
while(~scanf("%d%d",&n,&q))
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
build(1,n,1,_and,1);
build(1,n,1,_or,2);
build(1,n,1,_xor,3);
while(q--)
{
int p;
scanf("%d",&p);
if(p==1)
printf("%d %d %d\n",query(2,n,1,n,1,1),query(2,n,1,n,1,2),query(2,n,1,n,1,3));
else if(p==n)
printf("%d %d %d\n",query(1,n-1,1,n,1,1),query(1,n-1,1,n,1,2),query(1,n-1,1,n,1,3));
else
printf("%d %d %d\n",query(1,p-1,1,n,1,1)&query(p+1,n,1,n,1,1),query(1,p-1,1,n,1,2)|query(p+1,n,1,n,1,2),query(1,p-1,1,n,1,3)^query(p+1,n,1,n,1,3));
}
}
return 0;
}
2、前缀、后缀数组求解
#include
using namespace std;
const int N=100005;
int x[N];//n个数
int a[N],b[N];//and的前缀、后缀数组
int c[N],d[N];//or的前缀、后缀数组
int total;//n个数异或的值
int main()
{
int n,q,p;
while(~scanf("%d%d",&n,&q))
{
total=0,a[0]=~0,b[n+1]=~0,c[0]=0,d[n+1]=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&x[i]);
total^=x[i];
}
//& |的前缀数组
for(int i=1;i<=n;i++) a[i]=a[i-1]&x[i],c[i]=c[i-1]|x[i];
//& |的后缀数组
for(int i=n;i>=1;i--) b[i]=b[i+1]&x[i],d[i]=d[i+1]|x[i];
while(q--)
{
scanf("%d",&p);
printf("%d %d %d\n",a[p-1]&b[p+1],c[p-1]|d[p+1],total^x[p]);
}
}
return 0;
}