Skip to content

Commit 5af742e

Browse files
authored
Create FenwickTree.cpp
1 parent 143d2e1 commit 5af742e

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

Graph/FenwickTree/FenwickTree.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include<bits/stdc++.h>
2+
3+
using namespace std;
4+
#define mx 100009
5+
typedef long long int ll;
6+
int arr[mx];
7+
int bit[mx];
8+
int n,m;
9+
void update(int index,ll val)
10+
{
11+
index+=1;
12+
while(index<=n)
13+
{
14+
bit[index]+=val;
15+
index += index & (-index);
16+
17+
}
18+
19+
}
20+
ll getsum(int index)
21+
{
22+
ll sum=0;
23+
index+=1;
24+
while(index>0)
25+
{
26+
//cout<<index<<" "<<bit[index]<<endl;
27+
sum+=bit[index];
28+
index-=index&(-index);
29+
}
30+
return sum;
31+
}
32+
int main()
33+
{
34+
35+
cin>>n>>m;
36+
for(int i=0;i<n;i++)
37+
cin>>arr[i];
38+
39+
memset(bit,0,sizeof bit);
40+
for(int i=0;i<n;i++)
41+
{
42+
update(i,arr[i]);
43+
}
44+
int x,y,t;
45+
46+
47+
while(m--)
48+
{
49+
50+
cin>>t;
51+
if(t==1)
52+
{
53+
cin>>x>>y;
54+
ll diff=y-arr[x];
55+
update(x,diff);
56+
arr[x]=y;
57+
}
58+
else
59+
{
60+
cin>>x;
61+
62+
cout<<getsum(x)<<endl;
63+
}
64+
}
65+
return 0;
66+
}

0 commit comments

Comments
 (0)