-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path(Range Queries) Dynamic Range Queries
More file actions
70 lines (60 loc) · 1.38 KB
/
Copy path(Range Queries) Dynamic Range Queries
File metadata and controls
70 lines (60 loc) · 1.38 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
#include <bits/stdc++.h>
using namespace std;
#define int long long
const long long mod =1e9+7;
vector<int> vec;
vector<int> segTree;
void buildTree(int pos, int l, int r){
if(l==r){
segTree[pos]=vec[l];
return;
}
int mid=(l+r)/2;
buildTree(2*pos, l, mid);
buildTree(2*pos+1, mid+1, r);
segTree[pos]=segTree[2*pos]+segTree[2*pos+1];
}
void updateTree(int k, int u, int l, int r, int pos){
if(l==r && k==l){
segTree[pos]=u;
return;
}
if(k <= r && k>= l){
int mid=(l+r)/2;
updateTree(k, u, l, mid, 2*pos);
updateTree(k, u, mid+1, r, 2*pos+1);
segTree[pos]=segTree[2*pos+1]+segTree[2*pos];
}
}
int sumQuery(int a, int b, int l, int r, int pos){
if(a>r || b<l){
return 0;
}
if(a<=l && b>=r){
return segTree[pos];
}
int mid=(l+r)/2;
return sumQuery(a, b, l, mid, 2*pos)+sumQuery(a, b, mid+1, r, 2*pos+1);
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin>>n>>q;
vec.resize(n+1);
segTree.resize(4*n+1);
for(int i=1; i<=n; i++){
cin>>vec[i];
}
buildTree(1, 1, n);
for(int i=0; i<q; i++){
int a, b, c;
cin>>a>>b>>c;
if(a==1){
updateTree(b, c, 1, n, 1);
}else{
cout<<sumQuery(b, c, 1, n, 1)<<endl;
}
}
return 0;
}