-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfenwick.cpp
More file actions
53 lines (46 loc) Β· 843 Bytes
/
fenwick.cpp
File metadata and controls
53 lines (46 loc) Β· 843 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
48
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> fenwick;
vector<ll> vec;
int n;
void init(int len) {
n = len;
fenwick.assign(n+1, 0);
vec.assign(n+1, 0);
}
ll rsq(int a) {
ll res = 0;
a++;
while(a) {
res += fenwick[a];
a -= (a&-a);
}
return res;
}
void adjust(int a, int v) {
a++;
while(a<=n) {
fenwick[a] += v;
a += (a&-a);
}
}
void add(int a, int v) {
int diff = v - vec[a];
adjust(a, diff);
}
ll query(int a, int b) {
return rsq(b) - rsq(a-1);
}
int main()
{
init(5);
add(0, 3);
add(2, 1);
cout << query(0, 2) << endl; // expect 4
cout << query(0, 1) << endl; // expect 3
add(1, 5);
cout << query(0, 2) << endl; // expect 9
add(0, -3);
cout << query(0, 2) << endl; // expect 6
}