EntropyIncreaser

Splay Convex

Jun 13th, 2018
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3.  
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. typedef double Num;
  9. typedef double BNum;
  10.  
  11. struct Vec {
  12.     Num x, y;
  13.  
  14.     Vec() : x(), y() {}
  15.  
  16.     Vec(Num x, Num y) : x(x), y(y) {}
  17.  
  18.     bool operator<(const Vec &rhs) const {
  19.         return x < rhs.x;
  20.     }
  21.  
  22.     Vec operator+(const Vec& rhs) const { return Vec(x + rhs.x, y + rhs.y); }
  23.     Vec operator-(const Vec& rhs) const { return Vec(x - rhs.x, y - rhs.y); }
  24.  
  25.     BNum operator*(const Vec& rhs) const { return x * (BNum) rhs.y - y * (BNum) rhs.x; }
  26.     BNum operator^(const Vec& rhs) const { return x * (BNum) rhs.x + y * (BNum) rhs.y; }
  27. };
  28.  
  29. struct Node {
  30.     Vec v, prv, nxt;
  31.     union {
  32.         struct {
  33.             Node *ls, *rs;
  34.         };
  35.         Node* ch[2];
  36.     };
  37.     Node* prt;
  38.  
  39.     inline bool rel() const {
  40.         return this == prt->rs;
  41.     }
  42.  
  43.     inline void rotate() {
  44.         Node* p = prt;
  45.         bool f = rel();
  46.         prt = p->prt;
  47.         if (prt)
  48.             prt->ch[p->rel()] = this;
  49.         p->ch[f] = ch[!f];
  50.         if (ch[!f])
  51.             ch[!f]->prt = p;
  52.         ch[!f] = p;
  53.         p->prt = this;
  54.     }
  55.  
  56.     inline void splay(Node* goal) {
  57.         while (prt != goal) {
  58.             if (prt->prt == goal)
  59.                 return rotate();
  60.             if (rel() == prt->rel()) {
  61.                 prt->rotate();
  62.                 rotate();
  63.             } else {
  64.                 rotate();
  65.                 rotate();
  66.             }
  67.         }
  68.     }
  69. };
  70.  
  71. struct Convex {
  72.     Node* t;
  73.  
  74.     void ins(const Vec& v);
  75.  
  76.     BNum bound(const Vec& v) {
  77.         Vec x = Vec(v.y, -v.x);
  78.         while (true) {
  79.             if (t->prv * x > 0)
  80.                 t = t->ls;
  81.             else if (t->nxt * x < 0)
  82.                 t = t->rs;
  83.             else
  84.                 break;
  85.         }
  86.         t->splay(NULL);
  87.         return t->v ^ v;
  88.     }
  89. };
  90.  
  91. const int N;
  92.  
  93. Node* create();
  94.  
  95. Node* create() {
  96.     static Node pool[N];
  97.     static Node* p = pool;
  98.     return p++;
  99. }
  100.  
  101. void Convex::ins(const Vec &v) {
  102.     if (t == NULL) {
  103.         t = create();
  104.         t->v = v;
  105.         return;
  106.     }
  107.     Node *p = t, *tmp = t;
  108.     t = NULL;
  109.     while (p) {
  110.         if (p->v.x < v.x) {
  111.             t = p;
  112.             p = p->rs;
  113.         } else
  114.             p = p->ls;
  115.     }
  116.     if (t) {
  117.         t->splay(NULL);
  118.         if (!t->rs) {
  119.             p = create();
  120.             p->v = v;
  121.             p->ls = t;
  122.             t->prt = p;
  123.             t = p;
  124.         } else {
  125.             p = t->rs;
  126.             while (p->ls)
  127.                 p = p->ls;
  128.             p->splay(t);
  129.             if ((v - t->v) * (p->v - v) >= 0)
  130.                 return;
  131.             p = create();
  132.             p->v = v;
  133.             p->prt = t;
  134.             p->rs = t->rs;
  135.             t->rs = p;
  136.             p->rs->prt = p;
  137.             t = p;
  138.             t->splay(NULL);
  139.         }
  140.     } else {
  141.         t = tmp;
  142.         p = create();
  143.         p->v = v;
  144.         p->rs = t;
  145.         t->prt = p;
  146.         t = p;
  147.     }
  148.     Node* q;
  149.     if (t->ls) {
  150.         p = t->ls;
  151.         while (p->ls)
  152.             p = p->ls;
  153.         p->splay(t);
  154.         q = p->rs;
  155.         while (q) {
  156.             if (q->prv * (v - q->v) < 0) {
  157.                 p = q;
  158.                 q = q->rs;
  159.             } else
  160.                 q = q->ls;
  161.         }
  162.         p->splay(t);
  163.         p->rs = NULL;
  164.         p->nxt = t->prv = v - p->v;
  165.     }
  166.     if (t->rs) {
  167.         p = t->rs;
  168.         while (p->rs)
  169.             p = p->rs;
  170.         p->splay(t);
  171.         q = p->ls;
  172.         while (q) {
  173.             if ((q->v - v) * q->nxt < 0) {
  174.                 p = q;
  175.                 q = q->ls;
  176.             } else
  177.                 q = q->rs;
  178.         }
  179.         p->splay(t);
  180.         p->ls = NULL;
  181.         t->nxt = p->prv = p->v - v;
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment