-
Notifications
You must be signed in to change notification settings - Fork 153
/
segment_tree_range_query_point_update.cpp
68 lines (59 loc) · 1.64 KB
/
segment_tree_range_query_point_update.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
/**
* Description: Segment Tree Range query Point update.
* Usage: construct O(N), update O(lg(N)), query O(lg(N))
* Source: https://github.com/dragonslayerx
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <climits>
using namespace std;
#define MAX 100005
long long a[MAX];
int st[4*MAX];
int construct (int node, int ll, int rl){
if (ll == rl) st[node] = a[ll];
else {
int left = construct(2*node+1, ll, (ll+rl)/2);
int right = construct(2*node+2, (ll+rl)/2 + 1, rl);
st[node] = left + right;
}
return st[node];
}
int query(int node, int ll, int rl, int ql, int qr){
if (ll >= ql && rl <= qr) return st[node];
else if (rl < ql || ll > qr) return 0;
int left = query(2*node+1, ll, (ll+rl)/2, ql, qr);
int right = query(2*node+2, (ll+rl)/2 + 1, rl, ql, qr);
return left + right;
}
int update(int node, int ll, int rl, int q, int val){
if (rl < q || ll > q) return st[node];
if (q == ll && q == rl) st[node] = val;
else {
int left = update(2*node+1, ll, (ll+rl)/2, q, val);
int right = update(2*node+2, (ll+rl)/2 + 1, rl, q, val);
st[node] = left + right;
}
return st[node];
}
int main()
{
int tc;
scanf("%d", &tc);
for (int t=1; t<=tc; t++ ) {
printf("Case %d:\n", t);
memset(a, 0, sizeof(a));
int n, q;
scanf("%d%d", &n, &q);
for (int i = 0; i < n; i++)
scanf("%lld", &a[i]);
construct(0, 0, n-1);
while (q--) {
int x, y;
scanf("%d%d", &x, &y);
x--, y--;
printf("%d\n", query(0, 0, n-1, x, y));
}
}
}