-
Notifications
You must be signed in to change notification settings - Fork 0
/
1650 - Range Xor Queries.cpp
66 lines (54 loc) · 1.55 KB
/
1650 - Range Xor Queries.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
66
#include <bits/stdc++.h>
using namespace std;
int n, q;
vector<long long> *a, *seg;
void build(int index = 1, int seg_l = 0, int seg_r = n - 1) {
if (seg_l == seg_r) {
(*seg)[index] = (*a)[seg_l];
} else {
int seg_m = (seg_l + seg_r) / 2;
build(2 * index, seg_l, seg_m);
build(2 * index + 1, seg_m + 1, seg_r);
(*seg)[index] = (*seg)[2 * index] ^ (*seg)[2 * index + 1];
}
}
long long xor_sum(int l, int r, int index = 1, int seg_l = 0,
int seg_r = n - 1) {
if (l > r) return 0;
if (l == seg_l && r == seg_r) {
return (*seg)[index];
}
int seg_m = (seg_l + seg_r) / 2;
return xor_sum(l, min(seg_m, r), 2 * index, seg_l, seg_m) ^
xor_sum(max(l, seg_m + 1), r, 2 * index + 1, seg_m + 1, seg_r);
}
void update(int pos, int val, int index = 1, int seg_l = 0, int seg_r = n - 1) {
if (seg_l == seg_r) {
(*seg)[index] = val;
} else {
int seg_m = (seg_l + seg_r) / 2;
if (pos <= seg_m) {
update(pos, val, index * 2, seg_l, seg_m);
} else {
update(pos, val, index * 2 + 1, seg_m + 1, seg_r);
}
(*seg)[index] = (*seg)[2 * index] ^ (*seg)[2 * index + 1];
}
}
int main() {
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// cout.tie(nullptr);
cin >> n >> q;
a = new vector<long long>(n);
seg = new vector<long long>(4 * n);
for (int i = 0; i < n; ++i) {
cin >> (*a)[i];
}
build();
int x, y, k, u, type;
while (q--) {
cin >> x >> y;
cout << xor_sum(x - 1, y - 1) << "\n";
}
}