-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dynamic Range Sum Queries
68 lines (60 loc) · 1.77 KB
/
Dynamic Range Sum Queries
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
#include <bits/stdc++.h>
using namespace std;
void segmentTree(int ind, int low, int high, vector<long long> &seg, vector<long long> &inp) {
if(low == high) {
seg[ind] = inp[low];
return;
}
int mid = (low + high) >> 1;
segmentTree(2 * ind + 1, low, mid, seg, inp);
segmentTree(2 * ind + 2, mid + 1, high, seg, inp);
seg[ind] = seg[2 * ind + 1] + seg[2 * ind + 2];
}
void update(int ind, int low, int high, int pos, long long val, vector<long long> &seg) {
if(low == high) {
seg[ind] = val;
return;
}
int mid = (low + high) >> 1;
if(pos <= mid) update(2 * ind + 1, low, mid, pos, val, seg);
else update(2 * ind + 2, mid + 1, high, pos, val, seg);
seg[ind] = seg[2 * ind + 1] + seg[2 * ind + 2];
}
long long query(int ind, int low, int high, int l, int r, vector<long long> &seg) {
if(high < l || low > r) return 0LL;
if(low >= l && high <= r) return seg[ind];
int mid = (low + high) >> 1;
long long left = query(2 * ind + 1, low, mid, l, r, seg);
long long right = query(2 * ind + 2, mid + 1, high, l, r, seg);
return left + right;
}
void test_case() {
int n, q;
cin >> n >> q;
vector<long long> inp(n);
for(auto &a : inp) cin >> a;
vector<long long> seg(4 * n);
segmentTree(0, 0, n - 1, seg, inp);
while(q--) {
int type, l, r;
cin >> type;
if(type == 1) {
int pos;
long long val;
cin >> pos >> val;
pos--;
update(0, 0, n - 1, pos, val, seg);
} else {
cin >> l >> r;
l--;
r--;
cout << query(0, 0, n - 1, l, r, seg) << endl;
}
}
}
int main(){
int t = 1;
// cin >> t;
while(t--) test_case();
return 0;
}