Advertisement
RaFiN_

Dynamic Convex Hull Trick

May 29th, 2020
1,699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. // Short code and faster
  2. // Keeps upper hull for maximums.
  3. // add lines with -m and -b and return -ans to
  4. // make this code working for minimums.
  5. // source: http://codeforces.com/blog/entry/11155?#comment-162462
  6.  
  7. const ll is_query = -(1LL<<62);
  8. struct Line {
  9.     ll m, b;
  10.     mutable function<const Line*()> succ;
  11.     bool operator<(const Line& rhs) const {
  12.         if (rhs.b != is_query) return m < rhs.m;
  13.         const Line* s = succ();
  14.         if (!s) return 0;
  15.         ll x = rhs.m;
  16.         return b - s->b < (s->m - m) * x;
  17.     }
  18. };
  19. struct HullDynamic : public multiset<Line> {
  20.     bool bad(iterator y) {
  21.         auto z = next(y);
  22.         if (y == begin()) {
  23.             if (z == end()) return 0;
  24.             return y->m == z->m && y->b <= z->b;
  25.         }
  26.         auto x = prev(y);
  27.         if (z == end()) return y->m == x->m && y->b <= x->b;
  28.         return 1.0 * (x->b - y->b)*(z->m - y->m) >= 1.0 * (y->b - z->b)*(y->m - x->m);
  29.     }
  30.     void insert_line(ll m, ll b) {
  31.         auto y = insert({ m, b });
  32.         y->succ = [=] { return next(y) == end() ? 0 : &*next(y); };
  33.         if (bad(y)) { erase(y); return; }
  34.         while (next(y) != end() && bad(next(y))) erase(next(y));
  35.         while (y != begin() && bad(prev(y))) erase(prev(y));
  36.     }
  37.     ll eval(ll x) {
  38.         auto l = *lower_bound((Line) { x, is_query });
  39.         return l.m * x + l.b;
  40.     }
  41. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement