CodeForces 339D Xenia and Bit Operations

题目链接:http://codeforces.com/problemset/problem/339/D


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

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


思路:稍微分析一下题目就能够知道,可以构造一棵二叉树,而且该二叉树的叶子节点全部在最底层,叶子节点存储的是最初的数据,然后从叶子节点开始两两进行位操作,并将结果传给其父节点,接着向上走继续位操作,顺序是最底层或,然后倒数第二层异或、第三层或,...,依次交替,直到树根,树根的数据即为答案。再仔细想一下,很容易想到用线段树来维护,一旦到达叶子节点,就返回0,表示进行或操作,否则进行递归调用,将从子树传回来的位操作信息应用到两棵子树的数据,得到该节点的数据,然后将位操作信息异或1返回给当前节点的父节点,因为位操作是交替进行的。注意:我是用0表示或操作,1表示异或操作,所以从0开始每次异或1就能够进行交替的操作了,一个小技巧而已。


附上AC代码:

#include <bits/stdc++.h>
#define lrt rt<<1
#define rrt rt<<1|1
#define lson l, m, lrt
#define rson m+1, r, rrt
using namespace std;
const int maxn = 1<<17;
int xorv[maxn<<2];
int n, q;

void push_up(int rt, int ok){
	if (!ok)
		xorv[rt] = xorv[lrt]|xorv[rrt];
	else
		xorv[rt] = xorv[lrt]^xorv[rrt];
}

int build(int l, int r, int rt){
	if (l == r){
		int num;
		cin >> num;
		xorv[rt] = num;
		return 0;
	}
	int m = (l+r)>>1;
	int t = build(lson);
	build(rson);
	push_up(rt, t);
	return t^1;
}

int update(int p, int val, int l, int r, int rt){
	if (l == r){
		xorv[rt] = val;
		return 0;
	}
	int m = (l+r)>>1;
	int t = 0;
	if (p <= m)
		t = update(p, val, lson);
	else
		t = update(p, val, rson);
	push_up(rt, t);
	return t^1;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> q;
	build(1, 1<<n, 1);
	int p, v;
	while (q--){
		cin >> p >> v;
		update(p, v, 1, 1<<n, 1);
		cout << xorv[1] << endl;
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值