EntropyIncreaser

splay

Nov 17th, 2017
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <cstdio>
  2.  
  3. #include <algorithm>
  4.  
  5. #define GET_CHILD(P, SIDE) ((SIDE) ? (P)->r : (P)->l)
  6.  
  7. using namespace std;
  8.  
  9. struct node {
  10.     int cnt, id;
  11.     bool f;
  12.     node *l, *r;
  13. };
  14.  
  15. const int N = 100010;
  16.  
  17. node pool[N];
  18. node* root;
  19. bool f;
  20.  
  21. void update(node* p);
  22. void push_down(node* p);
  23. void rotate(node*& p, bool side);
  24. void splay(node*& p, int x);
  25. void order(node* p);
  26.  
  27. int main() {
  28.     int n, m, l, r;
  29.     scanf("%d%d", &n, &m);
  30.     for (int i = 1; i <= n; ++i) {
  31.         pool[i].id = i;
  32.         pool[i].cnt = n - i + 1;
  33.         if (i != n)
  34.             pool[i].r = pool + i + 1;
  35.     }
  36.     root = pool + 1;
  37.     while (m--) {
  38.         scanf("%d%d", &l, &r);
  39.         if (l == 1) {
  40.             if (r == n) {
  41.                 root->f = !root->f;
  42.                 continue;
  43.             }
  44.             splay(root, r + 1);
  45.             root->l->f = !root->l->f;
  46.             continue;
  47.         }
  48.         splay(root, l - 1);
  49.         if (r == n) {
  50.             root->r->f = !root->r->f;
  51.             continue;
  52.         }
  53.         splay(root, r + 1);
  54.         root->l->r->f = !root->l->r->f;
  55.     }
  56.     order(root);
  57.     putchar('\n');
  58.     return 0;
  59. }
  60.  
  61. inline void rotate(node*& p, bool side) {
  62.     push_down(p);
  63.     node* son = GET_CHILD(p, side);
  64.     push_down(son);
  65.     GET_CHILD(p, side) = GET_CHILD(son, !side);
  66.     GET_CHILD(son, !side) = p;
  67.     update(p);
  68.     p = son;
  69.     update(p);
  70. }
  71.  
  72. inline void push_down(node* p) {
  73.     if (p->f)
  74.         swap(p->l, p->r);
  75.     if (p->l)
  76.         p->l->f ^= p->f;
  77.     if (p->r)
  78.         p->r->f ^= p->f;
  79.     p->f = false;
  80. }
  81.  
  82. inline void update(node* p) {
  83.     p->cnt = 1;
  84.     if (p->l)
  85.         p->cnt += p->l->cnt;
  86.     if (p->r)
  87.         p->cnt += p->r->cnt;
  88. }
  89.  
  90. void splay(node*& p, int x) {
  91.     push_down(p);
  92.     if (p->l) {
  93.         if (p->l->cnt >= x) {
  94.             splay(p->l, x);
  95.             rotate(p, 0);
  96.             return;
  97.         }
  98.         x -= p->l->cnt;
  99.     }
  100.     if (x == 1)
  101.         return;
  102.     splay(p->r, x - 1);
  103.     rotate(p, 1);
  104. }
  105.  
  106. void order(node* p) {
  107.     if (!p)
  108.         return;
  109.     push_down(p);
  110.     order(p->l);
  111.     if (f)
  112.         putchar(' ');
  113.     printf("%d", p->id);
  114.     f = true;
  115.     order(p->r);
  116. }
Advertisement
Add Comment
Please, Sign In to add comment