题目大意:给定一个数组,然后交叉进行下面的操作
- In other words, she writes down the bit-wise OR of adjacent elements of sequence a.(相邻元素同或)
- Xenia writes the bitwise exclusive OR of adjacent elements of the sequence obtained after the first iteration(相邻元素异或)
当然了,还有一个操作:
Query p, b means that you need to perform the assignment ap = b.
对于Query p,b我们知道这题可以用线段树解决。因为修改了ap ,那么后面的值也要相应的做出改变...
下面是代码:
#include <stdio.h>
#define maxn 200200
int sum[maxn<<2];
int A[maxn];
//如果op为1,则为 OR
//如果op为0,则为 ~OR
void build(int l,int r,int rt,int op)
{
if(l == r)
{
sum[rt] = A[l];
return ;
}
int mid = (l + r) >> 1;
build(l,mid,rt<<1,1-op);
build(mid+1,r,rt<<1|1,1-op);
if(op) sum[rt] = sum[rt<<1] | sum[rt<<1|1];
else sum[rt] = sum[rt<<1] ^ sum[rt<<1|1];
}
void update(int p,int l,int r,int rt,int v,int op)
{
if(l == r)
{
sum[rt] = v;
return ;
}
int mid = (l + r) >> 1;
if(p > mid) update(p,mid+1,r,rt<<1|1,v,1-op);
else if(p <= mid) update(p,l,mid,rt<<1,v,1-op);
if(op) sum[rt] = sum[rt<<1] | sum[rt<<1|1];
else sum[rt] = sum[rt<<1] ^ sum[rt<<1|1];
}
int main()
{
int n,m,cnt;
scanf("%d%d",&n,&m);
for(int i = 1; i <= (1<<n); i++) scanf("%d",&A[i]);
cnt = 1<<n;
//printf("cnt = %d\n",cnt);
build(1,cnt,1,n%2);
//printf("%d\n",sum[1]);
for(int i = 1; i <= m; i++)
{
int p,b;
scanf("%d%d",&p,&b);
update(p,1,cnt,1,b,n%2);
printf("%d\n",sum[1]);
}
return 0;
}