-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfenwick.cpp
More file actions
48 lines (41 loc) · 1004 Bytes
/
fenwick.cpp
File metadata and controls
48 lines (41 loc) · 1004 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
/*
* Author: Maanas Karwa
* It is ok to share my code anonymously for educational purposes
* */
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n,q;
ll fenwickTree[5000001];
void add(int index, ll num) {
for (int i = index + 1; i <= n; i += (i & -i)) {
fenwickTree[i] += num;
}
}
ll sum(int index) {
ll val = 0;
for (int i = index+1; i > 0; i -= (i & -i)) {
val += fenwickTree[i];
}
return val;
}
//code TLEd without fast I/O. Keep in mind when dealing with huge amount of IO
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin.exceptions(std::istream::failbit);
memset(fenwickTree, 0, sizeof(fenwickTree));
cin >> n >> q;
char c; int t1,t2;
while (q--) {
cin >> c;
if (c == '+') {
cin >> t1 >> t2;
add(t1,t2);
} else if (c == '?') {
cin >> t1;
if (!t1) cout << "0\n"; //edge case
else cout << sum(t1-1) << "\n"; //t1 -1 since they interpreted as sum till that index
}
}
}