-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsegtree.cpp
147 lines (118 loc) · 2.88 KB
/
segtree.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define ll long long int
#define ld long int
#define nl "\n"
#define ss second
#define ff first
using namespace std;
ll MOD = 1000000007;
struct segtree{
ll size;
vector<ll> sums;
/*Initializing the sums vector size(or we can say segtree)*/
void init(int n){
size = 1;
while(size<n){
size*=2;
}
sums.assign(2*size,0LL);
}
/*build part2 -> here we use recursion to iterate over segtree
and add the value of array to the leaf */
void build2(vector<ll> &a,ll x,ll lx,ll rx){
if(rx-lx == 1){
if(lx<a.size()){
sums[x] = a[lx];
}
return;
}
ll m = (lx+rx)/2;
build2(a,2*x+1,lx,m);
build2(a,2*x+2,m,rx);
sums[x] = sums[2*x+1] + sums[2*x+2];
}
/* assigning the values of given vector to segtree*/
void build(vector<ll> &a){
build2(a,0,0,size);
}
/* setting a particluar value of leaf node of segtree */
void set(ll i ,ll v,ll x,ll lx,ll rx){
if(rx-lx == 1){
sums[x] = v;
return;
}
ll m = (lx+rx)/2;
if(i<m){
set(i,v,2*x+1,lx,m);
}
else{
set(i,v,2*x+2,m,rx);
}
sums[x] = sums[2*x+1] + sums[2*x+2];
}
void set(ll i,ll v){
set(i,v,0,0,size);
}
/* finding sum now */
ll sum(ll l ,ll r,ll x,ll lx,ll rx){
/* if the whole segment is outside the limits then return 0 */
if(lx>=r || rx<=l){
return 0;
}
/* if the whole segment is inside our limits pick all*/
else if(lx>=l && rx<=r){
return sums[x];
}
/* else perform recursion to iterate over it and find the sum of particular leaf
*/
ll m = (lx+rx)/2;
ll s1 = sum(l,r,2*x+1,lx,m);
ll s2 = sum(l,r,2*x+2,m,rx);
return s1+s2;
}
ll sum(ll l,ll r){
return sum(l,r,0,0,size);
}
};
void todo()
{
ll n,m;
cin >> n >> m;
segtree st;
st.init(n);
vector<ll> a(n);
for(ll i = 0;i<n;i++){
cin >> a[i];
}
st.build(a);
for(ll j = 0;j<m;j++){
ll op;
cin >> op;
if(op == 1){
ll i,v;
cin >> i>> v;
st.set(i,v);
}
else{
ll l,r;
cin >> l >> r;
cout << st.sum(l,r) << nl;
}
}
}
int main()
{
// Main Code
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll tt = 1;
// cin >> tt;
for (ll i = 1; i <= tt; i++)
{
// cout << "Case: " << i << " ";
todo();
}
return 0;
}