답안 #909983

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
909983 2024-01-17T17:24:54 Z Alcabel Abracadabra (CEOI22_abracadabra) C++17
100 / 100
881 ms 41624 KB
#include <bits/stdc++.h>
using namespace std;

mt19937 rnd(161);

struct Node {
    int val, priority, sz, maxVal;
    int left, right;
    Node() {
        left = right = -1;
    }
    Node(int _val) {
        val = _val, sz = 1, maxVal = val;
        priority = rnd();
        left = right = -1;
    }
};

const int maxn = 2e5;
Node treap[maxn];

int getSz(int root) {
    if (root == -1) { return 0; }
    return treap[root].sz;
}

int getMax(int root) {
    if (root == -1) { return 0; }
    return treap[root].maxVal;
}

void update(int root) {
    if (root == -1) { return; }
    treap[root].sz = getSz(treap[root].left) + getSz(treap[root].right) + 1;
    treap[root].maxVal = max(max(getMax(treap[root].left), getMax(treap[root].right)), treap[root].val);
}

int merge(int l, int r) {
    if (l == -1) { return r; }
    if (r == -1) { return l; }
    if (treap[l].priority > treap[r].priority) {
        treap[l].right = merge(treap[l].right, r);
        update(l);
        return l;
    }
    treap[r].left = merge(l, treap[r].left);
    update(r);
    return r;
}

pair<int, int> split(int root, int k) {
    if (root == -1) { return {-1, -1}; }
    int lsz = getSz(treap[root].left);
    if (lsz >= k) {
        auto [l, r] = split(treap[root].left, k);
        treap[root].left = r;
        update(root);
        return {l, root};
    }
    auto [l, r] = split(treap[root].right, k - lsz - 1);
    treap[root].right = l;
    update(root);
    return {root, r};
}

int getKth(int root, int k) {
    if (root == -1) { return 0; }
    while (root != -1) {
        int lsz = getSz(treap[root].left);
        if (lsz >= k) {
            root = treap[root].left;
        } else {
            k -= lsz;
            if (k == 1) {
                break;
            }
            --k;
            root = treap[root].right;
        }
    }
    assert(root != -1);
    return treap[root].val;
}

int getFirstGreater(int root, int x) {
    if (root == -1) { return 0; }
    int res = 0;
    while (root != -1) {
        if (getMax(treap[root].left) > x) {
            root = treap[root].left;
        } else {
            res += getSz(treap[root].left);
            if (treap[root].val > x) {
                break;
            }
            root = treap[root].right;
            ++res;
        }
    }
    return res;
}

void dfs(int root) {
    if (root == -1) { return; }
    dfs(treap[root].left);
    cerr << treap[root].val << ' ';
    dfs(treap[root].right);
}

struct Query {
    int t, pos, i;
    Query() {}
    Query(int _t, int _pos, int _i) {
        t = _t, pos = _pos, i = _i;
    }
    bool operator<(const Query& other) const {
        return t < other.t;
    }
    ~Query() {}
};

void solve() {
    int n, q;
    cin >> n >> q;
    int half = n / 2, root = -1;
    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        treap[i] = Node(x);
        root = merge(root, i);
    }
    /*cerr << "built:\n";
    dfs(root);
    cerr << '\n';*/
    vector<Query> queries(q);
    for (int i = 0; i < q; ++i) {
        cin >> queries[i].t >> queries[i].pos;
        queries[i].i = i;
    }
    sort(queries.begin(), queries.end());
    int t = 0;
    bool cycled = false;
    vector<int> ans(q);
    for (const Query& query : queries) {
        while (!cycled && t < query.t) {
            auto [lh, rh] = split(root, half);
            int firstL = getKth(lh, 1), firstR = getKth(rh, 1);
            if (firstR > getMax(lh)) {
                cycled = true;
                root = merge(lh, rh);
            } else {
                int turn = (firstL > firstR ? 1 : -1);
                root = -1;
                while (firstL != 0 && firstR != 0) {
                    if (turn == -1) {
                        // cut from left
                        int k = getFirstGreater(lh, firstR);
                        assert(k >= 1);
                        auto [start, fin] = split(lh, k);
                        root = merge(root, start);
                        firstL = getKth(fin, 1);
                        lh = fin;
                    } else {
                        // cut from right
                        int k = getFirstGreater(rh, firstL);
                        assert(k >= 1);
                        auto [start, fin] = split(rh, k);
                        root = merge(root, start);
                        firstR = getKth(fin, 1);
                        rh = fin;
                    }
                    turn = -turn;
                }
                if (firstL == 0) {
                    root = merge(root, rh);
                } else {
                    root = merge(root, lh);
                }
            }
            ++t;
            /*cerr << "t: " << t << '\n';
            dfs(root);
            cerr << '\n';*/
        }
        ans[query.i] = getKth(root, query.pos);
    }
    for (int i = 0; i < q; ++i) {
        cout << ans[i] << '\n';
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int T = 1;
    cin >> T;
    while (T--) {
        solve();
        cerr << "-----------\n";
        cout << "-----------\n";
    }
#else
    int T = 1;
    // cin >> T;
    while (T--) {
        solve();
    }
#endif

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 285 ms 32308 KB Output is correct
2 Correct 277 ms 32040 KB Output is correct
3 Correct 279 ms 31016 KB Output is correct
4 Correct 244 ms 29776 KB Output is correct
5 Correct 267 ms 31636 KB Output is correct
6 Correct 253 ms 30196 KB Output is correct
7 Correct 270 ms 31824 KB Output is correct
8 Correct 260 ms 30236 KB Output is correct
9 Correct 252 ms 29848 KB Output is correct
10 Correct 255 ms 30332 KB Output is correct
11 Correct 255 ms 30288 KB Output is correct
12 Correct 240 ms 29264 KB Output is correct
13 Correct 248 ms 30296 KB Output is correct
14 Correct 266 ms 31056 KB Output is correct
15 Correct 261 ms 30876 KB Output is correct
16 Correct 2 ms 5208 KB Output is correct
17 Correct 226 ms 29672 KB Output is correct
18 Correct 230 ms 29312 KB Output is correct
19 Correct 2 ms 4956 KB Output is correct
20 Correct 3 ms 4956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 543 ms 41624 KB Output is correct
2 Correct 409 ms 40708 KB Output is correct
3 Correct 316 ms 32868 KB Output is correct
4 Correct 328 ms 33100 KB Output is correct
5 Correct 379 ms 33704 KB Output is correct
6 Correct 306 ms 32512 KB Output is correct
7 Correct 372 ms 40528 KB Output is correct
8 Correct 359 ms 39012 KB Output is correct
9 Correct 345 ms 33808 KB Output is correct
10 Correct 332 ms 37888 KB Output is correct
11 Correct 303 ms 31828 KB Output is correct
12 Correct 274 ms 31808 KB Output is correct
13 Correct 340 ms 37124 KB Output is correct
14 Correct 278 ms 32592 KB Output is correct
15 Correct 332 ms 38732 KB Output is correct
16 Correct 34 ms 6228 KB Output is correct
17 Correct 297 ms 35412 KB Output is correct
18 Correct 274 ms 29624 KB Output is correct
19 Correct 89 ms 12120 KB Output is correct
20 Correct 103 ms 13652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 184 ms 9044 KB Output is correct
2 Correct 97 ms 8932 KB Output is correct
3 Correct 116 ms 8784 KB Output is correct
4 Correct 78 ms 8744 KB Output is correct
5 Correct 126 ms 8940 KB Output is correct
6 Correct 88 ms 8600 KB Output is correct
7 Correct 112 ms 8936 KB Output is correct
8 Correct 102 ms 8804 KB Output is correct
9 Correct 120 ms 8840 KB Output is correct
10 Correct 66 ms 8708 KB Output is correct
11 Correct 70 ms 8528 KB Output is correct
12 Correct 64 ms 8620 KB Output is correct
13 Correct 67 ms 8532 KB Output is correct
14 Correct 67 ms 8752 KB Output is correct
15 Correct 66 ms 8528 KB Output is correct
16 Correct 20 ms 5728 KB Output is correct
17 Correct 54 ms 8536 KB Output is correct
18 Correct 58 ms 8484 KB Output is correct
19 Correct 2 ms 4956 KB Output is correct
20 Correct 1 ms 4960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 285 ms 32308 KB Output is correct
2 Correct 277 ms 32040 KB Output is correct
3 Correct 279 ms 31016 KB Output is correct
4 Correct 244 ms 29776 KB Output is correct
5 Correct 267 ms 31636 KB Output is correct
6 Correct 253 ms 30196 KB Output is correct
7 Correct 270 ms 31824 KB Output is correct
8 Correct 260 ms 30236 KB Output is correct
9 Correct 252 ms 29848 KB Output is correct
10 Correct 255 ms 30332 KB Output is correct
11 Correct 255 ms 30288 KB Output is correct
12 Correct 240 ms 29264 KB Output is correct
13 Correct 248 ms 30296 KB Output is correct
14 Correct 266 ms 31056 KB Output is correct
15 Correct 261 ms 30876 KB Output is correct
16 Correct 2 ms 5208 KB Output is correct
17 Correct 226 ms 29672 KB Output is correct
18 Correct 230 ms 29312 KB Output is correct
19 Correct 2 ms 4956 KB Output is correct
20 Correct 3 ms 4956 KB Output is correct
21 Correct 543 ms 41624 KB Output is correct
22 Correct 409 ms 40708 KB Output is correct
23 Correct 316 ms 32868 KB Output is correct
24 Correct 328 ms 33100 KB Output is correct
25 Correct 379 ms 33704 KB Output is correct
26 Correct 306 ms 32512 KB Output is correct
27 Correct 372 ms 40528 KB Output is correct
28 Correct 359 ms 39012 KB Output is correct
29 Correct 345 ms 33808 KB Output is correct
30 Correct 332 ms 37888 KB Output is correct
31 Correct 303 ms 31828 KB Output is correct
32 Correct 274 ms 31808 KB Output is correct
33 Correct 340 ms 37124 KB Output is correct
34 Correct 278 ms 32592 KB Output is correct
35 Correct 332 ms 38732 KB Output is correct
36 Correct 34 ms 6228 KB Output is correct
37 Correct 297 ms 35412 KB Output is correct
38 Correct 274 ms 29624 KB Output is correct
39 Correct 89 ms 12120 KB Output is correct
40 Correct 103 ms 13652 KB Output is correct
41 Correct 184 ms 9044 KB Output is correct
42 Correct 97 ms 8932 KB Output is correct
43 Correct 116 ms 8784 KB Output is correct
44 Correct 78 ms 8744 KB Output is correct
45 Correct 126 ms 8940 KB Output is correct
46 Correct 88 ms 8600 KB Output is correct
47 Correct 112 ms 8936 KB Output is correct
48 Correct 102 ms 8804 KB Output is correct
49 Correct 120 ms 8840 KB Output is correct
50 Correct 66 ms 8708 KB Output is correct
51 Correct 70 ms 8528 KB Output is correct
52 Correct 64 ms 8620 KB Output is correct
53 Correct 67 ms 8532 KB Output is correct
54 Correct 67 ms 8752 KB Output is correct
55 Correct 66 ms 8528 KB Output is correct
56 Correct 20 ms 5728 KB Output is correct
57 Correct 54 ms 8536 KB Output is correct
58 Correct 58 ms 8484 KB Output is correct
59 Correct 2 ms 4956 KB Output is correct
60 Correct 1 ms 4960 KB Output is correct
61 Correct 881 ms 41160 KB Output is correct
62 Correct 664 ms 40396 KB Output is correct
63 Correct 676 ms 38936 KB Output is correct
64 Correct 668 ms 38740 KB Output is correct
65 Correct 673 ms 40200 KB Output is correct
66 Correct 662 ms 38968 KB Output is correct
67 Correct 616 ms 39984 KB Output is correct
68 Correct 625 ms 38740 KB Output is correct
69 Correct 605 ms 38656 KB Output is correct
70 Correct 553 ms 38340 KB Output is correct
71 Correct 500 ms 37352 KB Output is correct
72 Correct 542 ms 38004 KB Output is correct
73 Correct 533 ms 37832 KB Output is correct
74 Correct 576 ms 39044 KB Output is correct
75 Correct 532 ms 39000 KB Output is correct
76 Correct 40 ms 6392 KB Output is correct
77 Correct 464 ms 35664 KB Output is correct
78 Correct 490 ms 35552 KB Output is correct
79 Correct 2 ms 4952 KB Output is correct
80 Correct 1 ms 4956 KB Output is correct