-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlichao.cpp
47 lines (41 loc) · 893 Bytes
/
lichao.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
struct line {
ll a, b;
} tree[N << 3];
struct lichao {
lichao() {
for (int i = 1; i < N << 3; i++) {
tree[i].a = LLONG_MAX;
}
}
ll y(line l, ll x) {
if (l.a == LLONG_MAX) {
return LLONG_MAX;
}
return l.a * x + l.b;
}
void update(line x, int id = 1, int l = -1e5, int r = N) {
int mid = (l + r) >> 1;
bool i1 = y(x, l) < y(tree[id], l);
bool i2 = y(x, mid) < y(tree[id], mid);
if (i2) {
swap(tree[id], x);
}
if (r - l == 1) {
return;
} else if (i1 != i2) {
update(x, id << 1, l, mid);
} else {
update(x, (id << 1) | 1, mid, r);
}
}
ll query(int x, int id = 1, int l = -1e5, int r = N) {
int mid = (l + r) >> 1;
if (r - l == 1) {
return y(tree[id], x);
} else if (x < mid) {
return min(y(tree[id], x), query(x, id << 1, l, mid));
} else {
return min(y(tree[id], x), query(x, (id << 1) | 1, mid, r));
}
}
} s;