EntropyIncreaser

treap

Nov 17th, 2017
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.87 KB | None | 0 0
  1. #include <ctime>
  2. #include <cstdio>
  3. #include <cstdlib>
  4.  
  5. #include <algorithm>
  6.  
  7. #define GET_CHILD(P, SIDE) ((SIDE) ? (P)->r : (P)->l)
  8.  
  9. // #define NO_ROTATION
  10. // #define DEBUG_MODE
  11.  
  12. using namespace std;
  13.  
  14. struct node {
  15.     int d, rnd, sz, cnt;
  16.     node *l, *r;
  17. };
  18.  
  19. const int N = 100010;
  20. int ans;
  21. node* root;
  22. node* insertion_needed;
  23.  
  24. int fast_rand();
  25. node* create(int x);
  26. void insert(node*& p, int x);
  27. void insert(node*& p, node* q);
  28. void update(node* p);
  29. void rotate(node*& p, bool f);
  30. void del(node*& p, int x);
  31. void pre(node* p, int x);
  32. void post(node* p, int x);
  33. int get_rank(node* p, int x);
  34. int get_x(node* p, int r);
  35. void debug(node* p);
  36. void print_tree(node* p);
  37.  
  38. int main() {
  39.     int n, o, x;
  40.     scanf("%d", &n);
  41.     while (n--) {
  42.         scanf("%d%d", &o, &x);
  43.         switch (o) {
  44.         case 1:
  45.             insert(root, x);
  46.             break;
  47.         case 2:
  48.             insertion_needed = NULL;
  49.             del(root, x);
  50.             if (insertion_needed)
  51.                 insert(root, insertion_needed);
  52.             break;
  53.         case 3:
  54.             printf("%d\n", get_rank(root, x));
  55.             break;
  56.         case 4:
  57.             printf("%d\n", get_x(root, x));
  58.             break;
  59.         case 5:
  60.             ans = -1e9;
  61.             pre(root, x);
  62.             printf("%d\n", ans);
  63.             break;
  64.         case 6:
  65.             ans = 1e9;
  66.             post(root, x);
  67.             printf("%d\n", ans);
  68.             break;
  69.         default: break;
  70.         }
  71. #ifdef DEBUG_MODE
  72.         debug(root);
  73. #endif
  74.     }
  75.     return 0;
  76. }
  77.  
  78. inline node* create(int x) {
  79.     static node pool[N];
  80.     static int cnt = -1;
  81.     node* ret = pool + ++cnt;
  82.     ret->d = x;
  83.     ret->rnd = fast_rand();
  84.     ret->sz = ret->cnt = 1;
  85.     return ret;
  86. }
  87.  
  88. inline int fast_rand() {
  89.     static int x = time(NULL) << 4, y = time(NULL) >> 2, z = time(NULL);
  90.     int t;
  91.     x ^= x << 16;
  92.     x ^= x >> 5;
  93.     x ^= x << 1;
  94.     t = x;
  95.     x = y;
  96.     y = z;
  97.     z = t ^ x ^ y;
  98.     return z;
  99. }
  100.  
  101. void insert(node*& p, int x) {
  102.     if (!p) {
  103.         p = create(x);
  104.         return;
  105.     }
  106.     ++p->cnt;
  107.     if (p->d == x) {
  108.         ++p->sz;
  109.         return;
  110.     }
  111.     if (p->d > x) {
  112.         insert(p->l, x);
  113.         if (p->l->rnd < p->rnd)
  114.             rotate(p, false);
  115.     } else {
  116.         insert(p->r, x);
  117.         if (p->r->rnd < p->rnd)
  118.             rotate(p, true);
  119.     }
  120.     update(p);
  121. }
  122.  
  123. void insert(node*& p, node* q) {
  124.     if (!p) {
  125.         p = q;
  126.         return;
  127.     }
  128.     ++p->cnt;
  129.     if (p->d > q->d) {
  130.         insert(p->l, q);
  131.         if (p->l->rnd < p->rnd)
  132.             rotate(p, false);
  133.     } else {
  134.         insert(p->r, q);
  135.         if (p->r->rnd < p->rnd)
  136.             rotate(p, true);
  137.     }
  138.     update(p);
  139. }
  140.  
  141. inline void update(node* p) {
  142.     p->cnt = p->sz;
  143.     if (p->l)
  144.         p->cnt += p->l->cnt;
  145.     if (p->r)
  146.         p->cnt += p->r->cnt;
  147. }
  148.  
  149. inline void rotate(node*& p, bool f) {
  150. #ifndef NO_ROTATION
  151.     node* son = GET_CHILD(p, f);
  152.     GET_CHILD(p, f) = GET_CHILD(son, !f);
  153.     GET_CHILD(son, !f) = p;
  154.     update(p);
  155.     p = son;
  156. #endif
  157.     update(p);
  158. }
  159.  
  160. void del(node*& p, int x) {
  161.     if (p->d == x) {
  162.         if (--p->sz)
  163.             --p->cnt;
  164.         else {
  165.             if (p->l) {
  166.                 insert(insertion_needed, p->l);
  167.                 p->l = NULL;
  168.             }
  169.             if (p->r) {
  170.                 insert(insertion_needed, p->r);
  171.                 p->r = NULL;
  172.             }
  173.             p = NULL;
  174.         }
  175.         return;
  176.     }
  177.     if (p->d > x)
  178.         del(p->l, x);
  179.     else
  180.         del(p->r, x);
  181.     update(p);
  182. }
  183.  
  184. int get_x(node* p, int r) {
  185.     if (p->l) {
  186.         if (p->l->cnt >= r)
  187.             return get_x(p->l, r);
  188.         r -= p->l->cnt;
  189.     }
  190.     if (r <= p->sz)
  191.         return p->d;
  192.     return get_x(p->r, r - p->sz);
  193. }
  194.  
  195. int get_rank(node* p, int x) {
  196.     int ret = 0;
  197.     if (p->d > x)
  198.         return get_rank(p->l, x);
  199.     if (p->l)
  200.         ret += p->l->cnt;
  201.     if (p->d == x)
  202.         return ret + 1;
  203.     return ret + p->sz + get_rank(p->r, x);
  204. }
  205.  
  206. inline void pre(node* p, int x) {
  207.     while (p) {
  208.         if (p->d < x) {
  209.             ans = max(p->d, ans);
  210.             p = p->r;
  211.         } else
  212.             p = p->l;
  213.     }
  214. }
  215.  
  216. inline void post(node* p, int x) {
  217.     while (p) {
  218.         if (p->d > x) {
  219.             ans = min(p->d, ans);
  220.             p = p->l;
  221.         } else
  222.             p = p->r;
  223.     }
  224. }
  225.  
  226. void debug(node* p) {
  227.     if (p->l)
  228.         debug(p->l);
  229.     if (p->r)
  230.         debug(p->r);
  231.     int x = p->cnt;
  232.     update(p);
  233.     if (p->cnt != x) {
  234.         print_tree(root);
  235.         fprintf(stderr, "\nERROR!\n");
  236.         exit(0);
  237.     }
  238. }
  239.  
  240. void print_tree(node* p) {
  241.     if (!p)
  242.         return;
  243.     fputc('(', stderr);
  244.     print_tree(p->l);
  245.     fprintf(stderr, "%d", p->d);
  246.     print_tree(p->r);
  247.     fputc(')', stderr);
  248. }
Advertisement
Add Comment
Please, Sign In to add comment