forked from mejtfk/Algorithms-HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFenwick_Tree.cpp
More file actions
93 lines (67 loc) · 1.57 KB
/
Fenwick_Tree.cpp
File metadata and controls
93 lines (67 loc) · 1.57 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<bits/stdc++.h>
#include<string.h>
#include<tuple>
#define all(c) c.begin(),c.end()
#define tr(n,x) for(int i=x;x<n?i<n:i>=n;x<n?i++:i--)
#define ll long long int
#define pb push_back
#define em emplace_back
#define mk make_pair
#define isro ios_base::sync_with_stdio(false)
#define find(v,x) find(all(v),x)!=v.end()
#define debug(c) cout<<c<<endl;
using namespace std;
typedef vector<ll>vi;
typedef vector<vi>vii;
typedef vector<string>si;
typedef vector<si>sii;
typedef pair<ll,ll>pi;
typedef tuple<ll,ll,ll>ti;
void update(vi &v, int index, int val){
while(index<v.size()){
v[index]+=val;
index+=(index&-index);
}
}
int range_qurey(vi &v, int l, int r){
int sum1=0,sum2=0;
while(r>0){
sum1+=v[r];
r-=(r&-r);
}
while(l>0){
sum2+=v[l];
l-=(l&-l);
}
return sum1-sum2;
}
int main(){
isro;
int n,t;
cin>>n>>t;
vi bit(n+1,0);
//Create Fenwick_tree
for(int i=1;i<=n;i++){
int val;
cin>>val;
update(bit,i,val);
}
while(t--){
int choice;
cin>>choice;
//Choice 1 for update
if(choice==1){
int index,val;
cin>>index>>val;
update(bit,index,val);
}
//Choice 2 for find sum in range l to r
if(choice==2){
int l,r;
cin>>l>>r;
cout<<range_qurey(bit,l,r);
}
}
}
//For more deitals please follow below link
//https://www.hackerearth.com/practice/data-structures/advanced-data-structures/fenwick-binary-indexed-trees/tutorial/