Advertisement
bibaboba12345

робот через дд

Sep 7th, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <random>
  5. #include <vector>
  6. using namespace std;
  7. const long long MOD = 1e9 + 7, N = 2e3 + 7;
  8. mt19937 rnd;
  9.  
  10. struct Node {
  11. int key, priority, size = 1;
  12. Node* left = nullptr;
  13. Node* right = nullptr;
  14.  
  15. Node(int key) : key(key), priority(rnd()) {}
  16. };
  17.  
  18. int get_size(Node* a) {
  19. if (a == nullptr) {
  20. return 0;
  21. }
  22. else {
  23. return a->size;
  24. }
  25. }
  26.  
  27. Node* update_size(Node* a) {
  28. a->size = get_size(a->left) + 1 + get_size(a->right);
  29. return a;
  30. }
  31.  
  32. pair<Node*, Node*> split(Node* root, int key) {
  33. if (root == nullptr) {
  34. return { nullptr, nullptr };
  35. }
  36. if (root->key <= key) {
  37. auto splitted = split(root->right, key);
  38. root->right = splitted.first;
  39. root = update_size(root);
  40. return { root, splitted.second };
  41. }
  42. auto splitted = split(root->left, key);
  43. root->left = splitted.second;
  44. root = update_size(root);
  45. return { splitted.first, root };
  46. };
  47.  
  48. Node* merge(Node* a, Node* b) {
  49. if (a == nullptr) {
  50. return b;
  51. }
  52. if (b == nullptr) {
  53. return a;
  54. }
  55. if (a->priority < b->priority) {
  56. a->right = merge(a->right, b);
  57. a = update_size(a);
  58. return a;
  59. }
  60. else {
  61. b->left = merge(a, b->left);
  62. b = update_size(b);
  63. return b;
  64. }
  65. }
  66.  
  67. Node* insert(Node* root, Node* newNode) {
  68. auto splitted = split(root, newNode->key);
  69. root = merge(merge(splitted.first, newNode), splitted.second);
  70. return root;
  71. }
  72.  
  73. Node* erase(Node* root, int x) {
  74. auto splitted = split(root, x);
  75. auto splitted_less = split(splitted.first, x - 1);
  76. if (splitted_less.second != nullptr) {
  77. delete splitted_less.second;
  78. root = merge(splitted_less.first, splitted.second);
  79. return root;
  80. }
  81. else {
  82. return root;
  83. }
  84. }
  85. int find_lessandeq(Node* root, int x) {
  86. auto splitted = split(root, x);
  87. int ans = get_size(splitted.first);
  88. root = merge(splitted.first, splitted.second);
  89. return ans;
  90. }
  91. int find_less(Node* root, int x) {
  92. auto splitted = split(root, x-1);
  93. int ans = get_size(splitted.first);
  94. root = merge(splitted.first, splitted.second);
  95. return ans;
  96. }
  97. int find_moreandeq(Node* root, int x) {
  98. auto splitted = split(root, x-1);
  99. int ans = get_size(splitted.second);
  100. root = merge(splitted.first, splitted.second);
  101. return ans;
  102. }
  103. int find_more(Node* root, int x) {
  104. auto splitted = split(root, x);
  105. int ans = get_size(splitted.second);
  106. root = merge(splitted.first, splitted.second);
  107. return ans;
  108. }
  109. long long n, m, total_rasst, x, y;
  110. Node* root_x, *root_y;
  111. char s;
  112. int main()
  113. {
  114. root_x = nullptr;
  115. root_y = nullptr;
  116. cin >> n >> m;
  117. for (int i = 0; i < n; i++) {
  118. cin >> x >> y;
  119. total_rasst += abs(x) + abs(y);
  120. root_x = insert(root_x, new Node(x));
  121. root_y = insert(root_y, new Node(y));
  122. }
  123. x = 0; y = 0;
  124. for (int i = 0; i < m; i++) {
  125. cin >> s;
  126. if (s == 'S') {
  127. total_rasst += find_lessandeq(root_y, y);
  128. total_rasst -= find_more(root_y, y);
  129. y++;
  130. cout << total_rasst << "\n";
  131. }
  132. if (s == 'I') {
  133. total_rasst += find_lessandeq(root_x, x);
  134. total_rasst -= find_more(root_x, x);
  135. x++;
  136. cout << total_rasst << "\n";
  137. }
  138. if (s == 'J') {
  139. total_rasst += find_moreandeq(root_y, y);
  140. total_rasst -= find_less(root_y, y);
  141. y--;
  142. cout << total_rasst << "\n";
  143. }
  144. if (s == 'Z') {
  145. total_rasst += find_moreandeq(root_x, x);
  146. total_rasst -= find_less(root_x, x);
  147. x--;
  148. cout << total_rasst << "\n";
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement