-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFenwickTree.cpp
33 lines (33 loc) · 891 Bytes
/
FenwickTree.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
class FenwickTree {
const int IDENTITY = 0;
int fn(int a, int b) { return a+b; }
int invFn(int a, int b) { return a-b; }
int diff(int i, int x) { return invFn(x, a[i]); }
vector<int> bit, a;
public:
FenwickTree(int n): bit(n, IDENTITY), a(n, IDENTITY) {}
FenwickTree(vector<int> &a): bit(SZ(a), IDENTITY), a(a) {
for (int i = 0; i < SZ(a); i++) {
bit[i] = fn(bit[i], a[i]);
int r = i | (i+1);
if (r < SZ(a)) bit[r] = fn(bit[r], bit[i]);
}
}
void update(int i, int x) {
int delta = diff(i,x);
a[i] = x;
for (int j = i; j < SZ(a); j |= (j+1)) {
bit[j] = fn(bit[j], delta);
}
}
int query(int r) {
int res = IDENTITY;
for (;r >= 0; r = (r & (r+1)) - 1) {
res = fn(res, bit[r]);
}
return res;
}
int query(int l, int r) {
return l == r ? a[l]: l > 0 ? invFn(query(r), query(l-1)): query(r);
}
};