Advertisement
anoosykh95

Untitled

Jul 22nd, 2016
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. // minimum static convex hull
  2. typedef long long ll;
  3. class Line{
  4.     public:
  5.     ll m , c;
  6.     Line(){}
  7.     Line(ll M , ll C) : m(M) , c(C){}
  8.     ll query(ll x){
  9.         return m*x+c;
  10.     }
  11. };
  12.  
  13. class Convex_Hull{
  14.     public:
  15.     deque < Line > S;
  16.     int sz;
  17.     void init(){
  18.         S.clear();
  19.         sz=0;
  20.     }
  21.     bool bad(Line L1 , Line L2 , Line L3){
  22.         //(c3 - c1)/(m1 - m3) intersection with first;
  23.         //(c3 - c2)/(m2 - m3) intersection with second
  24.         ll coef1 = (L3.c - L2.c);
  25.         coef1*= (L1.m - L3.m);
  26.         ll coef2=(L3.c - L1.c);
  27.         coef2*=(L2.m - L3.m);
  28.         return coef1<=coef2;
  29.     }
  30.     void addline(Line L){
  31.         while(sz > 1 && bad(S[sz-2] , S[sz-1] , L) ) {
  32.             S.pop_back();
  33.             sz--;
  34.         }
  35.         S.push_back(L);
  36.         sz++;
  37.     }
  38.     ll query(ll x){
  39.         while(sz > 1 && S[0].query(x) >= S[1].query(x) ){
  40.             sz--;
  41.             S.pop_front();
  42.         }
  43.         return S[0].query(x);
  44.     }
  45. };
  46.  
  47.  
  48. //////////////////////////////////////////////////////////////////////////////
  49.  
  50. // maximum divine convex hull
  51.  
  52. const LL is_query = -(1LL<<62);
  53. struct Line {
  54.     LL m, b;
  55.     mutable function<const Line*()> succ;
  56.     bool operator<(const Line& rhs) const {
  57.         if (rhs.b != is_query) return m < rhs.m;
  58.         const Line* s = succ();
  59.         if (!s) return 0;
  60.         LL x = rhs.m;
  61.         return b - s->b < (s->m - m) * x;
  62.     }
  63. };
  64. struct HullDynamic : public multiset<Line> { // will maintain upper hull for maximum
  65.     bool bad(iterator y) {
  66.         auto z = next(y);
  67.         if (y == begin()) {
  68.             if (z == end()) return 0;
  69.             return y->m == z->m && y->b <= z->b;
  70.         }
  71.         auto x = prev(y);
  72.         if (z == end()) return y->m == x->m && y->b <= x->b;
  73.         return (x->b - y->b)*(z->m - y->m) >= (y->b - z->b)*(y->m - x->m);
  74.     }
  75.     void insert_line(LL m, LL b) {
  76.         auto y = insert({ m, b });
  77.         y->succ = [=] { return next(y) == end() ? 0 : &*next(y); };
  78.         if (bad(y)) { erase(y); return; }
  79.         while (next(y) != end() && bad(next(y))) erase(next(y));
  80.         while (y != begin() && bad(prev(y))) erase(prev(y));
  81.     }
  82.     LL eval(LL x) {
  83.         auto l = *lower_bound((Line) { x, is_query });
  84.         return l.m * x + l.b;
  85.     }
  86. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement