Codeforces 339D Xenia and Bit Operations 线段树单点修改

传送门:http://codeforces.com/contest/339/problem/D

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.

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

题意:给一个长度为2^n的数组,对相邻的数先进行OR操作,得到新的数组,在对新的数组进行XOR操作,再OR……XOR,当数组元素个数为1的时候,输入这个是数。比如

 (1, 2, 3, 4)  →  (1 or 2 = 3, 3 or 4 = 7)  →  (3 xor 7 = 4). 输出4.

对于每一个修改,将Pi位置的数改变为Bi,改变后输出上述操作的值。

思路:用线段树的每一层来维护上述的每一次操作。标记叶子节点为XOR操作,根节点的标记与儿子节点的标记相反,表示不同的两种操作。修改维护的时候,直接对两个子树维护的值进行位运算即可。最后输出线段树根节点维护的值。

代码:

#include
#include
#define maxn ((1<<17)+5)
using namespace std;
bool c[maxn << 2];
int sum[maxn << 2];
int v[maxn];
void build(int id, int L, int R)
{
	if (L == R)
	{
		c[id] = 1;
		sum[id] = v[L];
	}
	else
	{
		int mid = (L + R) >> 1;
		build(id << 1, L, mid);
		build(id << 1 | 1, mid + 1, R);
		c[id] = c[id << 1] ^ 1;
		if (c[id]) sum[id] = sum[id << 1] ^ sum[id << 1 | 1];
		else sum[id] = sum[id << 1] | sum[id << 1 | 1];
	}
}
void update(int id, int L, int R, int pos, int x)
{
	if (L == R) sum[id] = x;
	else
	{
		int mid = (L + R) >> 1;
		if (pos <= mid) update(id << 1, L, mid, pos, x);
		else update(id << 1 | 1, mid + 1, R, pos, x);
		if (c[id]) sum[id] = sum[id << 1] ^ sum[id << 1 | 1];
		else sum[id] = sum[id << 1] | sum[id << 1 | 1];
	}
}
int main()
{
	int n, m;
	scanf("%d %d", &n, &m);
	n = 1 << n;
	for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
	build(1, 1, n);
	while (m--)
	{
		int pos, x;
		scanf("%d %d", &pos, &x);
		update(1, 1, n, pos, x);
		printf("%d\n", sum[1]);
	}
	return 0;
}



你可能感兴趣的:(数据结构)