|
| 1 | +#pragma once |
| 2 | +#include <cassert> |
| 3 | +#include <list> |
| 4 | +#include <utility> |
| 5 | + |
| 6 | +// CUT begin |
| 7 | +// Convex Hull Trick for monotone increasing queries, monotone decreasing slopes |
| 8 | +// Each operation is amortized O(1) |
| 9 | +// - is_minimizer: if true, calculates min. Otherwise, calculates max. |
| 10 | +// - insert_line(a, b): Insert `y = ax + b`, |
| 11 | +// a must be monotone decreasing (if is_minimizer == true) / increasing (otherwise) |
| 12 | +// - add_convex_parabola(c, a, b): Add `y = c(x - a)^2 + b`, c is constant, a is monotone |
| 13 | +// increasing (if is_minimizer == true) / decreasing (otherwise) |
| 14 | +// - get(x): Calculate min/max. value of `y = ax + b`'s at point x, x must be monotone |
| 15 | +// increasing FOR BOTH CASES. |
| 16 | +// - parabola_get(c, x): Caclculate min/max. value of `y = c(x - a)^2 + b`'s, x must be monotone |
| 17 | +// increasing FOR BOTH CASES. |
| 18 | +// - If you need random access, change `std::list` to `std::deque` |
| 19 | +// Verified: https://yukicoder.me/submissions/409156 |
| 20 | +template <bool is_minimizer, class T = long long, class T_MP = __int128, T INF = 1LL << 61> |
| 21 | +class MonotoneConvexHullTrick : std::list<std::pair<T, T>> { |
| 22 | + // (a, b) means `y = ax + b` |
| 23 | + T_MP _eval(typename std::list<std::pair<T, T>>::const_iterator itr, T x) { |
| 24 | + return T_MP(itr->first) * x + itr->second; |
| 25 | + } |
| 26 | + |
| 27 | +public: |
| 28 | + MonotoneConvexHullTrick() { static_assert(INF > 0, "INF must be positive."); } |
| 29 | + void insert_line(T a, T b) { // Add y = ax + b |
| 30 | + if (!is_minimizer) a = -a, b = -b; |
| 31 | + assert(this->empty() or this->back().first >= a); |
| 32 | + while (this->size() > 1u) { |
| 33 | + if (this->back().first == a) { |
| 34 | + if (this->back().second <= b) return; |
| 35 | + this->pop_back(); |
| 36 | + continue; |
| 37 | + } |
| 38 | + auto ill = std::prev(this->end(), 2); |
| 39 | + auto l = (T_MP)(this->back().second - ill->second) * (this->back().first - a); |
| 40 | + auto r = (T_MP)(b - this->back().second) * (ill->first - this->back().first); |
| 41 | + if (l < r) break; |
| 42 | + this->pop_back(); |
| 43 | + } |
| 44 | + this->emplace_back(a, b); |
| 45 | + } |
| 46 | + |
| 47 | + struct Ret { |
| 48 | + T line_a, line_b; |
| 49 | + bool is_valid; |
| 50 | + T_MP val; |
| 51 | + }; |
| 52 | + Ret get(T x) { |
| 53 | + if (this->empty()) return {0, 0, false, is_minimizer ? INF : -INF}; |
| 54 | + while (this->size() > 1 and _eval(this->begin(), x) >= _eval(std::next(this->begin()), x)) { |
| 55 | + this->pop_front(); |
| 56 | + } |
| 57 | + T_MP val = _eval(this->begin(), x) * (is_minimizer ? 1 : -1); |
| 58 | + return {(is_minimizer ? 1 : -1) * this->begin()->first, |
| 59 | + (is_minimizer ? 1 : -1) * this->begin()->second, true, val}; |
| 60 | + } |
| 61 | + void insert_convex_parabola(T c, T a, T b) { insert_line(c * a * (-2), c * a * a + b); } |
| 62 | + T_MP parabola_get(T c, T x) { return get(x).val + c * x * x; } |
| 63 | + |
| 64 | + static MonotoneConvexHullTrick |
| 65 | + merge(const MonotoneConvexHullTrick &cht1, const MonotoneConvexHullTrick &cht2) { |
| 66 | + MonotoneConvexHullTrick ret; |
| 67 | + auto i1 = cht1.begin(), i2 = cht2.begin(); |
| 68 | + static const T sgn = is_minimizer ? 1 : -1; |
| 69 | + T a = 0, b = 0; |
| 70 | + while (i1 != cht1.end() and i2 != cht2.end()) { |
| 71 | + if (i1->first == i2->first) { |
| 72 | + a = i1->first, b = std::min(i1->second, i2->second); |
| 73 | + ++i1, ++i2; |
| 74 | + } else if (i1->first > i2->first) { |
| 75 | + a = i1->first, b = i1->second, ++i1; |
| 76 | + } else { |
| 77 | + a = i2->first, b = i2->second, ++i2; |
| 78 | + } |
| 79 | + ret.insert_line(a * sgn, b * sgn); |
| 80 | + } |
| 81 | + while (i1 != cht1.end()) ret.insert_line(i1->first * sgn, i1->second * sgn), ++i1; |
| 82 | + while (i2 != cht2.end()) ret.insert_line(i2->first * sgn, i2->second * sgn), ++i2; |
| 83 | + return ret; |
| 84 | + } |
| 85 | +}; |
0 commit comments