-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLi Chao Tree.cpp
More file actions
51 lines (46 loc) · 914 Bytes
/
Li Chao Tree.cpp
File metadata and controls
51 lines (46 loc) · 914 Bytes
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
/// MAX
struct line {
int m, b;
int eval(int x) {
return m * x + b;
}
};
struct ST {
vector<line> t;
ST(int n) {
int dim = 1;
while (dim < n) {
dim <<= 1;
}
t.resize(dim << 1);
}
void addLine(int x, int lx, int rx, line l) {
if (lx == rx) {
if (t[x].eval(lx) < l.eval(lx)) {
t[x] = l;
}
return;
}
int mid = (lx + rx) >> 1;
if (l.m < t[x].m) {
swap(t[x], l);
}
if (t[x].eval(mid) < l.eval(mid)) {
swap(t[x], l);
addLine(x << 1, lx, mid, l);
} else {
addLine(x << 1 | 1, mid + 1, rx, l);
}
}
int query(int x, int lx, int rx, int pos) {
int val = t[x].eval(pos);
if (lx == rx) {
return val;
}
int mid = (lx + rx) >> 1;
if (pos <= mid) {
return max(val, query(x << 1, lx, mid, pos));
}
return max(val, query(x << 1 | 1, mid + 1, rx, pos));
}
};