Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <fstream>
  5. #include <string>
  6. #include <queue>
  7.  
  8. using namespace std;
  9.  
  10. typedef long long ll;
  11.  
  12. struct info {
  13. int l, r, p;
  14. };
  15.  
  16.  
  17.  
  18. struct Node {
  19. ll x, y, number;
  20. Node *left, *right;
  21. Node(ll x, ll y, ll number)
  22. {
  23. this->x = x;
  24. this->number = number;
  25. this->y = y;
  26. left = right = NULL;
  27. }
  28. };
  29.  
  30. typedef Node* pNode;
  31.  
  32.  
  33.  
  34. pNode merge(pNode Left, pNode Right)
  35. {
  36. if (Left == NULL)
  37. return Right;
  38. if (Right == NULL)
  39. return Left;
  40. if (Left->y > Right->y)
  41. {
  42. Left->right = merge(Left->right, Right);
  43. return Left;
  44. }
  45. else
  46. {
  47. Right->left = merge(Left, Right->left);
  48. return Right;
  49. }
  50. }
  51.  
  52. pair<pNode, pNode> split(pNode Tree, ll x)
  53. {
  54. if (Tree == NULL)
  55. return {NULL, NULL};
  56. if (Tree->x > x)
  57. {
  58. auto pair_tree = split(Tree->left, x);
  59. Tree->left = pair_tree.second;
  60. return {pair_tree.first, Tree};
  61. }
  62. else
  63. {
  64. auto pair_tree = split(Tree->right, x);
  65. Tree->right = pair_tree.first;
  66. return {Tree, pair_tree.second};
  67. }
  68. }
  69.  
  70.  
  71. pNode insert(pNode Tree, pNode vertex)
  72. {
  73. if (Tree == NULL)
  74. {
  75. return vertex;
  76. }
  77. auto pair_tree = split(Tree, vertex->x);
  78. Tree = merge(pair_tree.first, vertex);
  79. Tree = merge(Tree, pair_tree.second);
  80. return Tree;
  81. }
  82.  
  83.  
  84. pNode remove(pNode Tree, ll x)
  85. {
  86. auto del1 = split(Tree, x - 1);
  87. auto del2 = split(del1.second, x);
  88. return merge(del1.first, del2.second);
  89. }
  90.  
  91.  
  92. bool check(pNode Tree, ll x)
  93. {
  94. if (Tree == NULL)
  95. return false;
  96. if (Tree->x == x)
  97. return true;
  98. if (x < Tree->x)
  99. return check(Tree->left, x);
  100. if (x > Tree->x)
  101. return check(Tree->right, x);
  102. }
  103.  
  104. void print(pNode Tree, int parent, info *infos) {
  105. info local;
  106. if (!Tree)
  107. return;
  108. local.p = parent;
  109. if (Tree->left) {
  110. local.l = Tree->left->number;
  111. }
  112. else {
  113. local.l = 0;
  114. }
  115. if (Tree->right) {
  116. local.r = Tree->right->number;
  117. }
  118. else {
  119. local.r = 0;
  120. }
  121. infos[Tree->number - 1] = local;
  122. if (Tree->left)
  123. print(Tree->left, Tree->number, infos);
  124. if (Tree->right)
  125. print(Tree->right, Tree->number, infos);
  126. }
  127.  
  128. bool compare(pair<pair<ll,ll>, ll> first, pair<pair<ll,ll>, ll> second) {
  129. if (first.first.first <= second.first.first) {
  130. return true;
  131. } else {
  132. return false;
  133. }
  134. }
  135.  
  136. int main()
  137. {
  138. string question;
  139. ll a, b;
  140.  
  141. int cnt_el = 0;
  142. int n;
  143. cin >> n;
  144. info* infos = new info[n];
  145.  
  146. vector<pair<pair<ll, ll>, ll>> in;
  147. for (int i = 0; i < n; i++)
  148. {
  149. cin >> a >> b;
  150. in.push_back(make_pair(make_pair(a, -b), i+1));
  151. }
  152. sort(in.begin(), in.end(), compare);
  153.  
  154. queue<pNode> tree;
  155. for (auto e : in) {
  156. cout << e.first.first << ' ' << e.first.second << endl;
  157. pNode vertex = new Node(e.first.first, e.first.second, e.second);
  158. tree.push(vertex);
  159. }
  160. while (tree.size() > 1) {
  161. pNode vertex1 = tree.front();
  162. tree.pop();
  163. pNode vertex2 = tree.front();
  164. tree.pop();
  165. tree.push(merge(vertex1, vertex2));
  166. }
  167.  
  168. print(tree.front(), 0, infos);
  169. cout << "YES" << endl;
  170. for (int i = 0; i < n; i++) {
  171. printf("%d %d %d\n", infos[i].p, infos[i].l, infos[i].r);
  172. }
  173. //system("pause");
  174. }
  175.  
  176. /*bool in_tree = check(tree, a);
  177. if (in_tree) {
  178. cout << "NO";
  179. return 0;
  180. }*/
  181.  
  182. //pNode vertex = new Node(a, -b, i + 1);
  183. //tree = insert(tree, vertex);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement