This documentation is automatically generated by online-judge-tools/verification-helper
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/line_add_get_min
#include "../../template/template.hpp"
#include "../../structure/convex-hull-trick/convex-hull-trick-add-monotone.hpp"
int main() {
int N, Q;
cin >> N >> Q;
using CHT = ConvexHullTrickAddMonotone< int64, true >;
vector< CHT > cht;
auto add = [&](int64 a, int64 b) {
cht.emplace_back();
cht.back().add(a, b);
while(cht.size() >= 2 and cht.back().H.size() >= cht[cht.size() - 2].H.size()) {
auto X = cht.back().H;
cht.pop_back();
auto Y = cht.back().H;
cht.pop_back();
reverse(begin(X), end(X));
reverse(begin(Y), end(Y));
CHT c;
int k = 0;
for(auto &p: X) {
while(k < (int) Y.size() and Y[k] < p) {
c.add(Y[k].first, Y[k].second);
++k;
}
c.add(p.first, p.second);
}
while(k < (int) Y.size()) {
c.add(Y[k].first, Y[k].second);
++k;
}
cht.emplace_back(c);
}
};
for(int i = 0; i < N; i++) {
int64 a, b;
cin >> a >> b;
add(a, b);
}
while(Q--) {
int t;
cin >> t;
if(t == 0) {
int64 a, b;
cin >> a >> b;
add(a, b);
} else {
int64 x;
cin >> x;
int64 ret = infll;
for(auto &c: cht) {
chmin(ret, c.query(x));
}
cout << ret << "\n";
}
}
}
#line 1 "test/verify/yosupo-line-add-get-min-2.test.cpp"
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/line_add_get_min
#line 1 "template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); i++) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v) is >> in;
return is;
}
template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
return a > b && (a = b, true);
}
template <typename T = int64>
vector<T> make_v(size_t a) {
return vector<T>(a);
}
template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
for (auto &e : t) fill_v(e, v);
}
template <typename F>
struct FixPoint : F {
explicit FixPoint(F &&f) : F(forward<F>(f)) {}
template <typename... Args>
decltype(auto) operator()(Args &&...args) const {
return F::operator()(*this, forward<Args>(args)...);
}
};
template <typename F>
inline decltype(auto) MFP(F &&f) {
return FixPoint<F>{forward<F>(f)};
}
#line 4 "test/verify/yosupo-line-add-get-min-2.test.cpp"
#line 1 "structure/convex-hull-trick/convex-hull-trick-add-monotone.hpp"
/**
* @brief Convex Hull Trick Add Monotone
*
*/
template <typename T, bool isMin>
struct ConvexHullTrickAddMonotone {
#define F first
#define S second
using P = pair<T, T>;
deque<P> H;
ConvexHullTrickAddMonotone() = default;
bool empty() const { return H.empty(); }
void clear() { H.clear(); }
inline int sgn(T x) { return x == 0 ? 0 : (x < 0 ? -1 : 1); }
inline bool check(const P &a, const P &b, const P &c) {
if (b.S == a.S || c.S == b.S)
return sgn(b.F - a.F) * sgn(c.S - b.S) >= sgn(c.F - b.F) * sgn(b.S - a.S);
// return (b.F-a.F)*(c.S-b.S) >= (b.S-a.S)*(c.F-b.F);
if (is_integral<T>::value) {
return (b.S - a.S) / (a.F - b.F) >= (c.S - b.S) / (b.F - c.F);
} else {
return (b.F - a.F) * sgn(c.S - b.S) / abs(b.S - a.S) >=
(c.F - b.F) * sgn(b.S - a.S) / abs(c.S - b.S);
}
}
void add(T a, T b) {
if (!isMin) a *= -1, b *= -1;
P line(a, b);
if (empty()) {
H.emplace_front(line);
return;
}
if (H.front().F <= a) {
if (H.front().F == a) {
if (H.front().S <= b) return;
H.pop_front();
}
while (H.size() >= 2 && check(line, H.front(), H[1])) H.pop_front();
H.emplace_front(line);
} else {
assert(a <= H.back().F);
if (H.back().F == a) {
if (H.back().S <= b) return;
H.pop_back();
}
while (H.size() >= 2 && check(H[H.size() - 2], H.back(), line))
H.pop_back();
H.emplace_back(line);
}
}
inline T get_y(const P &a, const T &x) { return a.F * x + a.S; }
T query(T x) {
assert(!empty());
int l = -1, r = H.size() - 1;
while (l + 1 < r) {
int m = (l + r) >> 1;
if (get_y(H[m], x) >= get_y(H[m + 1], x))
l = m;
else
r = m;
}
if (isMin) return get_y(H[r], x);
return -get_y(H[r], x);
}
T query_monotone_inc(T x) {
assert(!empty());
while (H.size() >= 2 && get_y(H.front(), x) >= get_y(H[1], x))
H.pop_front();
if (isMin) return get_y(H.front(), x);
return -get_y(H.front(), x);
}
T query_monotone_dec(T x) {
assert(!empty());
while (H.size() >= 2 && get_y(H.back(), x) >= get_y(H[H.size() - 2], x))
H.pop_back();
if (isMin) return get_y(H.back(), x);
return -get_y(H.back(), x);
}
#undef F
#undef S
};
#line 6 "test/verify/yosupo-line-add-get-min-2.test.cpp"
int main() {
int N, Q;
cin >> N >> Q;
using CHT = ConvexHullTrickAddMonotone< int64, true >;
vector< CHT > cht;
auto add = [&](int64 a, int64 b) {
cht.emplace_back();
cht.back().add(a, b);
while(cht.size() >= 2 and cht.back().H.size() >= cht[cht.size() - 2].H.size()) {
auto X = cht.back().H;
cht.pop_back();
auto Y = cht.back().H;
cht.pop_back();
reverse(begin(X), end(X));
reverse(begin(Y), end(Y));
CHT c;
int k = 0;
for(auto &p: X) {
while(k < (int) Y.size() and Y[k] < p) {
c.add(Y[k].first, Y[k].second);
++k;
}
c.add(p.first, p.second);
}
while(k < (int) Y.size()) {
c.add(Y[k].first, Y[k].second);
++k;
}
cht.emplace_back(c);
}
};
for(int i = 0; i < N; i++) {
int64 a, b;
cin >> a >> b;
add(a, b);
}
while(Q--) {
int t;
cin >> t;
if(t == 0) {
int64 a, b;
cin >> a >> b;
add(a, b);
} else {
int64 x;
cin >> x;
int64 ret = infll;
for(auto &c: cht) {
chmin(ret, c.query(x));
}
cout << ret << "\n";
}
}
}
Env | Name | Status | Elapsed | Memory |
---|---|---|---|---|
g++ | example_00 | AC | 6 ms | 4 MB |
g++ | half_00 | AC | 94 ms | 4 MB |
g++ | hand_max_00 | AC | 322 ms | 10 MB |
g++ | max_random_00 | AC | 184 ms | 4 MB |
g++ | max_random_01 | AC | 179 ms | 4 MB |
g++ | max_random_02 | AC | 178 ms | 4 MB |
g++ | parabola_random_00 | AC | 382 ms | 10 MB |
g++ | parabola_random_01 | AC | 379 ms | 11 MB |
g++ | parabola_random_02 | AC | 364 ms | 11 MB |
g++ | random_00 | AC | 129 ms | 4 MB |
g++ | random_01 | AC | 134 ms | 4 MB |
g++ | random_02 | AC | 83 ms | 4 MB |
g++ | small_00 | AC | 7 ms | 4 MB |
g++ | small_01 | AC | 6 ms | 4 MB |
clang++ | example_00 | AC | 6 ms | 4 MB |
clang++ | half_00 | AC | 97 ms | 4 MB |
clang++ | hand_max_00 | AC | 329 ms | 10 MB |
clang++ | max_random_00 | AC | 183 ms | 4 MB |
clang++ | max_random_01 | AC | 181 ms | 4 MB |
clang++ | max_random_02 | AC | 180 ms | 4 MB |
clang++ | parabola_random_00 | AC | 387 ms | 10 MB |
clang++ | parabola_random_01 | AC | 386 ms | 11 MB |
clang++ | parabola_random_02 | AC | 373 ms | 11 MB |
clang++ | random_00 | AC | 131 ms | 4 MB |
clang++ | random_01 | AC | 139 ms | 4 MB |
clang++ | random_02 | AC | 82 ms | 4 MB |
clang++ | small_00 | AC | 7 ms | 4 MB |
clang++ | small_01 | AC | 6 ms | 4 MB |