-
Notifications
You must be signed in to change notification settings - Fork 0
/
fenwick.cpp
57 lines (48 loc) · 809 Bytes
/
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAXN 5000005
long long int bit[MAXN];
long long int n,q;
long long int getSum(int idx)
{
long long int sum = 0;
for(int i = idx; i > 0; i -= i&-i)
{
sum += bit[i];
}
return sum;
}
void update(long long int idx, long long int val)
{
for(long long int i = idx; i < n+5 ; i += i&-i)
{
bit[i] += val;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
scanf("%lld%lld\n", &n, &q);
for(int i = 0; i <= n; i++)
bit[i] = 0;
char l;
long long int a,b;
for(long long int i = 0; i < q; i++)
{
scanf("%c", &l);
if(l == '+')
{
scanf("%lld%lld\n", &a, &b);
update(a+1,b);
}
else
{
scanf("%lld\n", &a);
printf("%lld\n", getSum(a));
}
}
return 0;
}