-
Notifications
You must be signed in to change notification settings - Fork 7
/
segtree.cpp
83 lines (68 loc) · 1.65 KB
/
segtree.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
79
80
81
82
83
#include <bits/stdc++.h>
#define MAXN 100000
vector<int> seg(4 * MAXN + 2), lazy(4 * MAXN + 2);
int left(int i) {
return 2*i+1;
}
int right(int i) {
return 2*i+2;
}
void propagate(int idx) {
seg[idx] += lazy[idx];
lazy[left(idx)] += lazy[idx];
lazy[right(idx)] += lazy[idx];
lazy[idx] = 0;
}
void radd(int ql, int qr, int val, int l, int r, int idx) {
if (l > r) return;
if (l != r) propagate(idx);
if (ql > r or qr < l) return;
if (ql <= l and r <= qr) {
lazy[idx] += val;
if (l != r) propagate(idx);
return;
}
int m = l + (r - l) / 2;
radd(ql, qr, val, l, m, left(idx));
radd(ql, qr, val, m+1, r, right(idx));
}
void radd(int ql, int qr, int val, int n) {
radd(ql, qr, val, 0, n-1, 0);
}
int pquery(int pos, int l, int r, int idx) {
if (l == r) {
seg[idx] += lazy[idx];
lazy[idx] = 0;
return seg[idx];
}
propagate(idx);
int m = l + (r - l) / 2;
if (pos <= m) return pquery(pos, l, m, left(idx));
return pquery(pos, m+1, r, right(idx));
}
int pquery(int pos, int n) {
return pquery(pos, 0, n-1, 0);
}
void add(int pos, int val, int l, int r, int idx) {
if (l == r) {
seg[idx] += val;
return;
}
int m = l + (r - l) / 2;
if (pos <= m) add(pos, val, l, m, left(idx));
else add(pos, val, m+1, r, right(idx));
seg[idx] += val;
}
void add(int pos, int val, int n) {
add(pos, val, 0, n-1, 0);
}
int query(int ql, int qr, int l, int r, int idx) {
if (ql > r or qr < l or l > r) return 0;
if (ql <= l and r <= qr) return seg[idx];
if (l == r) return seg[l];
int m = l + (r - l) / 2;
return query(ql, qr, l, m, left(idx)) + query(ql, qr, m+1, r, right(idx));
}
int query(int ql, int qr, int n) {
return query(ql, qr, 0, n-1, 0);
}