Advertisement
Guest User

Dynamic CHT

a guest
Jun 20th, 2021
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. struct Line {
  2.   LL m, b;
  3.   mutable function<const Line*()> succ;
  4.   bool operator<(const Line& rhs) const {
  5.     if (rhs.b != is_query) return m > rhs.m;
  6.     const Line* s = succ();
  7.     if (!s) return 0;
  8.     return s->b - b < (m - s->m) * rhs.m;
  9.   }
  10. };
  11. struct HullDynamic : public multiset<Line> {
  12.   bool bad(iterator y) {  // maintains lower hull for min
  13.     auto z = next(y);
  14.     if (y == begin()) {
  15.       if (z == end()) return 0;
  16.       return y->m == z->m && y->b >= z->b;
  17.     }
  18.     auto x = prev(y);
  19.     if (z == end()) return y->m == x->m && y->b >= x->b;
  20.     return (x->b - y->b) * (z->m - y->m) >= (y->b - z->b) * (y->m - x->m);
  21.   }
  22.   void insert_line(LL m, LL b) {
  23.     auto y = insert({m, b});
  24.     y->succ = [=] { return next(y) == end() ? 0 : &*next(y); };
  25.     if (bad(y)) {
  26.       erase(y);
  27.       return;
  28.     }
  29.     while (next(y) != end() && bad(next(y))) erase(next(y));
  30.     while (y != begin() && bad(prev(y))) erase(prev(y));
  31.   }
  32.   LL eval(LL x) {
  33.     auto l = *lower_bound((Line){x, is_query});
  34.     return l.m * x + l.b;
  35.   }
  36. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement