forked from zimpha/algorithmic-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicConvexHull.cc
More file actions
39 lines (38 loc) · 1.15 KB
/
DynamicConvexHull.cc
File metadata and controls
39 lines (38 loc) · 1.15 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
#include <set>
#include <functional>
typedef long long LL;
const LL is_query = -(1LL<<62), inf = 1ll << 62;
struct Line {// y = m * x + b
LL m, b;
mutable std::function<const Line*()> succ;
bool operator<(const Line& rhs) const {
if (rhs.b != is_query) return m < rhs.m;
const Line* s = succ();
if (!s) return 0;
return b - s->b < (s->m - m) * rhs.m;
}
};// wiLL maintain lower huLL for maximum
struct HuLLDynamic: public std::multiset<Line> {
bool bad(iterator y) {
auto z = next(y);
if (y == begin()) {
if (z == end()) return 0;
return y->m == z->m && y->b <= z->b;
}
auto x = prev(y);
if (z == end()) return y->m == x->m && y->b <= x->b;
return (x->b - y->b)*(z->m - y->m) >= (y->b - z->b)*(y->m - x->m);
}
void ins(LL m, LL b) {
auto y = insert({m, b});
y->succ = [=] {return next(y) == end()?0:&*next(y);};
if (bad(y)) {erase(y); return;}
while (next(y) != end() && bad(next(y))) erase(next(y));
while (y != begin() && bad(prev(y))) erase(prev(y));
}
LL eval(LL x) {
auto l = lower_bound((Line){x,is_query});
if (l == end()) return -inf;
return l->m * x + l->b;
}
};