-
Notifications
You must be signed in to change notification settings - Fork 9
/
convex_hull_trick.cpp
69 lines (54 loc) · 1.47 KB
/
convex_hull_trick.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
// convex_hull_trick.cpp
// Eric K. Zhang; Nov. 22, 2017
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
#define MAXN 100013
int N, Q;
pii ar[MAXN];
vector<pii> envelope;
vector<double> hull;
double intersect(int m1, int b1, int m2, int b2) {
return (b2 - b1) / ((double) m1 - m2);
}
double intersect(pii y1, pii y2) {
return intersect(y1.first, y1.second, y2.first, y2.second);
}
int main() {
// make cin, cout faster
ios_base::sync_with_stdio(false);
cin.tie(0);
freopen("cht.in", "r", stdin);
cin >> N >> Q;
for (int i = 0; i < N; i++) {
cin >> ar[i].first >> ar[i].second;
}
// Ok, time for the CHT algorithm! We'll find the lower envelope
sort(ar, ar + N, [](pii y1, pii y2) {
return y1.first != y2.first ? y1.first > y2.first : y1.second < y2.second;
});
for (int i = 0; i < N; i++) {
if (i != 0 && ar[i].first == ar[i - 1].first)
continue; // repeated slope <-> parallel lines
int sz;
while ((sz = envelope.size()) >= 2) {
if (intersect(envelope[sz - 2], ar[i]) <
intersect(envelope[sz - 2], envelope[sz - 1])) {
envelope.pop_back();
}
else break;
}
envelope.push_back(ar[i]);
}
for (int i = 0; i < envelope.size() - 1; i++) {
hull.push_back(intersect(envelope[i], envelope[i + 1]));
}
for (int i = 0; i < Q; i++) {
double x;
cin >> x;
int idx = lower_bound(begin(hull), end(hull), x) - begin(hull);
cout << envelope[idx].first * x + envelope[idx].second << '\n';
}
cout.flush();
return 0;
}