-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLineContainer.cpp
More file actions
35 lines (30 loc) · 1.04 KB
/
LineContainer.cpp
File metadata and controls
35 lines (30 loc) · 1.04 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
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct Line {
mutable ll k, m, p;
bool operator<(const Line& o) const { return k < o.k; }
bool operator<(ll x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<>> {
static const ll inf = LLONG_MAX; // Double: inf = 1/.0, div(a,b) = a/b
ll div(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } //floored division
bool isect(iterator x, iterator y) {
if(y == end()) return x->p = inf, 0;
if(x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add_line(ll k, ll m){ // kx + m //if minimum k*=-1, m*=-1, query*-1
auto z = insert({k, m, 0}), y = z++, x = y;
while(isect(y, z)) z = erase(z);
if(x != begin() && isect(--x, y)) isect(x, y = erase(y));
while((y = x) != begin() && (--x)->p >= y->p) isect(x, erase(y));
}
ll query(ll x) {
assert(!empty());
auto l = *lower_bound(x);
return l.k * x + l.m;
}
};
/* Credits: kactl (https://github.com/kth-competitive-programming) */