CF 197 DIV2 Xenia and Bit Operations 线段树

线段树!!1A

代码如下:

 1 #include<iostream>
 2 #include<cstdio>
 3 #define lson i<<1
 4 #define rson i<<1|1
 5 #define MAX 140000
 6 #define ll __int64
 7 using namespace std;
 8 ll a[MAX],s;
 9 struct tree
10 {
11     int l,r;
12     ll res;
13     bool flag;
14 }T[3*MAX];
15 //奇数OR,偶数XOR
16 void built(int i,int l,int r,bool f)
17 {
18     T[i].l=l;
19     T[i].r=r;
20     T[i].flag=f;
21     if(l==r){
22         T[i].res=a[l];
23         return ;
24     }
25     int m=(l+r)>>1;
26     built(lson,l,m,!f);
27     built(rson,m+1,r,!f);
28     if(T[i].flag) T[i].res=T[lson].res|T[rson].res;
29     else T[i].res=T[lson].res^T[rson].res;
30 }
31 void update(int i,int d,ll p)
32 {
33     if(T[i].l==d&&T[i].r==d){
34         T[i].res=p;
35         return ;
36     }
37     int m=(T[i].l+T[i].r)>>1;
38     if(d<=m) update(lson,d,p);
39     else if(d>m) update(rson,d,p);
40     if(T[i].flag) T[i].res=T[lson].res|T[rson].res;
41     else T[i].res=T[lson].res^T[rson].res;
42 }
43 int main()
44 {
45     int n,m,i,k;
46     while(scanf("%d%d",&n,&m)!=EOF){
47         for(i=1;i<=(1<<n);i++) scanf("%I64d",&a[i]);
48         built(1,1,(1<<n),n&1);
49         for(i=0;i<m;i++){
50             scanf("%d %I64d",&k,&s);
51             update(1,k,s);
52             printf("%I64d
",T[1].res);
53         }
54     }
55     return 0;
56 }
View Code
原文地址:https://www.cnblogs.com/xin-hua/p/3294232.html