链接:http://codeforces.com/problemset/problem/339/D
题目:
Description
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 sequencea1 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 mqueries. 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 Input
2 4 1 6 3 5 1 4 3 4 1 2 1 2
1 3 3 3
题目大意:
给你(2^n)个数,更新其中的某个值,交替对这些数进行“或操作”和“异或操作”,得到最终的结果。
解题思路:
这题可以用线段树来做,主要就涉及线段树的一个单点更新问题。我们可以在更新元素的同时,更新从这个元素到根节点上所有的节点的值。这样最后根节点的值,就是我们所要求的最终结果。
代码:
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 1 << 19;
struct Tree
{
int left, right;
int value;
};
Tree tree[MAXN];
void build_tree(int l, int r, int i)
{
tree[i].left = l;
tree[i].right = r;
tree[i].value = 0;
if(l == r) return ;
int mid = (l + r) >> 1;
build_tree(l, mid, 2*i);
build_tree(mid+1, r, 2*i+1);
}
void insert_tree(int l, int r, int i, int x, int cur)
{
if(l == tree[i].left && r == tree[i].right)
{
tree[i].value = x;
return ;
}
int mid = (tree[i].left + tree[i].right) >> 1;
if(r <= mid)
{
insert_tree(l, r, 2*i, x, cur - 1);
}
else
{
insert_tree(l, r, 2*i+1, x, cur - 1);
}
if(0 == cur % 2)
{
tree[i].value = tree[2*i].value | tree[2*i+1].value;
}
else
{
tree[i].value = tree[2*i].value ^ tree[2*i+1].value;
}
}
int query_tree()
{
return tree[1].value;
}
int main()
{
std::ios::sync_with_stdio(false);
int n, m; cin >> n >> m;
int num = 1 << n;
build_tree(1, num, 1);
for(int i = 1; i <= num; i++)
{
int x; cin >> x;
insert_tree(i, i, 1, x, n + 1);
}
while(m--)
{
int a, b; cin >> a >> b;
insert_tree(a, a, 1, b, n + 1);
cout << query_tree() << endl;
}
return 0;
}