-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegmentTree.cpp
54 lines (47 loc) · 900 Bytes
/
segmentTree.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
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
struct SegmentTree{
ll n;
vector <ll>a;
void build(){
for(ll i=n-1; i>0; --i)a[i]=a[i<<1]+a[i<<1|1];
}
void update(ll pos, ll val){
for(a[pos+=n]=val; pos>1; pos>>=1)a[pos>>1]=a[pos]+a[pos^1];
}
ll query(ll l, ll r){
ll res=0;
for(l+=n, r+=n; l<r; l>>=1, r>>=1){
if(l&1)res+=a[l++];
if(r&1)res+=a[--r];
}
return res;
}
SegmentTree(vector<ll>& arr){
n=arr.size();
a = vector<ll>(n<<1);
for(ll i=0; i<n; ++i)a[i+n]=arr[i];
build();
}
};
int main(){
ll q, n;
vector <ll>arr;
do{
cin>>n;
arr.push_back(n);
}while(n);
cout<<"Enter no. of queries\n";
cin>>q;
SegmentTree s = SegmentTree(arr);
while(q--){
ll u,v, w;
cin>>u>>v>>w;
if(u==1)s.update(v+1,w);
else
cout<<s.query(v-1,w)<<"\n";
}
return 0;
}