forked from spandey1296/Learn-Share-Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SegmentTree.cpp
98 lines (86 loc) ยท 1.84 KB
/
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
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
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define F first
#define S second
typedef long long int ll;
const int mod = 1e9+7;
void FAST(){
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
cout.tie(nullptr);
}
void fileIO(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
ll query(vector<ll>&st, ll tl, ll tr, ll tv, ll l, ll r){
// completely outside the given range
if(tl > r || tr < l){
return 0;
}
// completely inside the given range
if(tl >= l && tr <= r){
return st[tv];
}
// Partially inside and partially outside
ll tm = (tl+tr)/2;
ll ans1 = query(st, tl, tm, 2*tv, l, r);
ll ans2 = query(st, tm+1, tr, 2*tv+1, l, r);
return ans1 +ans2;
}
void updateTree(vector<ll>&arr, vector<ll>&st, ll tl, ll tr, ll tv, ll idx, ll value){
if(tl==tr){
arr[idx] = value;
st[tv] = arr[idx];
return;
}
ll tm = (tl+tr)/2;
if(idx > tm){
updateTree(arr, st, tm+1, tr, 2*tv+1, idx, value);
} else {
updateTree(arr, st, tl, tm, 2*tv, idx, value);
}
st[tv] = st[2*tv]+st[2*tv+1];
}
void buildTree(vector<ll>&arr, vector<ll>&st, ll tl, ll tr, ll tv){
// BASE CASE
if(tl == tr){
st[tv] = arr[tl];
return;
}
ll tm =(tl+tr)/2;
// two recursive calls to build the left half and
// right half respectively
buildTree(arr, st, tl, tm, 2*tv);
buildTree(arr, st, tm+1, tr, 2*tv+1);
// the answer to the recursive calls gets stored in
// the tv
st[tv] = st[2*tv]+st[2*tv + 1];
}
int main(){
FAST();
fileIO();
ll n,q,x, a,b,k,u,qt;
cin>>n>>q;
vector<ll> arr(n);
vector<ll> st(4*n);
for(ll i=0 ; i<n ; i++){
cin>>arr[i];
}
buildTree(arr, st, 0,n-1,1);
for(ll i=0 ; i<q ; i++){
cin>>qt;
if(qt==1){
cin>>k>>u;
updateTree(arr, st, 0, n-1, 1, k-1, u);
}
if(qt==2){
cin>>a>>b;
cout<<query(st, 0, n-1, 1, a-1, b-1)<<"\n";
}
}
return 0;
}