Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <string>
- #include <cstdio>
- using namespace std;
- struct TNode {
- int l, r;
- TNode *left, *right;
- bool mark;
- };
- TNode *new_node(int l, int r) {
- return new TNode{ l, r, nullptr, nullptr, 1 };
- }
- bool safe_mark(TNode *node) {
- return (node ? node->mark : 0);
- }
- void relax(TNode *root) {
- root->mark = safe_mark(root->left) | safe_mark(root->right);
- }
- int enter(TNode *root, int l, int r) { //����� �� �����
- if (root->l >= r || root->r <= l || !root->mark) {
- return false;
- }
- if (root->r - root->l == 1) {
- root->mark = 0;
- return root->l;
- }
- int ans = enter(root->left, l, r);
- if (ans == 0) {
- ans = enter(root->right, l, r);
- }
- relax(root);
- return ans;
- }
- void exit(TNode *root, int x) {
- if (root->l > x || root->r <= x) {
- return;
- }
- if (root->r - root->l == 1) {
- root->mark = 1;
- return;
- }
- exit(root->left, x);
- exit(root->right, x);
- relax(root);
- }
- TNode *build_tree(int l, int r) {
- TNode *root = new_node(l, r);
- if (r - l > 1) {
- int m = (r + l) / 2;
- root->left = build_tree(l, m);
- root->right = build_tree(m, r);
- }
- return root;
- }
- int main() {
- ios_base::sync_with_stdio(0);
- freopen("parking.in", "r", stdin);
- freopen("parking.out", "w", stdout);
- int n, m;
- cin >> n >> m;
- TNode *tree = build_tree(1, n + 1);
- string s;
- int x;
- for (int i = 0; i < m; i++) {
- cin >> s >> x;
- if (s == "exit") {
- exit(tree, x);
- } else {
- int ans = enter(tree, x, n + 1);
- if (ans == 0) {
- ans = enter(tree, 1, x);
- }
- cout << ans << "\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement