题目
题意翻译
有2^n2
n
个数,有mm个操作,每次修改一个数,然后你要输出\ \ \ \ \ ( (a1|a2)xor(a3|a4) )|( (a5|a6)xor(a7|a8) )…
即\ or\ \ \ xor\ or xor 交替计算。
第一行两个数字n,mn,m。
第二行2^n2
n
个数字。
下面mm行,每行两个数字x,yx,y,将第xx个数字改为yy。
保证1\le n \le 17\ , \ 1\le m \le 10^51≤n≤17 , 1≤m≤10
5
,数字任意时刻满足0\le x \le 2^{30}0≤x≤2
30
共mm行,输出每次改完数字后上述表达式的值。
题目描述
Xenia the beginner programmer has a sequence a a , consisting of 2^{n} 2
n
non-negative integers: a_{1},a_{2},…,a_{2^{n}} a
1
,a
2
,…,a
2
n
. Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value v v for a a .
Namely, it takes several iterations to calculate value v v . At the first iteration, Xenia writes a new sequence $ a_{1} or a_{2},a_{3} or a_{4},…,a_{2^{n}-1} or a_{2^{n}} $ , consisting of 2^{n-1} 2
n−1
elements. In other words, she writes down the bit-wise OR of adjacent elements of sequence a a . At the second iteration, Xenia writes the bitwise exclusive OR of adjacent elements of the sequence obtained after the first iteration. At the third iteration Xenia writes the bitwise OR of the adjacent elements of the sequence obtained after the second iteration. And so on; the operations of bitwise exclusive OR and bitwise OR alternate. In the end, she obtains a sequence consisting of one element, and that element is v v .
Let’s consider an example. Suppose that sequence a=(1,2,3,4) a=(1,2,3,4) . Then let’s write down all the transformations (1,2,3,4) (1,2,3,4) → → $ (1 or 2=3,3 or 4=7) $ → → $ (3 xor 7=4) $ . The result is v=4 v=4 .
You are given Xenia’s initial sequence. But to calculate value v v for a given sequence would be too easy, so you are given additional m m queries. Each query is a pair of integers p,b p,b . Query p,b p,b means that you need to perform the assignment a_{p}=b a
p
=b . After each query, you need to print the new value v v for the new sequence a a .
输入输出格式
输入格式:
The first line contains two integers n n and m m (1<=n<=17,1<=m<=10^{5}) (1<=n<=17,1<=m<=10
5
) . The next line contains 2^{n} 2
n
integers a_{1},a_{2},…,a_{2^{n}} a
1
,a
2
,…,a
2
n
$ (0<=a_{i}<2^{30}) $ . Each of the next m m lines contains queries. The i i -th line contains integers p_{i},b_{i} p
i
,b
i
$ (1<=p_{i}<=2{n},0<=b_{i}<2{30}) $ — the i i -th query.
输出格式:
Print m m integers — the i i -th integer denotes value v v for sequence a a after the i i -th query.
输入输出样例
输入样例#1: 复制
2 4
1 6 3 5
1 4
3 4
1 2
1 2
输出样例#1: 复制
1
3
3
3
说明
For more information on the bit operations, you can follow this link: http://en.wikipedia.org/wiki/Bitwise_operation
思路
我们发现线段树可以完美解决这个问题。
因为保证有2n个数字。。。
代码
#include<cstdio>
const int N=1050000;
int n,m,x,y,q;
int tre[N];
void add(int now,int ls,int rs,int mb,int val,int t)
{
if(ls>mb||rs<mb)return;
if(ls==rs)
{
tre[now]=val;
return;
}
int noww=now<<1,nrs=ls+rs>>1;
add(noww,ls,nrs,mb,val,t-1);
add(noww|1,nrs+1,rs,mb,val,t-1);
if(t&1)tre[now]=tre[noww]|tre[noww|1];
else tre[now]=tre[noww]^tre[noww|1];
}
int main()
{
scanf("%d%d",&n,&m);
q=n;n=1<<n;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
add(1,1,n,i,x,q);
}
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
add(1,1,n,x,y,q);
printf("%d\n",tre[1]);
}
}