Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. struct Node {
  4. int dataFirst;
  5. int dataSecond;
  6. long long ln = 1ll;
  7. Node * left = nullptr;
  8. Node * right = nullptr;
  9. };
  10.  
  11. struct Pair {
  12. Node * first;
  13. Node * second;
  14. };
  15.  
  16. Node * root;
  17. int size_ = 0;
  18.  
  19. int getSum(Node * current) {
  20. if (current == NULL) {
  21. return 0;
  22. } else {
  23. return current -> ln;
  24. }
  25. }
  26.  
  27. void qSort(int **arr, int nStart, int nEnd) {
  28. int l, r, x;
  29. if (nStart >= nEnd) return;
  30. l = nStart;
  31. r = nEnd;
  32. x = arr[(l + r) / 2][1];
  33. while (l <= r) {
  34. while (arr[l][1] < x) l++;
  35. while (arr[r][1] > x) r--;
  36. if (l <= r) {
  37. int first = arr[l][0];
  38. int second = arr[l][1];
  39. arr[l][0] = arr[r][0];
  40. arr[r][0] = first;
  41. arr[l][1] = arr[r][1];
  42. arr[r][1] = second;
  43. }
  44. l++;
  45. r--;
  46. }
  47. qSort(arr, nStart, r);
  48. qSort(arr, l, nEnd);
  49. }
  50.  
  51. void update(Node * root) {
  52. if (root == NULL) {
  53. return;
  54. } else {
  55. root -> ln = 1 + getSum(root -> left) + getSum(root -> right);
  56. }
  57. }
  58.  
  59. Node * merge(Node * first, Node * second) {
  60. if (first == NULL || second == NULL) {
  61. return first == NULL ? second : first;
  62. } else {
  63. if (first -> dataSecond > second -> dataSecond) {
  64. first -> right = merge(first -> right, second);
  65. update(first);
  66. return first;
  67. } else {
  68. second -> left = merge(first, second -> left);
  69. update(second);
  70. return second;
  71. }
  72. }
  73. }
  74.  
  75. Pair split(Node * root, int value) {
  76. Pair pair;
  77. if (root == NULL) {
  78. pair.first = NULL;
  79. pair.second = NULL;
  80. return pair;
  81. } else {
  82. if (root -> dataFirst < value) {
  83. pair = split(root -> right, value);
  84. root -> right = pair.first;
  85. pair.first = root;
  86. } else {
  87. pair = split(root -> left, value);
  88. root -> left = pair.second;
  89. pair.second = root;
  90. }
  91. }
  92. update(pair.first);
  93. update(pair.second);
  94. return pair;
  95. }
  96.  
  97. void insert(int dataFirst, int dataSecond) {
  98. Node * forInsert = new Node();
  99. forInsert -> dataFirst = dataFirst;
  100. forInsert -> dataSecond = dataSecond;
  101. Pair pair;
  102. pair = split(root, dataFirst);
  103. pair.first = merge(pair.first, forInsert);
  104. root = merge(pair.first, pair.second);
  105. size_++;
  106. }
  107.  
  108. void inOrderTraversal(Node * root, Node * currentParent) {
  109. if (root != nullptr) {
  110. inOrderTraversal(root -> left, root);
  111. if (currentParent != nullptr) {
  112. cout << currentParent -> dataFirst << " ";
  113. } else {
  114. cout << 0 << " ";
  115. }
  116. if (root -> left != nullptr) {
  117. cout << root -> left -> dataFirst << " ";
  118. } else {
  119. cout << 0 << " ";
  120. }
  121. if (root -> right != nullptr) {
  122. cout << root -> right -> dataFirst << " ";
  123. } else {
  124. cout << 0 << " ";
  125. }
  126. cout << endl;
  127. inOrderTraversal(root -> right, root);
  128. }
  129. }
  130.  
  131. int main() {
  132. ios_base::sync_with_stdio(false);
  133. cin.tie(0);
  134. cout.tie(0);
  135. int n;
  136. cin >> n;
  137. int **arr;
  138. arr = new int*[n];
  139. for (int i = 0; i < n; i++) {
  140. int x, y;
  141. cin >> x >> y;
  142. arr[i] = new int[2];
  143. arr[i][0] = x;
  144. arr[i][1] = y;
  145. }
  146. qSort(arr, 0, n - 1);
  147. for(int i = 0; i < n; i++) {
  148. insert(arr[n - 1 - i][0], arr[i][1]);
  149. }
  150. inOrderTraversal(root, nullptr);
  151. return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement