zoukankan      html  css  js  c++  java
  • Xenia and Bit Operations CodeForces

    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 a1 or a2, a3 or 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.

    Example

    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 #include<bits/stdc++.h>
     2 #define lson l , m , rt << 1
     3 #define rson m+1,r , rt << 1 | 1
     4 using namespace std;
     5 
     6 const int maxn=2e5;
     7 
     8 int n,m,p,b;
     9 int cal[20],x[4*maxn],mark[4*maxn];
    10 
    11 int get(int k){
    12     int tem=1;
    13     for(int i=1;i<=k;i++) tem*=2;
    14     return tem;
    15 }
    16 
    17 void Inite(){
    18     for(int i=1;i<=20;i++) cal[i]=get(i);
    19 }
    20 
    21 void Bookup(int rt){
    22     int sonl=rt<<1;
    23     int sonr=rt<<1|1;
    24     if(mark[sonl] && mark[sonr]) mark[rt]=0;
    25     if(!mark[sonl]&&!mark[sonr]) mark[rt]=1;
    26 }
    27 
    28 void Pushup(int rt){
    29     int sonl=rt<<1;
    30     int sonr=rt<<1|1;
    31     //cout<<"ROOT: "<<rt<<endl;
    32     //cout<<mark[rt]<<" "<<x[sonl]<<" "<<x[sonr]<<endl;
    33     if(mark[rt]==1) x[rt]=x[sonl]|x[sonr];
    34     if(mark[rt]==0) x[rt]=x[sonl]^x[sonr];
    35 }
    36 
    37 void Build(int l,int r,int rt){
    38     if(l==r){
    39         scanf("%d",&x[rt]);
    40         return;
    41     }
    42     int m=(l+r)>>1;
    43     Build(lson);
    44     Build(rson);
    45     Bookup(rt);
    46     Pushup(rt);
    47 }
    48 
    49 void Update(int l,int r,int rt){
    50     if(l==r){
    51         x[rt]=b;
    52         return;
    53     }
    54     int m=(l+r)>>1;
    55     if(p<=m) Update(lson);
    56     else Update(rson);
    57     Pushup(rt);
    58 }
    59 
    60 int main()
    61 {   Inite();
    62     scanf("%d%d",&n,&m);
    63     Build(1,cal[n],1);
    64     while(m--){
    65         scanf("%d%d",&p,&b);
    66         Update(1,cal[n],1);
    67         printf("%d
    ",x[1]);
    68     }
    69     return 0;
    70 }
  • 相关阅读:
    eclipse- DDMS截图功能使用
    宏-新项目物理按键不能用
    宏-宏的添加跟代码中的使用
    SQlite-数据库的访问实例(转)
    git 工具的使用总结(6)-提交合并处理
    git 工具的使用总结(5)-查看历史记录
    git -处理分支合并
    Linux查询网址
    SQLite常用网址
    Java查询网址
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7862509.html
Copyright © 2011-2022 走看看