-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path339-D.cpp
65 lines (52 loc) · 1.33 KB
/
339-D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
data structures > segment tree
difficulty: easy
date: 07/Dec/2019
by: @brpapa
*/
#include <cmath>
#include <iostream>
#include <vector>
#define MAX_N 132000 // pow(2, 17)
using namespace std;
int A[MAX_N];
int N;
vector<int> tree;
int le(int v) { return 2 * v + 1; };
int ri(int v) { return 2 * v + 2; };
void build(int v, int l, int r, bool isExcl) {
if (l == r) {
tree[v] = A[l];
return;
}
int mid = (l + r) / 2;
build(le(v), l, mid, !isExcl);
build(ri(v), mid + 1, r, !isExcl);
tree[v] = isExcl ? (tree[le(v)] ^ tree[ri(v)]) : (tree[le(v)] | tree[ri(v)]);
}
void pointUpdate(int v, int l, int r, bool isExcl, int i, int value) {
if (i > r || i < l) return;
if (l == r) {
tree[v] = value;
return;
}
int mid = (l + r) / 2;
pointUpdate(le(v), l, mid, !isExcl, i, value);
pointUpdate(ri(v), mid + 1, r, !isExcl, i, value);
tree[v] = isExcl ? (tree[le(v)] ^ tree[ri(v)]) : (tree[le(v)] | tree[ri(v)]);
}
int main() {
int M; cin >> N >> M;
bool opRootIsExclusive = N % 2 == 0;
N = pow(2, N);
for (int i = 0; i < N; i++) cin >> A[i];
tree.assign(2 * N, 0);
build(0, 0, N - 1, opRootIsExclusive);
int p, b;
while (M--) {
cin >> p >> b;
pointUpdate(0, 0, N-1, opRootIsExclusive, p-1, b);
cout << tree[0] << endl;
}
return 0;
}