Dang_Quan_10_Tin

Splay tree

Nov 8th, 2020
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. struct KNode
  2. {
  3.     int Value;
  4.     int Size;
  5.     KNode *P, *L, *R;
  6. };
  7. using QNode = KNode *;
  8. KNode No_thing_here;
  9. QNode nil = &No_thing_here, root;
  10.  
  11. void Link(QNode par, QNode child, bool Right)
  12. {
  13.     child->P = par;
  14.     if (Right)
  15.         par->R = child;
  16.     else
  17.         par->L = child;
  18. }
  19.  
  20. void Update(QNode &a)
  21. {
  22.     a->Size = a->L->Size + a->R->Size + 1;
  23. }
  24.  
  25. void Init()
  26. {
  27.     nil->Size = 0;
  28.     nil->P = nil->L = nil->R = nil;
  29.     root = nil;
  30.     for (int i = 1; i <= n; ++i)
  31.     {
  32.         QNode cur = new KNode;
  33.         cur->P = cur->L = cur->R = nil;
  34.         cur->Value = i;
  35.         Link(cur, root, false);
  36.         root = cur;
  37.         Update(root);
  38.     }
  39. }
  40.  
  41. void Rotate(QNode x)
  42. {
  43.     QNode y = x->P;
  44.     QNode z = y->P;
  45.     if (x == y->L)
  46.     {
  47.         Link(y, x->R, false);
  48.         Link(x, y, true);
  49.     }
  50.     else
  51.     {
  52.         Link(y, x->L, true);
  53.         Link(x, y, false);
  54.     }
  55.     Update(y);
  56.     Update(x);
  57.     x->P = nil;
  58.     if (z != nil)
  59.         Link(z, x, z->R == y);
  60. }
  61.  
  62. void Up_to_Root(QNode x)
  63. {
  64.     while (1){
  65.         QNode y = x->P;
  66.         QNode z = y->P;
  67.         if(y == nil)
  68.             break;
  69.         if(z != nil){
  70.             if((x == y->L) == (y == z->L))
  71.                 Rotate(y);
  72.             else
  73.                 Rotate(x);
  74.         }
  75.         Rotate(x);
  76.     }
  77. }
  78.  
  79. QNode The_kth(QNode x, int k)
  80. {
  81.     while (true)
  82.     {
  83.         if (x->L->Size == k - 1)
  84.             return x;
  85.         if (x->L->Size >= k)
  86.             x = x->L;
  87.         else
  88.         {
  89.             k -= x->L->Size + 1;
  90.             x = x->R;
  91.         }
  92.     }
  93.     return nil;
  94. }
  95.  
  96. void Split(QNode x, int k, QNode &a, QNode &b)
  97. {
  98.     if (k == 0)
  99.     {
  100.         a = nil;
  101.         b = x;
  102.         return;
  103.     }
  104.     QNode cur = The_kth(x, k);
  105.     Up_to_Root(cur);
  106.     a = cur;
  107.     b = a->R;
  108.     a->R = nil;
  109.     b->P = nil;
  110.     Update(a);
  111. }
  112.  
  113. QNode Join(QNode a, QNode b)
  114. {
  115.     if (a == nil)
  116.         return b;
  117.     while (a->R != nil)
  118.         a = a->R;
  119.     Up_to_Root(a);
  120.     Link(a, b, true);
  121.     Update(a);
  122.     return a;
  123. }
  124.  
  125. void Print(QNode &a)
  126. {
  127.     if (a->L != nil)
  128.         Print(a->L);
  129.     cout << (a->Value) << " ";
  130.     if (a->R != nil)
  131.         Print(a->R);
  132. }
Advertisement
Add Comment
Please, Sign In to add comment