답안 #752804

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
752804 2023-06-03T22:16:06 Z math_rabbit_1028 Abracadabra (CEOI22_abracadabra) C++14
100 / 100
817 ms 49140 KB
#include <bits/stdc++.h>
using namespace std;
int n, q, arr[202020];
 
int ans[1010101];
int res[202020];
int nxt[202020];
stack< pair<int, int> > S;
 
int qnum = 1;
struct query {
    int time, idx, ord;
    bool operator<(const query &other) const {
        if (time == other.time) return idx < other.idx;
        return time < other.time;
    }
} queries[1010101];
 
struct segment {
    int first, st, ed, sz;
    bool operator<(const segment &other) const {
        return first > other.first;
    }
};
set<segment> segs;

struct segtree {
    int tree[808080];
    void update(int v, int st, int ed, int idx, int val) {
        if (st > idx) return;
        if (ed < idx) return;
        if (st == ed) {
            tree[v] = val;
            return;
        }
        int mid = (st + ed) / 2;
        update(2 * v, st, mid, idx, val);
        update(2 * v + 1, mid + 1, ed, idx, val);
        tree[v] = tree[2 * v] + tree[2 * v + 1];
    } 
    pair<int, int> get(int v, int st, int ed, int val) {
        if (st == ed) return {st, val - tree[v]};
        int mid = (st + ed) / 2;
        if (tree[2 * v] < val) return get(2 * v + 1, mid + 1, ed, val - tree[2 * v]);
        else return get(2 * v, st, mid, val);
    }
} sumtree;
 
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
 
    cin >> n >> q;
    for (int i = 1; i <= n; i++) cin >> arr[i];
 
    for (int i = 1; i <= q; i++) {
        cin >> queries[i].time >> queries[i].idx;
        queries[i].ord = i;
        queries[i].time = min(queries[i].time, n);
    }
    sort(queries + 1, queries + q + 1);
 
    while (qnum <= q) {
        if (queries[qnum].time > 0) break;
        ans[queries[qnum].ord] = arr[queries[qnum].idx];
        qnum++;
    }
 
    for (int i = 1; i <= n; i++) {
        while (!S.empty()) {
            if (S.top().second < arr[i]) {
                nxt[S.top().first] = i;
                S.pop();
            }
            else break;
        }
        S.push({i, arr[i]});
    }
    while (!S.empty()) {
        nxt[S.top().first] = n + 1;
        S.pop();
    }
 
    int k = 1;
    while (nxt[k] <= n/2) {
        segs.insert({arr[k], k, nxt[k] - 1, nxt[k] - k});
        sumtree.update(1, 1, n, arr[k], nxt[k] - k);
        k = nxt[k];
    }
    segs.insert({arr[k], k, n/2, n/2 - k + 1});
    sumtree.update(1, 1, n, arr[k], n/2 - k + 1);
 
    k = n/2 + 1;
    while (nxt[k] <= n) {
        segs.insert({arr[k], k, nxt[k] - 1, nxt[k] - k});
        sumtree.update(1, 1, n, arr[k], nxt[k] - k);
        k = nxt[k];
    }
    segs.insert({arr[k], k, n, n - k + 1});
    sumtree.update(1, 1, n, arr[k], n - k + 1);
 
    int cnt = 0, p = n;
    for (int t = 1; t <= queries[q].time; t++) {
        while (qnum <= q) {
            if (queries[qnum].time > t) break;
            if (res[queries[qnum].idx] > 0) ans[queries[qnum].ord] = res[queries[qnum].idx];
            else {
                pair<int, int> val = sumtree.get(1, 1, n, queries[qnum].idx);
                assert(segs.find({val.first, 0, 0, 0}) != segs.end());
                ans[queries[qnum].ord] = arr[segs.find({val.first, 0, 0, 0})->ed + val.second];
            }
            qnum++;
        }
        while (1) {
            if (cnt + segs.begin()->sz > n/2) break;
            cnt += segs.begin()->sz;
            for (int i = segs.begin()->ed; i >= segs.begin()->st; i--) {
                res[p--] = arr[i];
            }
            sumtree.update(1, 1, n, segs.begin()->first, 0);
            segs.erase(segs.begin());
        }
        if (cnt == n/2) continue;
        segment div = *segs.begin();
        segs.erase(segs.begin());
        segs.insert({div.first, div.st, div.st + cnt + div.sz - n/2 - 1, cnt + div.sz - n/2});
        sumtree.update(1, 1, n, div.first, cnt + div.sz - n/2);
        k = div.st + cnt + div.sz - n/2;
        while (nxt[k] <= div.ed) {
            segs.insert({arr[k], k, nxt[k] - 1, nxt[k] - k});
            sumtree.update(1, 1, n, arr[k], nxt[k] - k);
            k = nxt[k];
        }
        segs.insert({arr[k], k, div.ed, div.ed - k + 1});
        sumtree.update(1, 1, n, arr[k], div.ed - k + 1);
    }
 
    for (int i = 1; i <= q; i++) cout << ans[i] << "\n";
 
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 327 ms 19940 KB Output is correct
2 Correct 335 ms 27260 KB Output is correct
3 Correct 324 ms 26316 KB Output is correct
4 Correct 278 ms 25032 KB Output is correct
5 Correct 316 ms 26912 KB Output is correct
6 Correct 286 ms 25416 KB Output is correct
7 Correct 317 ms 27056 KB Output is correct
8 Correct 332 ms 25516 KB Output is correct
9 Correct 293 ms 25132 KB Output is correct
10 Correct 301 ms 25660 KB Output is correct
11 Correct 285 ms 25676 KB Output is correct
12 Correct 257 ms 24476 KB Output is correct
13 Correct 288 ms 25424 KB Output is correct
14 Correct 299 ms 26216 KB Output is correct
15 Correct 309 ms 26164 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 241 ms 25016 KB Output is correct
18 Correct 261 ms 24656 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 441 ms 29460 KB Output is correct
2 Correct 422 ms 35864 KB Output is correct
3 Correct 394 ms 26944 KB Output is correct
4 Correct 288 ms 23844 KB Output is correct
5 Correct 319 ms 25240 KB Output is correct
6 Correct 277 ms 22756 KB Output is correct
7 Correct 325 ms 27876 KB Output is correct
8 Correct 321 ms 27084 KB Output is correct
9 Correct 294 ms 24524 KB Output is correct
10 Correct 292 ms 25688 KB Output is correct
11 Correct 262 ms 21824 KB Output is correct
12 Correct 264 ms 21688 KB Output is correct
13 Correct 297 ms 25548 KB Output is correct
14 Correct 261 ms 22092 KB Output is correct
15 Correct 310 ms 26016 KB Output is correct
16 Correct 18 ms 3540 KB Output is correct
17 Correct 314 ms 37176 KB Output is correct
18 Correct 236 ms 20924 KB Output is correct
19 Correct 60 ms 6348 KB Output is correct
20 Correct 83 ms 8712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 7628 KB Output is correct
2 Correct 93 ms 9104 KB Output is correct
3 Correct 109 ms 7384 KB Output is correct
4 Correct 49 ms 6608 KB Output is correct
5 Correct 76 ms 8976 KB Output is correct
6 Correct 52 ms 6820 KB Output is correct
7 Correct 68 ms 8060 KB Output is correct
8 Correct 62 ms 7500 KB Output is correct
9 Correct 69 ms 8204 KB Output is correct
10 Correct 47 ms 5904 KB Output is correct
11 Correct 49 ms 6208 KB Output is correct
12 Correct 46 ms 5880 KB Output is correct
13 Correct 48 ms 6116 KB Output is correct
14 Correct 46 ms 6092 KB Output is correct
15 Correct 44 ms 5800 KB Output is correct
16 Correct 11 ms 3084 KB Output is correct
17 Correct 69 ms 11608 KB Output is correct
18 Correct 39 ms 5452 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 327 ms 19940 KB Output is correct
2 Correct 335 ms 27260 KB Output is correct
3 Correct 324 ms 26316 KB Output is correct
4 Correct 278 ms 25032 KB Output is correct
5 Correct 316 ms 26912 KB Output is correct
6 Correct 286 ms 25416 KB Output is correct
7 Correct 317 ms 27056 KB Output is correct
8 Correct 332 ms 25516 KB Output is correct
9 Correct 293 ms 25132 KB Output is correct
10 Correct 301 ms 25660 KB Output is correct
11 Correct 285 ms 25676 KB Output is correct
12 Correct 257 ms 24476 KB Output is correct
13 Correct 288 ms 25424 KB Output is correct
14 Correct 299 ms 26216 KB Output is correct
15 Correct 309 ms 26164 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 241 ms 25016 KB Output is correct
18 Correct 261 ms 24656 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 441 ms 29460 KB Output is correct
22 Correct 422 ms 35864 KB Output is correct
23 Correct 394 ms 26944 KB Output is correct
24 Correct 288 ms 23844 KB Output is correct
25 Correct 319 ms 25240 KB Output is correct
26 Correct 277 ms 22756 KB Output is correct
27 Correct 325 ms 27876 KB Output is correct
28 Correct 321 ms 27084 KB Output is correct
29 Correct 294 ms 24524 KB Output is correct
30 Correct 292 ms 25688 KB Output is correct
31 Correct 262 ms 21824 KB Output is correct
32 Correct 264 ms 21688 KB Output is correct
33 Correct 297 ms 25548 KB Output is correct
34 Correct 261 ms 22092 KB Output is correct
35 Correct 310 ms 26016 KB Output is correct
36 Correct 18 ms 3540 KB Output is correct
37 Correct 314 ms 37176 KB Output is correct
38 Correct 236 ms 20924 KB Output is correct
39 Correct 60 ms 6348 KB Output is correct
40 Correct 83 ms 8712 KB Output is correct
41 Correct 89 ms 7628 KB Output is correct
42 Correct 93 ms 9104 KB Output is correct
43 Correct 109 ms 7384 KB Output is correct
44 Correct 49 ms 6608 KB Output is correct
45 Correct 76 ms 8976 KB Output is correct
46 Correct 52 ms 6820 KB Output is correct
47 Correct 68 ms 8060 KB Output is correct
48 Correct 62 ms 7500 KB Output is correct
49 Correct 69 ms 8204 KB Output is correct
50 Correct 47 ms 5904 KB Output is correct
51 Correct 49 ms 6208 KB Output is correct
52 Correct 46 ms 5880 KB Output is correct
53 Correct 48 ms 6116 KB Output is correct
54 Correct 46 ms 6092 KB Output is correct
55 Correct 44 ms 5800 KB Output is correct
56 Correct 11 ms 3084 KB Output is correct
57 Correct 69 ms 11608 KB Output is correct
58 Correct 39 ms 5452 KB Output is correct
59 Correct 0 ms 340 KB Output is correct
60 Correct 1 ms 344 KB Output is correct
61 Correct 587 ms 46592 KB Output is correct
62 Correct 653 ms 49140 KB Output is correct
63 Correct 817 ms 44420 KB Output is correct
64 Correct 467 ms 41056 KB Output is correct
65 Correct 492 ms 43020 KB Output is correct
66 Correct 490 ms 41160 KB Output is correct
67 Correct 443 ms 40780 KB Output is correct
68 Correct 486 ms 40160 KB Output is correct
69 Correct 511 ms 40604 KB Output is correct
70 Correct 447 ms 37936 KB Output is correct
71 Correct 389 ms 36616 KB Output is correct
72 Correct 415 ms 37772 KB Output is correct
73 Correct 402 ms 37216 KB Output is correct
74 Correct 421 ms 38532 KB Output is correct
75 Correct 426 ms 38436 KB Output is correct
76 Correct 19 ms 4812 KB Output is correct
77 Correct 388 ms 46868 KB Output is correct
78 Correct 346 ms 34280 KB Output is correct
79 Correct 0 ms 340 KB Output is correct
80 Correct 0 ms 340 KB Output is correct