-
Notifications
You must be signed in to change notification settings - Fork 37
/
fenwick.cpp
62 lines (59 loc) · 902 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
58
59
60
61
62
#include<bits/stdc++.h>
using namespace std;
#define int long long
struct FenTree{
int size;
vector<int> f;
void init(int n){
size = n + 1;
f.assign(size, 0);
}
void update(int idx, int v){
while(idx < size){
f[idx] += v;
idx += idx&-idx;
}
}
int sum(int idx){
int ans = 0;
while(idx > 0){
ans += f[idx];
idx -= idx&-idx;
}
return ans;
}
int sum(int l, int r){
return sum(r) - sum(l - 1);
}
};
void solve(){
int n, q;
cin>>n>>q;
FenTree ft;
ft.init(n);
for(int i = 0; i < q; i++){
int type;
cin>>type;
if(type == 1){
//to change value at index idx by v
int idx, v;
cin>>idx>>v;
ft.update(idx, v);
}
else{
// find the sum of the range [l, r]
int l, r;
cin>>l>>r;
cout<<ft.sum(l, r)<<'\n';
}
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
//cin>>t;
while(t--)
solve();
return 0;
}