EntropyIncreaser

treap2

Nov 25th, 2017
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #include <climits>
  2. #include <cstdio>
  3. #include <ctime>
  4.  
  5. #include <algorithm>
  6.  
  7. #define GET_CH(P, F) ((F) ? (P)->rs : (P)->ls)
  8. #define LOG(FMT...) fprintf(stderr, FMT)
  9.  
  10. using namespace std;
  11.  
  12. struct node {
  13.     int x, cnt, rnd;
  14.     node *ls, *rs;
  15. };
  16.  
  17. const int N = 300010;
  18.  
  19. int n;
  20. node *res1, *res2, *root;
  21.  
  22. node* create(int x);
  23. int get_cnt(node* p);
  24. int ran(node* p, int x);
  25. int get(node* p, int rk);
  26. int prev(node* p, int x);
  27. int post(node* p, int x);
  28. void update(node* p);
  29. void debug_order(node* p);
  30. void insert(node*& p, node* q);
  31. void rotate(node*& p, bool f);
  32. void p_remove(node*& p, int x);
  33. void remove(node*& p, int x);
  34.  
  35. int main() {
  36.     int op, x;
  37.     scanf("%d", &n);
  38.     while (n--) {
  39.         scanf("%d%d", &op, &x);
  40.         switch (op) {
  41.         case 0:
  42.             insert(root, create(x));
  43.             break;
  44.         case 1:
  45.             remove(root, x);
  46.             break;
  47.         case 2:
  48.             printf("%d\n", get(root, x));
  49.             break;
  50.         case 3:
  51.             printf("%d\n", ran(root, x));
  52.             break;
  53.         case 4:
  54.             x = prev(root, x);
  55.             printf("%d\n", x == INT_MIN ? -1 : x);
  56.             break;
  57.         case 5:
  58.             x = post(root, x);
  59.             printf("%d\n", x == INT_MAX ? -1 : x);
  60.             break;
  61.         }
  62.     }
  63.     return 0;
  64. }
  65.  
  66. void debug_order(node* root) {
  67.     if (!root)
  68.         return;
  69.     LOG("(");
  70.     debug_order(root->ls);
  71.     LOG(")<-%d->(", root->x);
  72.     debug_order(root->rs);
  73.     LOG(")");
  74. }
  75.  
  76. inline int fast_rand() {
  77.     static int x = time(NULL) << 4, y = time(NULL) >> 2, z = time(NULL);
  78.     int t;
  79.     x ^= x << 16;
  80.     x ^= x >> 5;
  81.     x ^= x << 1;
  82.     t = x;
  83.     x = y;
  84.     y = z;
  85.     z = t ^ x ^ y;
  86.     return z;
  87. }
  88.  
  89. inline node* create(int x) {
  90.     static node pool[N];
  91.     static node* p = pool;
  92.     ++p;
  93.     p->x = x;
  94.     p->cnt = 1;
  95.     p->rnd = fast_rand();
  96.     return p;
  97. }
  98.  
  99. inline void rotate(node*& p, bool f) {
  100.     node* son = GET_CH(p, f);
  101.     GET_CH(p, f) = GET_CH(son, !f);
  102.     GET_CH(son, !f) = p;
  103.     update(p);
  104.     p = son;
  105.     update(p);
  106. }
  107.  
  108. inline int get_cnt(node* p)
  109. { return p ? p->cnt : 0; }
  110.  
  111. inline void update(node* p)
  112. { p->cnt = 1 + get_cnt(p->ls) + get_cnt(p->rs); }
  113.  
  114. void insert(node*& p, node* q) {
  115.     if (!p) {
  116.         p = q;
  117.         return;
  118.     }
  119.     if (q->x < p->x) {
  120.         insert(p->ls, q);
  121.         update(p);
  122.         if (p->ls->rnd < p->rnd)
  123.             rotate(p, 0);
  124.     } else {
  125.         insert(p->rs, q);
  126.         update(p);
  127.         if (p->rs->rnd < p->rnd)
  128.             rotate(p, 1);
  129.     }
  130. }
  131.  
  132. inline void remove(node*& p, int x) {
  133.     p_remove(p, x);
  134.     if (res1)
  135.         insert(p, res1);
  136.     if (res2)
  137.         insert(p, res2);
  138. }
  139.  
  140. void p_remove(node*& p, int x) {
  141.     if (p->x == x) {
  142.         res1 = p->ls;
  143.         res2 = p->rs;
  144.         p = NULL;
  145.         return;
  146.     }
  147.     if (x < p->x)
  148.         p_remove(p->ls, x);
  149.     else
  150.         p_remove(p->rs, x);
  151.     update(p);
  152. }
  153.  
  154. inline int ran(node* p, int x) {
  155.     int ret = 0;
  156.     while (p) {
  157.         if (x <= p->x)
  158.             p = p->ls;
  159.         else {
  160.             ret += get_cnt(p->ls) + 1;
  161.             p = p->rs;
  162.         }
  163.     }
  164.     return ret;
  165. }
  166.  
  167. int get(node* p, int rk) {
  168.     while (p) {
  169.         if (rk == get_cnt(p->ls) + 1)
  170.             return p->x;
  171.         if (rk <= get_cnt(p->ls))
  172.             p = p->ls;
  173.         else {
  174.             rk -= get_cnt(p->ls) + 1;
  175.             p = p->rs;
  176.         }
  177.     }
  178.     return 0;
  179. }
  180.  
  181. inline int prev(node* p, int x) {
  182.     int ret = INT_MIN;
  183.     while (p)
  184.         if (p->x < x) {
  185.             ret = max(ret, p->x);
  186.             p = p->rs;
  187.         } else
  188.             p = p->ls;
  189.     return ret;
  190. }
  191.  
  192. inline int post(node* p, int x) {
  193.     int ret = INT_MAX;
  194.     while (p)
  195.         if (p->x > x) {
  196.             ret = min(ret, p->x);
  197.             p = p->ls;
  198.         } else
  199.             p = p->rs;
  200.     return ret;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment