Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. struct node {
  7. int x, y;
  8. node *left = nullptr;
  9. node *right = nullptr;
  10. node (int _x, int _y)
  11. :x{_x}, y{_y} {}
  12. };
  13.  
  14. pair <node*, node*> split(node *root, int k){
  15. //cout << "begin";
  16. if (root == nullptr) {
  17. //cout << "LOL";
  18. return {nullptr, nullptr};
  19. }
  20. if (root->x < k) {
  21. //cout << "wtf1";
  22. pair <node*, node*> tmp = split(root->right, k);
  23. root->right = tmp.first;
  24. return {root, tmp.second};
  25. }
  26. else {
  27. //cout << "wtf2";
  28. pair <node*, node*> tmp = split(root->left, k);
  29. root->left = tmp.second;
  30. return {root, tmp.first};
  31. }
  32. }
  33.  
  34. node *merge(node *root1, node *root2) {
  35. if (root1 == nullptr)
  36. return root2;
  37. if (root2 == nullptr)
  38. return root1;
  39. if (root1->y > root2->y) {
  40. node *root = merge(root1->right, root2);
  41. root1->right = root;
  42. return root1;
  43. }
  44. if (root1->y < root2->y) {
  45. node *root = merge(root1, root2->left);
  46. root2->left = root;
  47. return root2;
  48. }
  49. }
  50.  
  51. void add_dekart(node *root, node *v) {
  52. pair <node*, node*> tmp = split(root, v->x);
  53. node *root1 = merge(v, tmp.second);
  54. root = merge(tmp.first, root1);
  55. }
  56.  
  57. void add_binary(node *&root, int x) {
  58. if (root == nullptr) {
  59. root = new node (x, -1);
  60. cout << root->x << "<---\n";
  61. return;
  62. }
  63. if (root->x <= x)
  64. add_binary(root->right, x);
  65. else
  66. add_binary(root->left, x);
  67. }
  68.  
  69. int dfs(node *v, vector <int> &d, int &n) {
  70. if (v != nullptr && d[v->right->x % n] == -1 && v->right != nullptr) {
  71. d[v->right->x % n] = d[v->x] + 1;
  72. dfs(v->right, d, n);
  73. }
  74. if (v != nullptr && d[v->left->x % n] == -1 && v->left != nullptr) {
  75. d[v->left->x % n] = d[v->x] + 1;
  76. dfs(v->left, d, n);
  77. }
  78. }
  79.  
  80. int main() {
  81. int n;
  82. cin >> n;
  83. node *root_dekart{};
  84. node *root_binary{};
  85. vector <int> d(n, -1);
  86. for (int i = 0; i < n; ++i) {
  87. int x, y;
  88. cin >> x >> y;
  89. add_dekart(root_dekart, new node(x, y));
  90. add_binary(root_binary, x);
  91. }
  92.  
  93. int depth1 = 0, depth2 = 0;
  94. if (root_binary == nullptr)
  95. cout << "huli";
  96. d[root_binary->x % n] = 0;
  97. dfs(root_binary, d, n);
  98. for (int a : d)
  99. depth1 = max(a, depth1);
  100.  
  101. d[root_dekart->x % n] = 0;
  102. dfs(root_dekart, d, n);
  103. for (int a : d)
  104. depth2 = max(a, depth2);
  105. cout << depth1 - depth2;
  106. return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement