EntropyIncreaser

splay2

Nov 29th, 2017
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. #include <cstdio>
  2.  
  3. #include <algorithm>
  4.  
  5. #define LOG(FMT...) fprintf(stderr, FMT)
  6.  
  7. using namespace std;
  8.  
  9. struct node {
  10.     int x, cnt;
  11.     bool lazy;
  12.     node *ls, *rs;
  13. };
  14.  
  15. const int N = 100010;
  16.  
  17. int n, m;
  18. node* root;
  19.  
  20. int get_cnt(node* p);
  21. node* build(int l, int r);
  22. void push_down(node* p);
  23. void update(node* p);
  24. void zig(node*& p);
  25. void zag(node*& p);
  26. void splay(node*& p, int x);
  27. void out(node* p);
  28.  
  29. int main() {
  30.     int l, r;
  31.     scanf("%d%d", &n, &m);
  32.     root = build(1, n);
  33.     while (m--) {
  34.         scanf("%d%d", &l, &r);
  35.         if (l > 1) {
  36.             splay(root, l - 1);
  37.             if (r < n) {
  38.                 push_down(root);
  39.                 splay(root->rs, r - l + 2);
  40.                 root->rs->ls->lazy ^= true;
  41.             } else
  42.                 root->rs->lazy ^= true;
  43.         } else if (r < n) {
  44.             splay(root, r + 1);
  45.             root->ls->lazy ^= true;
  46.         } else
  47.             root->lazy ^= true;
  48.     }
  49.     out(root);
  50.     putchar('\n');
  51.     return 0;
  52. }
  53.  
  54. void out(node* p) {
  55.     static bool fst = true;
  56.     if (!p)
  57.         return;
  58.     push_down(p);
  59.     out(p->ls);
  60.     if (fst) {
  61.         printf("%d", p->x);
  62.         fst = false;
  63.     } else
  64.         printf(" %d", p->x);
  65.     out(p->rs);
  66. }
  67.  
  68. node* build(int l, int r) {
  69.     static node pool[N];
  70.     static int cnt;
  71.     if (r < l)
  72.         return NULL;
  73.     node* p = pool + cnt;
  74.     ++cnt;
  75.     int mid = (l + r) >> 1;
  76.     p->x = mid;
  77.     p->cnt = r - l + 1;
  78.     p->ls = build(l, mid - 1);
  79.     p->rs = build(mid + 1, r);
  80.     return p;
  81. }
  82.  
  83. inline int get_cnt(node* p)
  84. { return p ? p->cnt : 0; }
  85.  
  86. inline void update(node* p)
  87. { p->cnt = 1 + get_cnt(p->ls) + get_cnt(p->rs); }
  88.  
  89. inline void push_down(node* p) {
  90.     if (!p->lazy)
  91.         return;
  92.     swap(p->ls, p->rs);
  93.     if (p->ls)
  94.         p->ls->lazy ^= true;
  95.     if (p->rs)
  96.         p->rs->lazy ^= true;
  97.     p->lazy = false;
  98. }
  99.  
  100. inline void zig(node*& p) {
  101.     push_down(p);
  102.     node* son = p->ls;
  103.     push_down(son);
  104.     p->ls = son->rs;
  105.     son->rs = p;
  106.     update(p);
  107.     p = son;
  108.     update(p);
  109. }
  110.  
  111. inline void zag(node*& p) {
  112.     push_down(p);
  113.     node* son = p->rs;
  114.     push_down(son);
  115.     p->rs = son->ls;
  116.     son->ls = p;
  117.     update(p);
  118.     p = son;
  119.     update(p);
  120. }
  121.  
  122. void splay(node*& p, int x) {
  123.     push_down(p);
  124.     if (x == get_cnt(p->ls) + 1)
  125.         return;
  126.     if (x <= get_cnt(p->ls)) {
  127.         push_down(p->ls);
  128.         if (x == get_cnt(p->ls->ls) + 1) {
  129.             zig(p);
  130.             return;
  131.         }
  132.         if (x <= get_cnt(p->ls->ls)) {
  133.             splay(p->ls->ls, x);
  134.             zig(p);
  135.             zig(p);
  136.             return;
  137.         }
  138.         splay(p->ls->rs, x - 1 - get_cnt(p->ls->ls));
  139.         zag(p->ls);
  140.         zig(p);
  141.         return;
  142.     }
  143.     x -= 1 + get_cnt(p->ls);
  144.     push_down(p->rs);
  145.     if (x == get_cnt(p->rs->ls) + 1) {
  146.         zag(p);
  147.         return;
  148.     }
  149.     if (x <= get_cnt(p->rs->ls)) {
  150.         splay(p->rs->ls, x);
  151.         zig(p->rs);
  152.         zag(p);
  153.         return;
  154.     }
  155.     splay(p->rs->rs, x - 1 - get_cnt(p->rs->ls));
  156.     zag(p);
  157.     zag(p);
  158. }
Advertisement
Add Comment
Please, Sign In to add comment