-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment_tree.cpp
46 lines (41 loc) · 1.08 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
// segment tree for scanning line
int num[maxn];
struct seg_tree
{
int l[5 * maxn], r[5 * maxn];
int c[5 * maxn];
int ans[5 * maxn];
void build(int pos, int le, int ri)
{
l[pos] = le; r[pos] = ri;
c[pos] = 0;
ans[pos] = num[ri] - num[le];
if(ri - le > 1)
{
int mid = (le + ri) / 2;
build(ls(pos), le, mid);
build(rs(pos), mid, ri);
}
}
void update(int pos)
{
if(c[pos] > 0) ans[pos] = 0;
else if(r[pos] - l[pos] == 1) ans[pos] = num[r[pos]] - num[l[pos]];
else ans[pos] = ans[ls(pos)] + ans[rs(pos)];
}
void add(int pos, int le, int ri, int flag)
{
if(le <= l[pos] && ri >= r[pos])
{
if(flag) c[pos]++;
else c[pos]--;
}
else
{
int mid = (l[pos] + r[pos]) / 2;
if(ri > mid) add(rs(pos), le, ri, flag);
if(le < mid) add(ls(pos), le, ri, flag);
}
update(pos);
}
}tree;