-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
FenwickTree.cpp
66 lines (57 loc) · 946 Bytes
/
FenwickTree.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
63
64
65
66
#include<bits/stdc++.h>
using namespace std;
#define mx 100009
typedef long long int ll;
int arr[mx];
int bit[mx];
int n,m;
void update(int index,ll val)
{
index+=1;
while(index<=n)
{
bit[index]+=val;
index += index & (-index);
}
}
ll getsum(int index)
{
ll sum=0;
index+=1;
while(index>0)
{
//cout<<index<<" "<<bit[index]<<endl;
sum+=bit[index];
index-=index&(-index);
}
return sum;
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>arr[i];
memset(bit,0,sizeof bit);
for(int i=0;i<n;i++)
{
update(i,arr[i]);
}
int x,y,t;
while(m--)
{
cin>>t;
if(t==1)
{
cin>>x>>y;
ll diff=y-arr[x];
update(x,diff);
arr[x]=y;
}
else
{
cin>>x;
cout<<getsum(x)<<endl;
}
}
return 0;
}