Advertisement
IzhanVarsky

Untitled

Apr 12th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. #include <ctime>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. struct Node {
  10.     int Y, size, data, empty;
  11.     Node *Left, *Right, *Parent;
  12.  
  13.     Node(int y, int d, int emp = 0) {
  14.         empty = emp;
  15.         Y = y;
  16.         data = d;
  17.         size = 1;
  18.         Left = Right = Parent = nullptr;
  19.     }
  20. };
  21.  
  22. int getSize(Node *treap) {
  23.     return treap ? treap->size : 0;
  24. }
  25.  
  26. void updSize(Node *treap) {
  27.     if (treap != nullptr) treap->size = getSize(treap->Left) + getSize(treap->Right) + 1;
  28. }
  29.  
  30. int getEmptyCnt(Node *treap) {
  31.     return treap ? treap->empty : 0;
  32. }
  33.  
  34. void updEmptyCnt(Node *treap) {
  35.     if (treap != nullptr)
  36.         treap->empty = getEmptyCnt(treap->Left) + getEmptyCnt(treap->Right) + (treap->data == 0 ? 1 : 0);
  37. }
  38.  
  39. void bigUpd(Node *treap) {
  40.     updSize(treap);
  41.     updEmptyCnt(treap);
  42. }
  43.  
  44. void split(Node *treap, int pivotKey, Node *&lRes, Node *&rRes) {
  45.     if (treap == nullptr) {
  46.         lRes = rRes = nullptr;
  47.         return;
  48.     }
  49.     int XX = getSize(treap->Left);
  50.     if (pivotKey < XX) {
  51.         split(treap->Left, pivotKey, lRes, treap->Left);
  52.         rRes = treap;
  53.         bigUpd(rRes);
  54.     } else {
  55.         split(treap->Right, pivotKey, treap->Right, rRes);
  56.         lRes = treap;
  57.         bigUpd(lRes);
  58.     }
  59. }
  60.  
  61. void merge(Node *&treap, Node *l, Node *r) {
  62.     if (!l || !r) {
  63.         treap = l ? l : r;
  64.     } else if (l->Y > r->Y) {
  65.         merge(l->Right, l->Right, r);
  66.         treap = l;
  67.     } else {
  68.         merge(r->Left, l, r->Left);
  69.         treap = r;
  70.     }
  71.     bigUpd(treap);
  72. }
  73.  
  74. void delNextZero(Node *&treap) {
  75.     if (treap == nullptr) return;
  76.     if (getEmptyCnt(treap->Left) != 0){
  77.         return delNextZero(treap->Left);
  78.     }
  79.     if (treap->data == 0) {
  80.         Node *t1, *t2, *t3;
  81.         split(treap, treap->size - 2, t1, t2);
  82.         split(t2, treap->size, t2, t3);
  83.         merge(treap, t1, t3);
  84.         bigUpd(treap);
  85.     }
  86.     if (getEmptyCnt(treap->Right) != 0){
  87.         return delNextZero(treap->Right);
  88.     }
  89. }
  90.  
  91. void insertWithDelNextZero(Node *&treap, int pos, Node *toIns) {
  92.     Node *t1, *t2;
  93.     // Troubles::??
  94.     split(treap, pos, t1, t2);
  95.     delNextZero(t2);
  96.     merge(treap, t1, toIns);
  97.     merge(treap, treap, t2);
  98.     bigUpd(treap);
  99. }
  100.  
  101. void printTreap(Node *&treap) {
  102.     if (treap == nullptr) return;
  103.     printTreap(treap->Left);
  104.     cout << treap->data << " ";
  105.     printTreap(treap->Right);
  106. }
  107.  
  108. int main() {
  109.     srand(time(nullptr));
  110.     ios_base::sync_with_stdio(false);
  111.     cin.tie(nullptr);
  112.     cout.tie(nullptr);
  113.     Node *treap = nullptr;
  114.     int n, m;
  115.     cin >> n >> m;
  116.     for (int i = 0; i < m; ++i) {
  117.         merge(treap, treap, new Node(rand(), 0, 1));
  118.         bigUpd(treap);
  119.     }
  120.  
  121.     // заполнить курево
  122. //    treap = new Node(rand(), 0, 1);
  123. //    Node *last = treap;
  124. //    for (int i = 1; i < m; ++i) {
  125. //        int YY = rand();
  126. //        Node *toAdd = new Node(YY, 0, 1);
  127. //        if (last->Y < YY) {
  128. //            last->Right = toAdd;
  129. //            last->Right->Parent = last;
  130. //        } else {
  131. //            Node *cur = last->Parent;
  132. //            while (cur && cur->Y > YY) {
  133. //                cur = cur->Parent;
  134. //            }
  135. //            if (cur == nullptr) {
  136. //                // вылетели из корня*
  137. //                toAdd->Left = treap;
  138. //                toAdd->Left->Parent = toAdd;
  139. //                treap = toAdd;
  140. //            } else {
  141. //                Node *oldRight = cur->Right;
  142. //                cur->Right = toAdd;
  143. //                cur->Right->Parent = cur;
  144. //                cur->Right->Left = oldRight;
  145. //                cur->Right->Left->Parent = toAdd;
  146. //            }
  147. //        }
  148. //        last = toAdd;
  149. //        bigUpd(last);
  150. //    }
  151.     // закончили заполнять
  152.  
  153.     printTreap(treap);
  154.     cout << endl;
  155.     for (int i = 1; i <= n; ++i) {
  156.         int x;
  157.         cin >> x;
  158.         insertWithDelNextZero(treap, x, new Node(rand(), i, 0));
  159.     }
  160.  
  161.     printTreap(treap);
  162.     cout << endl;
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement