-
Notifications
You must be signed in to change notification settings - Fork 28
/
segment tree.cpp
97 lines (83 loc) · 1.52 KB
/
segment tree.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
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
93
94
95
96
97
#include<bits/stdc++.h>
using namespace std;
void build(int v,int tl,int tr,long long st[],int a[])
{
if(tl==tr)
{
st[v]=a[tl];
}
else
{
build(2*v,tl,(tl+tr)/2,st,a);
build(2*v+1,(tl+tr)/2+1,tr,st,a);
st[v]=st[v*2]+st[v*2+1];
}
return;
}
void update(int v,int tl,int tr,int pos,int val,long long st[])
{
if(pos<tl||pos>tr)
{
return;
}
else if(pos==tl&&pos==tr)
{
st[v]=val;
}
else
{
update(2*v,tl,(tl+tr)/2,pos,val,st);
update(2*v+1,(tl+tr)/2+1,tr,pos,val,st);
st[v]=st[v*2]+st[v*2+1];
}
return;
}
long long query(int v,int tl,int tr,int l,int r,long long st[])
{
if(r<tl||l>tr||r<l)
{
return 0;
}
else if(l==tl&&r==tr)
{
return st[v];
}
else
{
return query(v*2,tl,(tl+tr)/2,l,min(r,(tl+tr)/2),st) +
query(2*v+1,(tl+tr)/2+1,tr,max(l,(tl+tr)/2+1),r,st);
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,q;
cin>>n>>q;
int a[n+1];
for(int i=1; i<=n; i++)
{
cin>>a[i];
}
long long st[4*(n+1)];
build(1,1,n,st,a);
for(int i=1;i<=q;i++)
{
int type;
cin>>type;
if(type==1)
{
int k,x;
cin>>k>>x; // a[k]=x;
update(1,1,n,k,x,st);
}
else
{
int l,r;
cin>>l>>r;
cout<<query(1,1,n,l,r,st)<<'\n';
}
}
return 0;
}