Advertisement
Guest User

Untitled

a guest
May 20th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. #include <cstdio>
  6. using namespace std;
  7.  
  8. struct TNode {
  9. int l, r;
  10. TNode *left, *right;
  11. bool mark;
  12. };
  13.  
  14. TNode *new_node(int l, int r) {
  15. return new TNode{ l, r, nullptr, nullptr, 1 };
  16. }
  17.  
  18. bool safe_mark(TNode *node) {
  19. return (node ? node->mark : 0);
  20. }
  21.  
  22. void relax(TNode *root) {
  23. root->mark = safe_mark(root->left) | safe_mark(root->right);
  24. }
  25.  
  26. int enter(TNode *root, int l, int r) { //����� �� �����
  27. if (root->l >= r || root->r <= l || !root->mark) {
  28. return false;
  29. }
  30. if (root->r - root->l == 1) {
  31. root->mark = 0;
  32. return root->l;
  33. }
  34. int ans = enter(root->left, l, r);
  35. if (ans == 0) {
  36. ans = enter(root->right, l, r);
  37. }
  38. relax(root);
  39. return ans;
  40. }
  41.  
  42. void exit(TNode *root, int x) {
  43. if (root->l > x || root->r <= x) {
  44. return;
  45. }
  46. if (root->r - root->l == 1) {
  47. root->mark = 1;
  48. return;
  49. }
  50. exit(root->left, x);
  51. exit(root->right, x);
  52. relax(root);
  53. }
  54.  
  55. TNode *build_tree(int l, int r) {
  56. TNode *root = new_node(l, r);
  57. if (r - l > 1) {
  58. int m = (r + l) / 2;
  59. root->left = build_tree(l, m);
  60. root->right = build_tree(m, r);
  61. }
  62. return root;
  63. }
  64.  
  65. int main() {
  66. ios_base::sync_with_stdio(0);
  67. freopen("parking.in", "r", stdin);
  68. freopen("parking.out", "w", stdout);
  69. int n, m;
  70. cin >> n >> m;
  71. TNode *tree = build_tree(1, n + 1);
  72. string s;
  73. int x;
  74. for (int i = 0; i < m; i++) {
  75. cin >> s >> x;
  76. if (s == "exit") {
  77. exit(tree, x);
  78. } else {
  79. int ans = enter(tree, x, n + 1);
  80. if (ans == 0) {
  81. ans = enter(tree, 1, x);
  82. }
  83. cout << ans << "\n";
  84. }
  85. }
  86. return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement