-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfenwick.cpp
More file actions
39 lines (35 loc) · 1.17 KB
/
fenwick.cpp
File metadata and controls
39 lines (35 loc) · 1.17 KB
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
/*
* Fenwick Tree (0-indexed) with range update
*
* Time: O(log n) with low constant factor
* Status: stress tested (https://judge.yosupo.jp/submission/275173)
*/
class FenwickTree {
public:
int N;
vector<ll> fw, fw2;
FenwickTree(ll size) : N(size + 1), fw(N, 0), fw2(N, 0) {}
void update(int x, int y, ll v) { // inclusive
x++, y++; // Shift to 1-based indexing internally
for (int tx = x; tx < N; tx += tx & (-tx)) {
fw[tx] += v;
fw2[tx] -= v * (x - 1);
}
for (int ty = y + 1; ty < N; ty += ty & (-ty)) {
fw[ty] -= v;
fw2[ty] += v * y;
}
}
ll sum(int x) {
x++; // Shift to 1-based indexing internally
ll res = 0;
for (int tx = x; tx; tx -= tx & (-tx)) res += fw[tx] * x + fw2[tx];
return res;
}
ll range_sum(int x, int y) { // inclusive
return sum(y) - sum(x - 1);
}
};
// FenwickTree ft(n); // Init a Fenwick Tree (0-indexed)
// ll rs = ft.range_sum(p, q); // Do a range_sum from [p, q] inclusive
// ft.update(a, b, c) // Add c to every element in [a, b] inclusive