E - Xenia and Bit Operations CodeForces - 339D 线段树

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

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

给出2的n次方个数,叶子节点两两进行位运算,倒数最后一行进行或运算,倒数第二行进行异或运算,倒数第三行进行或运算,以此类推,最后根节点的数据就是答案。我们用0表示进行或运算,用1表示进行异或运算。一旦到达叶子节点,就返回0,表示进行或操作,否则进行递归调用,将从子树传回来的位操作信息应用到两棵子树的数据,得到该节点的数据,然后将位操作信息异或1返回给当前节点的父节点,因为位操作是交替进行的。注意:用0表示或操作,1表示异或操作,所以从0开始每次异或1就能够进行交替的操作了,一个小技巧而已。

参考博客:https://blog.csdn.net/silenceneo/article/details/52068244

AC代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn=1e5;
int s[maxn<<2];
void pushup(int rt,int ok)
{
    if(!ok)
    {
        s[rt]=s[rt<<1]|s[rt<<1|1];//或运算
    }
    else
    {
        s[rt]=s[rt<<1]^s[rt<<1|1];//异或运算
    }
}
int build(int l,int r,int rt)
{
    if(l==r)
    {
        int n;
        cin>>n;
        s[rt]=n;
        return 0;//叶子节点返回0
    }
    int m=(l+r)>>1;
    int t=build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
    pushup(rt,t);
    return t^1;
}
int update(int L,int C,int l,int r,int rt)
{
    if(l==r)
    {
        s[rt]=C;
        return 0;
    }
    int m=(l+r)>>1;
    int t;
    if(L<=m)
        t=update(L,C,l,m,rt<<1);
    else
        t=update(L,C,m+1,r,rt<<1|1);
    pushup(rt,t);
    return t^1;
}
int main()
{
    int n,q;
    cin>>n>>q;
    build(1,1<<n,1);
    int p,v;
    while(q--)
    {
        cin>>p>>v;
        update(p,v,1,1<<n,1);
        cout<<s[1]<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值