码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 339C Xenia and Bit Operations(线段树水题)

时间:2015-03-30 06:44:48      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

转载请注明出处: http://www.cnblogs.com/fraud/           ——by fraud

 

Xenia and Bit Operations

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 mqueries. 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.

Sample test(s)
input
2 4
1 6 3 5
1 4
3 4
1 2
1 2
output
1
3
3
3
Note

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

 

线段树的入门题难度,往上维护的时候或与异或轮流搞。

技术分享
 1 //#####################
 2 //Author:fraud
 3 //Blog: http://www.cnblogs.com/fraud/
 4 //#####################
 5 #include <iostream>
 6 #include <sstream>
 7 #include <ios>
 8 #include <iomanip>
 9 #include <functional>
10 #include <algorithm>
11 #include <vector>
12 #include <string>
13 #include <list>
14 #include <queue>
15 #include <deque>
16 #include <stack>
17 #include <set>
18 #include <map>
19 #include <cstdio>
20 #include <cstdlib>
21 #include <cmath>
22 #include <cstring>
23 #include <climits>
24 #include <cctype>
25 using namespace std;
26 #define XINF INT_MAX
27 #define INF 0x3FFFFFFF
28 #define MP(X,Y) make_pair(X,Y)
29 #define PB(X) push_back(X)
30 #define REP(X,N) for(int X=0;X<N;X++)
31 #define REP2(X,L,R) for(int X=L;X<=R;X++)
32 #define DEP(X,R,L) for(int X=R;X>=L;X--)
33 #define CLR(A,X) memset(A,X,sizeof(A))
34 #define IT iterator
35 typedef long long ll;
36 typedef pair<int,int> PII;
37 typedef vector<PII> VII;
38 typedef vector<int> VI;
39 #define MAXN 200100
40 int A[MAXN*4];
41 
42 
43 int main()
44 {
45     ios::sync_with_stdio(false);
46     int n,m;
47     cin>>n>>m;
48     int t=1<<n;
49     for(int i=0;i<t;i++)cin>>A[i+t];
50     for(int i=n;i>0;i--){
51         int l=1<<(i-1),r=1<<(i);
52         if((n-i)&1){
53             while(l<r){
54                 A[l]=A[l<<1]^A[l<<1|1];
55                 l++;
56             }
57         }else{
58             while(l<r){
59                 A[l]=A[l<<1]|A[l<<1|1];
60                 l++;
61             }
62         }
63     }
64     int u,v,x;
65     for(int i=0;i<m;i++){
66         cin>>u>>v;
67         u+=(1<<n)-1;
68         x=1;
69         A[u]=v;
70         u>>=1;
71         while(u){
72             if(x){
73                 A[u]=A[u<<1]|A[u<<1|1];
74             }else{
75                 A[u]=A[u<<1]^A[u<<1|1];
76             }
77             u>>=1;
78             x^=1;
79         }
80         cout<<A[1]<<endl;
81     }
82     return 0;
83 }
代码君

 

codeforces 339C Xenia and Bit Operations(线段树水题)

标签:

原文地址:http://www.cnblogs.com/fraud/p/4376999.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!