Advertisement
VisualPaul

Implicit key splay

Oct 28th, 2014
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. struct node;
  4. extern node *const null;
  5.  
  6. struct node {
  7.     node *ch[2], *par;
  8.     int value;
  9.     int size;
  10.     node(int val) : par(null), value(val), size(1) {
  11.         ch[0] = ch[1] = null;
  12.     }
  13.     node() : par(this), value(0), size(0) {
  14.         ch[0] = ch[1] = this;
  15.     }
  16.     void setCh(int x, node *n) {
  17.         ch[x] = n;
  18.         if (n != null)
  19.             n->par = this;
  20.     }
  21.     node *delCh(int x) {
  22.         node *res = ch[x];
  23.         ch[x]->par = null;
  24.         ch[x] = null;
  25.         return res;
  26.     }
  27. };
  28.  
  29. node *const null = new node;
  30.  
  31. void update(node *nd) {
  32.     nd->size = nd->ch[1]->size + 1 + nd->ch[0]->size;
  33. }
  34.  
  35. int child(node *nd) {
  36.     return nd == nd->par->ch[1];
  37. }
  38.  
  39. void rot(node *nd) {
  40.     node *pr = nd->par;
  41.     int x = child(nd);
  42.     if (pr->par != null)
  43.         pr->par->setCh(child(pr), nd);
  44.     nd->par = pr->par;
  45.     pr->setCh(x, nd->ch[1 - x]);
  46.     nd->setCh(1 - x, pr);
  47.     update(pr);
  48.     update(nd);
  49. }
  50.  
  51. void splay(node *nd) {
  52.     while (nd->par != null) {
  53.         if (nd->par->par == null) {
  54.             rot(nd);
  55.         } else if (child(nd) == child(nd->par)) {
  56.             rot(nd->par);
  57.             rot(nd);
  58.         } else {
  59.             rot(nd);
  60.             rot(nd);
  61.         }
  62.     }
  63. }
  64.  
  65. node *access(node *root, int key) {
  66.     node *cur;
  67.     for (cur = root; cur->ch[0]->size != key;) {
  68.         if (key < cur->ch[0]->size) {
  69.             cur = cur->ch[0];
  70.         } else {
  71.             key -= cur->ch[0]->size + 1;
  72.             cur = cur->ch[1];
  73.         }          
  74.     }
  75.     splay(cur);
  76.     return cur;
  77. }
  78.  
  79. void split(node *nd, int key, node *&left, node *&right) {
  80.     if (key == nd->size) {
  81.         left = nd;
  82.         right = null;
  83.         return;
  84.     }
  85.     right = access(nd, key);
  86.     left = right->delCh(0);
  87.     update(right);
  88. }
  89.  
  90. node *merge(node *le, node *ri) {
  91.     if (ri == null)
  92.         return le;
  93.     ri = access(ri, 0);
  94.     ri->setCh(0, le);
  95.     update(ri);
  96.     return ri;
  97. }
  98.                                
  99. node *build(int l, int r) {
  100.     if (l == r)
  101.         return null;
  102.     int m = (l + r) / 2;
  103.     node *res = new node(m);
  104.     res->setCh(0, build(l, m));
  105.     res->setCh(1, build(m + 1, r));
  106.     update(res);
  107.     return res;
  108. }
  109.  
  110. bool splay_check(node *nd, node *parent) {
  111.     return (nd == null && nd->par == null && nd->ch[0] == null && nd->ch[1] == null && nd->size == 0 && nd->value == 0)
  112.         || (nd->par == parent && splay_check(nd->ch[0], nd) && splay_check(nd->ch[1], nd)
  113.          && nd->size == nd->ch[0]->size + 1 + nd->ch[1]->size);
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement