-
Notifications
You must be signed in to change notification settings - Fork 5
/
segment_tree.cpp
78 lines (68 loc) · 1.94 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**************************************
* author - Amirul Islam (shiningflash)
--------------------------------------
* Segment tree (data structure)
--------------------------------------
* height (total) - log(n)
* per query - O(log n)
* per update - O(log n)
* init func - O(n log n)
* total complexity - O(n log n)
**************************************/
#include <bits/stdc++.h>
using namespace std;
const int mx = 100005;
/******* param ***********
* arr[] - main array
* tree[] - conceptual tree
* b - begin index
* e - end index
* node - root node index
* ls - left part sum
* rs - right part sum
*************************/
int arr[mx];
int tree[mx*4];
int n;
void print(int a[], int b, int e) {
for (int i = b; i <= e; printf("%d %d\n", i, a[i]), i++);
printf("\n");
}
void init(int node, int b, int e) {
if (b == e) tree[node] = arr[b];
else {
int left = node << 1, right = left | 1, mid = (b+e) >> 1;
init(left, b, mid);
init(right, mid+1, e);
tree[node] = tree[left] + tree[right];
}
}
int query(int node, int b, int e, int i, int j) {
if (i > j) return 0;
if (i == b && j == e) return tree[node];
int left = node << 1, right = left | 1, mid = (b+e) >> 1;
return query(left, b, mid, i, min(mid, j)) + query(right, mid+1, e, max(mid+1, i), j);
}
void update(int node, int b, int e, int i, int val) {
if (b == e) tree[node] = val;
else {
int left = node << 1, right = left | 1, mid = (b+e) >> 1;
if (i <= mid) update(left, b, mid, i, val);
else update(right, mid+1, e, i, val);
tree[node] = tree[left] + tree[right];
}
}
int main() {
// freopen("in", "r", stdin);
int n, q, i, j, ans;
scanf("%d %d", &n, &q);
for (int k = 1; k <= n; scanf("%d", &arr[k]), k++);
init(1, 1, n);
while (q--) {
scanf("%d %d", &i, &j);
ans = query(1, 1, n, i, j);
printf("%d\n", ans);
}
return 0;
}
// 14 Feb, 2019 //////////////////