-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegment Tree Template.cpp
More file actions
124 lines (91 loc) · 2.38 KB
/
Segment Tree Template.cpp
File metadata and controls
124 lines (91 loc) · 2.38 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const int NIL = 0; /// !!!
struct node {
int val, lazy;
friend node operator + (const node &A, const node &B) { /// !!!
node ans;
ans.val = A.val + B.val;
ans.lazy = NIL;
return ans;
}
};
const node empty_node = {0, NIL}; /// !!!
struct SegTree {
int n;
vector<node> t;
void init(int N) {
n = N;
int Size = 1;
while (Size < n) {
Size <<= 1;
}
t.resize(Size << 1, empty_node);
}
void update_node(int x, int len) { /// !!!
t[x].val = len - t[x].val;
t[x].lazy ^= 1;
}
void push(int x, int lx, int rx) {
if (t[x].lazy == NIL) {
return;
}
int mid = (lx + rx) >> 1;
int len[] = {mid - lx + 1, rx - mid};
for (int i = 0; i <= 1; ++i) {
update_node(x << 1 | i, len[i]);
}
t[x].lazy = NIL;
}
void update_pos(int x, int lx, int rx, int pos) {
if (lx == rx) {
update_node(x, rx - lx + 1);
return;
}
push(x, lx, rx);
int mid = (lx + rx) >> 1, lSon = x << 1, rSon = x << 1 | 1;
if (pos <= mid) {
update_pos(lSon, lx, mid, pos);
} else {
update_pos(rSon, mid + 1, rx, pos);
}
t[x] = t[lSon] + t[rSon];
}
void update_pos(int pos) {
update_pos(1, 1, n, pos);
}
void update_interval(int x, int lx, int rx, int st, int dr) {
if (st <= lx && rx <= dr) {
update_node(x, rx - lx + 1);
return;
}
push(x, lx, rx);
int mid = (lx + rx) >> 1, lSon = x << 1, rSon = x << 1 | 1;
if (st <= mid) {
update_interval(lSon, lx, mid, st, dr);
}
if (mid + 1 <= dr) {
update_interval(rSon, mid + 1, rx, st, dr);
}
t[x] = t[lSon] + t[rSon];
}
void update_interval(int st, int dr) {
update_interval(1, 1, n, st, dr);
}
node query(int x, int lx, int rx, int st, int dr) {
if (st <= lx && rx <= dr) {
return t[x];
}
push(x, lx, rx);
int mid = (lx + rx) >> 1;
node ans1 = empty_node, ans2 = empty_node;
if (st <= mid) {
ans1 = query(x << 1, lx, mid, st, dr);
}
if (mid + 1 <= dr) {
ans2 = query(x << 1 | 1, mid + 1, rx, st, dr);
}
return ans1 + ans2;
}
node query(int st, int dr) {
return query(1, 1, n, st, dr);
}
};