-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathefficient_segment_tree.cpp
More file actions
95 lines (86 loc) · 1.47 KB
/
efficient_segment_tree.cpp
File metadata and controls
95 lines (86 loc) · 1.47 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <vector>
using namespace std;
vector<int> tree;
size_t n;
void init(const vector<int> &vec) {
n = vec.size();
tree.assign(n << 1, 0);
for (int i = 0; i < n; ++i)
tree[i + n] = vec[i];
}
void build(const vector<int> &vec) {
init(vec);
for (int i = n - 1; i > 0; --i)
tree[i] = tree[i << 1] + tree[(i << 1) | 1];
}
void build(int s) {
n = s;
tree.assign(s << 1, 0);
}
int query(int l, int r) { // min[l, r)
l += n;
r += n;
int res = 0;
while (l < r) {
if (l & 1)
res = res + tree[l++];
if (r & 1)
res = res + tree[--r];
l >>= 1;
r >>= 1;
}
return res;
}
int query(int l) {
int res = 0;
l += n;
while (l > 0) {
res = res + tree[l];
l >>= 1;
}
return res;
}
void modify(int l, int del) { // modify[l, l]
l += n;
tree[l] += del;
while (l > 0) {
tree[l >> 1] = tree[l] + tree[l ^ 1];
l >>= 1;
}
}
void modify(int l, int r, int del) { // modify[l, r)
l += n;
r += n;
while (l < r) {
if (l & 1)
tree[l++] += del;
if (r & 1)
tree[--r] += del;
l >>= 1;
r >>= 1;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int64_t t;
cin >> t;
while (t--) {
size_t l, c;
cin >> l >> c;
build(l);
while (c--) {
int64_t p, q, v;
cin >> p >> q >> v;
modify(p, q + 1, v);
}
cin >> c;
while (c--) {
int64_t s;
cin >> s;
cout << query(s) << '\n';
}
}
return 0;
}