-
Notifications
You must be signed in to change notification settings - Fork 5
/
segment_tree.cpp
56 lines (49 loc) · 1.26 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
#include <stdio.h>
using namespace std;
#define MAX (int)10e6
int values[MAX], tree[MAX * 4];
int read(int id, int l, int r, int x, int y){
if(l > y || r < x) return 0;
else{
if(l >= x && r <= y) return tree[id];
else{
int mid = (l+r)/2;
int q1 = read(id*2, l, mid, x, y);
int q2 = read(id*2+1, mid+1, r, x, y);
return q1 + q2;
}
}
}
void update(int id, int l, int r, int p, int value){
if(l == r) tree[id] = value;
else{
int mid = (l+r)/2;
if(p <= mid) update(id*2, l, mid, p, value);
else update(id*2+1, mid+1, r, p, value);
tree[id] = tree[id*2] + tree[id*2+1];
}
}
void build(int id, int l, int r){
if(l == r) tree[id] = values[l];
else{
int mid = (l+r)/2;
build(id*2, l, mid);
build(id*2+1, mid+1, r);
tree[id] = tree[id*2] + tree[id*2+1];
}
}
int main(){
int n, q, x, y, opc;
scanf("%d %d", &n, &q);
for(int i=1; i<=n; i++) scanf("%d", &values[i]);
build(1, 1, n);
while(q--){
scanf("%d %d %d", &opc, &x, &y);
if(opc == 1){ //read
printf("%d\n", read(1, 1, n, x, y));
}else{ //update
update(1, 1, n, x, y);
}
}
return 0;
}