[Segment tree] Codeforces 339D Xenia and Bit Operations

Xenia and Bit Operations
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Xenia the beginner programmer has a sequence a, consisting of 2n non-negative integers: a1, 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 value v. At the first iteration, Xenia writes a new sequence aor a2, aor a4, ..., a2n - 1 or a2n, consisting of 2n - 1 elements. In other words, she writes down the bit-wise OR of adjacent elements of sequence 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.

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)  →  (1 or 2 = 3, 3 or 4 = 7)  →  (3 xor 7 = 4). The result is v = 4.

You are given Xenia's initial sequence. But to calculate value v for a given sequence would be too easy, so you are given additional m queries. Each query is a pair of integers p, b. Query p, b means that you need to perform the assignment ap = b. After each query, you need to print the new value v for the new sequence a.

Input

The first line contains two integers n and m (1 ≤ n ≤ 17, 1 ≤ m ≤ 105). The next line contains 2n integers a1, a2, ..., a2n (0 ≤ ai < 230). Each of the next m lines contains queries. The i-th line contains integers pi, bi (1 ≤ pi ≤ 2n, 0 ≤ bi < 230) — the i-th query.

Output

Print m integers — the i-th integer denotes value v for sequence a after the i-th query.

Examples
input
Copy
2 4
1 6 3 5
1 4
3 4
1 2
1 2
output
Copy
1
3
3
3
Note

For more information on the bit operations, you can follow this link: http://en.wikipedia.org/wiki/Bitwise_operation

Meaning of the questions:

Gives the number of n-th power of 2, every now and the combined sequence adjacent two the operand is a number to give a new sequence, the length of the new sequence is a sequence length - 1, when the new sequence length is 1 when the operation is stopped, the operation proceeds OR operation odd, even-numbered operation XOR operation,
there are m number of interrogation, interrogation will change each time a value in a sequence, the new sequence is asked operation How many

Ideas:

Each layer is a new sequence of the tree, the leaf nodes from the layer denoted as 0, updates up layers, each layer below it is the number of layers that layers + 1 layer
after recording the number of layers to know each to calculate what level, odd layers OR, the XOR even layers,
and finally a single point for each interrogation modification, the output value of the root node after it upwardly updated

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int amn=1<<17+5;
 4 typedef long long ll;
 5 int a[amn];
 6 struct tree{
 7     ll l,r,sum,deep;
 8     #define ls rt<<1
 9     #define rs rt<<1|1
10     #define trl tr[rt].l
11     #define trr tr[rt].r
12     #define trsum tr[rt].sum
13     #define trlssum TR [LS] .sum
 14      #define trrssum TR [RS] .sum
 15      #define TRDP TR [RT] .deep
 16  } TR [AMN];
 . 17  void push_up ( int RT) {
 18 is      TRDP = TR [LS ] .deep + . 1 ;      /// recording layers calculating leaf node is set to 0, the number of layers of the parent node is the node number of layers son + 1'd 
. 19      IF (& TRDP . 1 )               /// odd layers OR operation, the even number of layers XOR operation 
20 is          trsum = trlssum | trrssum;
 21 is      the else 
22 is          trsum ^ = trlssum trrssum;
 23 is  }
 24  void Build (int rt,int l,int r){
25     trl=l,trr=r;
26     if(trl==trr){
27         trsum=a[l];
28         trdp=0;             ///叶子节点计算层数
29         return ;
30     }
31     int mid=(l+r)>>1;
32     build(ls,l,mid);
33     build(rs,mid+1,r);
34     push_up(rt);
35 }
36 void updata(int rt,int l,int r,int pos,ll val){
37     int mid=(l+r)>>1;
38     if(trl==trr&&trl==pos){
39         trsum=val;
40         return;
41     }
42     if(pos<=mid)updata(ls,l,mid,pos,val);
43     else updata(rs,mid+1,r,pos,val);
44     push_up(rt);
45 }
46 int main(){
47     int n,m,p,b;
48     ios::sync_with_stdio(0 );
 49      CIN >> n >> m;
 50      int len = . 1 << n;            /// Note that the length of the n-th power of 2 
51 is      for ( int I = . 1 ; I <= len; I ++ )
 52 is          CIN >> A [I];
 53 is      Build ( . 1 , . 1 , len);
 54 is      the while (M-- ) {
 55          CIN >> >> P B;
 56 is          UPDATA ( . 1 , . 1 , len, P, B);
 57 is          the printf ( " % D \ n- " , TR [. 1 ] .sum);
 58      }
 59  }
 60  / * **
 61 is  given by the number of n-th power of 2, each of the now combined sequence adjacent to a number of two operands to obtain a new sequence, the length of the new sequence is a sequence length - 1, when a new sequence stops when the length of operation, operation of odd-OR operation, operation proceeds even-numbered XOR operation
 62  there are m number inquiry, each query will changing a value on a sequence of values, computing the new sequence asked how much
 63  the paper can be seen that this is a simulation complete binary tree structure, a single point of modification, update values up, it is conceivable to maintain a segment tree
 64  each layer is a new sequence of the tree, the leaf nodes from the layer denoted as 0, updates up layers, each layer below it is the number of layers that layers + 1 layer
 65  after recording each of the layers will know to calculate what level, odd layers OR, the XOR even layers,
 66  and finally changes to a single point of each interrogation, the output value of the root node is updated after it up
 67  ** * /

 

Guess you like

Origin www.cnblogs.com/Railgun000/p/11367587.html