Xenia and Bit OperationsTime
limit:2000MS
Memory Limit:262144KB
64bit IO Format:%i64d &%i6 4u SubmitStatusPracticecodeforces 339D
Description
Xenia The beginner programmer has a sequence a, consisting of 2n non-negative integers: a 1, a2, ..., a2n. Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value v for a.
Namely, it takes several iterations to calculate valuev. At the first iteration, Xenia writes a new sequencea1ora2,a3ora4, ..., a 2 n -1 or a 2 n , consisting of < Span class= "Tex-span" >2 n -1 elements. In other words, she writes down the bit-wise OR of adjacent elements of Sequence a . A t 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 ITER ation. 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 . /span>
Let's consider an example. Suppose that Sequence a = (1, 2, 3, 4). Then let's write down all the Transformations (1, 2, 3, 4) → or 2 = 3, 3 or 4 = 7) → (3 xor 7 = 4). The result is v = 4.
You are given Xenia ' s initial sequence. Calculate value v for a given sequence would is too easy, so you are given additional mqu Eries. Each query is a pair of integers p, b. Query P, b means, need to perform the assignment ap = b . After all query, you need to print the new value v for the new sequence a.
Input
The first line contains integersNandm(1≤N≤17, 1≤m≤105). The next line contains2 n integers a 1, a 2, ..., a 2 n (0≤ a I < 230). Each of the Next m lines contains queries. The i -th line contains Integers p i , b i (1≤ p i ≤2 n , 0≤ b I < 230) -the I -th query.
Output
Print m integers-the i-th integer denotes value v for sequence a aft Er the i-th query.
Sample Input
Input
2 4
1 6 3 5
1 4
3 4
1 2
1 2
Output
1
3
3
3
Hint
For more information on the bit operations, you can follow this link: http://en.wikipedia.org/wiki/bitwise_operation< /c10>
Very basic line-segment tree
#include <iostream>#include<stdio.h>using namespacestd;Const intMaxx =1<< -;intNum[maxx];intans[maxx<<2];voidBuildintLintRintRtintop) { if(l==R) {Ans[rt]=Num[l]; return; } intMid= (l+r) >>1; Build (L,mid,rt<<1,1-op); Build (Mid+1,r,rt<<1|1,1-op); if(op==1) ans[rt]=ans[rt<<1]|ans[rt<<1|1]; Elseans[rt]=ans[rt<<1]^ans[rt<<1|1];}voidUpdateintLintRintRtintIndexintValueintop) { if(l==r&&l==index) {Ans[rt]=value; return; } intMid= (l+r) >>1; if(mid<index) {Update (Mid+1, R, (rt<<1|1), Index,value,1-op); } Else{update (L,mid,rt<<1, Index,value,1-op); } if(op==1) ans[rt]=ans[rt<<1]|ans[rt<<1|1]; Elseans[rt]=ans[rt<<1]^ans[rt<<1|1];}intMain () {//cout<<maxx+1<<endl; intn,m; scanf ("%d%d",&n,&m); intsum=1<<N; for(intI=1; i<=sum;i++) scanf ("%d",&Num[i]); Build (1, Sum,1, n%2); for(intI=0; i<m;i++) { intb; scanf ("%d%d",&a,&b); Update (1, Sum,1, a,b,n%2); printf ("%d\n", ans[1]); } return 0;}
View Code
cf339d Xenia and Bit Operations