forked from indy256/codelibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFenwickTree.cpp
More file actions
47 lines (40 loc) · 883 Bytes
/
FenwickTree.cpp
File metadata and controls
47 lines (40 loc) · 883 Bytes
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
#include <iostream>
using namespace std;
const int maxn = 200000;
int t[maxn];
void add(int t[], int i, int value) {
for (; i < maxn; i |= i + 1)
t[i] += value;
}
// sum[0,i]
int sum(int t[], int i) {
int res = 0;
for (; i >= 0; i = (i & (i + 1)) - 1)
res += t[i];
return res;
}
// Returns min(p|sum[0,p]>=sum)
int lower_bound(int t[], int sum) {
--sum;
int pos = -1;
for (int blockSize = 1 << 30; blockSize != 0; blockSize >>= 1) {
if (blockSize > maxn) continue;
int nextPos = pos + blockSize;
if (nextPos < maxn && sum >= t[nextPos]) {
sum -= t[nextPos];
pos = nextPos;
}
}
return pos + 1;
}
// Usage example
int main() {
add(t, 0, 4);
add(t, 1, 5);
add(t, 2, 5);
add(t, 2, 5);
cout << (4 == sum(t, 0)) << endl;
cout << (19 == sum(t, 2)) << endl;
cout << (2 == lower_bound(t, 19)) << endl;
cout << (maxn == lower_bound(t, 20)) << endl;
}