-
Notifications
You must be signed in to change notification settings - Fork 0
/
fenwick.cpp
59 lines (52 loc) · 1.03 KB
/
fenwick.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
#include <bits/stdc++.h>
using namespace std;
template <class T>
class FenwickTree {
private:
vector<T> v;
int n;
public:
FenwickTree(int n) {
this->n = n;
v.assign(n + 1, 0);
}
T query(int l, int r) {
return query(r) - query(l - 1);
}
T query(int r) {
T sum = 0;
while (r > 0) {
sum += v[r];
r -= (r & (-r));
}
return sum;
}
void update(int i, T k) {
while (i < n + 1) {
v[i] += k;
i += (i & (-i));
}
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin >> n >> q;
FenwickTree<long long int> ft(n);
for(int i=0;i<q;i++) {
char op;
long long int value;
int index;
cin >> op;
if(op == '+') {
cin >> index >> value;
ft.update(index+1, value);
}
if(op == '?') {
cin >> index;
if(index == 0) cout << "0\n";
else cout << ft.query(index) << '\n';
}
}
}