답안 #725799

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
725799 2023-04-18T05:41:44 Z tengiz05 Abracadabra (CEOI22_abracadabra) C++17
100 / 100
1964 ms 50272 KB
#include <bits/stdc++.h>

using i64 = long long;

constexpr int N = 200000;
constexpr int B = 300;

struct DataStructure {
    int n;
    std::vector<int> a;
    std::vector<int> nxt;
    std::vector<std::pair<int, int>> blocks;
    std::deque<std::pair<int, int>> d[N / B + 2];
    int siz[N / B + 2];
    bool FirstTime;
    DataStructure(std::vector<int> a, std::vector<int> nxt, std::vector<std::pair<int, int>> blocks) : n(a.size()), a(a), nxt(nxt), blocks(blocks) {
        FirstTime = true;
        memset(siz, 0, sizeof siz);
    }
    int at(int p) {
        if (FirstTime) {
            return a[p];
        }
        int cursiz = 0;
        for (int cur = 0; ; cur++) {
            if (cursiz + siz[cur] > p) {
                int id = 0;
                for (auto [l, r] : d[cur]) {
                    cursiz += r - l;
                    if (cursiz > p) {
                        cursiz -= r - l;
                        return a[l + p - cursiz];
                    }
                    id++;
                }
            }
            cursiz += siz[cur];
        }
    }
    void insert(int L, int R) {
        //~ std::cerr << "Divided: " << L << " " << R << "\n";
        for (int cur = 0; ; cur++) {
            if (d[cur + 1].empty() || a[d[cur + 1][0].first] > a[L]) {
                int k = d[cur].size();
                for (int i = 0; i <= k; i++) {
                    if (i == k || a[d[cur][i].first] > a[L]) {
                        d[cur].insert(d[cur].begin() + i, {L, R});
                        siz[cur] += R - L;
                        break;
                    }
                }
                while (d[cur].size() > B) {
                    int s = d[cur].back().second - d[cur].back().first;
                    siz[cur + 1] += s;
                    d[cur + 1].push_front(d[cur].back());
                    siz[cur] -= s;
                    d[cur].pop_back();
                    cur++;
                }
                break;
            }
        }
    }
    void nextShuffle() {
        if (FirstTime) {
            std::sort(blocks.begin(), blocks.end(), [&](const auto &i, const auto &j) {
                return a[i.first] < a[j.first];
            });
            int cur = 0;
            for (auto [l, r] : blocks) {
                d[cur].push_back({l, r});
                siz[cur] += r - l;
                if (d[cur].size() == B) {
                    cur++;
                }
            }
            FirstTime = false;
            return;
        }
        int cursiz = 0;
        int L = -1, R = -1;
        int savecur = -1, saveid = -1;
        for (int cur = 0; ; cur++) {
            if (cursiz + siz[cur] > n / 2) {
                int id = 0;
                for (auto [l, r] : d[cur]) {
                    cursiz += r - l;
                    if (cursiz > n / 2) {
                        L = l;
                        R = r;
                        savecur = cur;
                        saveid = id;
                        break;
                    }
                    id++;
                }
                break;
            }
            cursiz += siz[cur];
        }
        //~ std::cerr << L << " " << R << "::\n";
        cursiz -= R - L;
        if (cursiz == n / 2) return;
        int mid = n / 2 - cursiz + L;
        // removal of the midBlock
        siz[savecur] -= R - L;
        d[savecur].erase(d[savecur].begin() + saveid);
        for (int i = savecur + 1; !d[i].empty(); i++) {
            //~ std::cerr << "Red flag\n";
            int s = d[i].front().second - d[i].front().first;
            siz[i - 1] += s;
            d[i - 1].push_back(d[i].front());
            siz[i] -= s;
            d[i].pop_front();
        }
        // division of the midBlock
        for (int i = L; i < mid; i = nxt[i]) {
            insert(i, std::min(mid, nxt[i]));
        }
        for (int i = mid; i < R; i = nxt[i]) {
            insert(i, std::min(R, nxt[i]));
        }
        //~ for (auto [l, r] : d[0]) {
            //~ std::cout << l << " " << r << "\n";
        //~ }
    }
};

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n, q;
    std::cin >> n >> q;
    
    std::vector<int> a(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
        a[i]--;
    }
    
    std::vector<std::vector<std::pair<int, int>>> Q(n + 1);
    std::vector<int> ans(q);
    for (int i = 0; i < q; i++) {
        int t, p;
        std::cin >> t >> p;
        t = std::min(t, n);
        p--;
        Q[t].push_back({p, i});
    }
    
    std::vector<int> nxt(n);
    std::vector<int> st{n};
    for (int i = n - 1; i >= 0; i--) {
        while (st.back() < n && a[st.back()] < a[i]) {
            st.pop_back();
        }
        nxt[i] = st.back();
        st.push_back(i);
    }
    
    std::vector<std::pair<int, int>> blocks;
    for (int i = 0; i < n / 2; i = nxt[i]) {
        blocks.push_back({i, std::min(n / 2, nxt[i])});
    }
    for (int i = n / 2; i < n; i = nxt[i]) {
        blocks.push_back({i, nxt[i]});
    }
    DataStructure D(a, nxt, blocks);
    for (int t = 0; t <= n; t++) {
        //~ std::cerr << "Stage " << t << ";\n";
        for (auto [p, id] : Q[t]) {
            ans[id] = D.at(p);
        }
        D.nextShuffle();
    }
    
    for (int i = 0; i < q; i++) {
        std::cout << ans[i] + 1 << "\n";
    }
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 298 ms 18360 KB Output is correct
2 Correct 354 ms 17276 KB Output is correct
3 Correct 343 ms 16896 KB Output is correct
4 Correct 205 ms 16452 KB Output is correct
5 Correct 286 ms 18900 KB Output is correct
6 Correct 238 ms 19628 KB Output is correct
7 Correct 321 ms 20044 KB Output is correct
8 Correct 269 ms 18068 KB Output is correct
9 Correct 244 ms 17416 KB Output is correct
10 Correct 270 ms 16756 KB Output is correct
11 Correct 290 ms 17176 KB Output is correct
12 Correct 189 ms 15760 KB Output is correct
13 Correct 220 ms 16364 KB Output is correct
14 Correct 273 ms 18856 KB Output is correct
15 Correct 248 ms 16884 KB Output is correct
16 Correct 1 ms 848 KB Output is correct
17 Correct 183 ms 15956 KB Output is correct
18 Correct 183 ms 15828 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 1 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1801 ms 31580 KB Output is correct
2 Correct 1177 ms 45992 KB Output is correct
3 Correct 1094 ms 37984 KB Output is correct
4 Correct 571 ms 34860 KB Output is correct
5 Correct 661 ms 35564 KB Output is correct
6 Correct 447 ms 34228 KB Output is correct
7 Correct 495 ms 41680 KB Output is correct
8 Correct 584 ms 39840 KB Output is correct
9 Correct 562 ms 35456 KB Output is correct
10 Correct 501 ms 38776 KB Output is correct
11 Correct 322 ms 33660 KB Output is correct
12 Correct 358 ms 33452 KB Output is correct
13 Correct 533 ms 38120 KB Output is correct
14 Correct 432 ms 34568 KB Output is correct
15 Correct 440 ms 39788 KB Output is correct
16 Correct 61 ms 11340 KB Output is correct
17 Correct 311 ms 41932 KB Output is correct
18 Correct 213 ms 31336 KB Output is correct
19 Correct 99 ms 16396 KB Output is correct
20 Correct 113 ms 17664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 394 ms 9640 KB Output is correct
2 Correct 227 ms 11272 KB Output is correct
3 Correct 275 ms 10552 KB Output is correct
4 Correct 89 ms 8552 KB Output is correct
5 Correct 155 ms 9348 KB Output is correct
6 Correct 108 ms 8520 KB Output is correct
7 Correct 136 ms 9164 KB Output is correct
8 Correct 131 ms 8672 KB Output is correct
9 Correct 145 ms 9112 KB Output is correct
10 Correct 92 ms 8160 KB Output is correct
11 Correct 93 ms 8532 KB Output is correct
12 Correct 73 ms 8268 KB Output is correct
13 Correct 93 ms 8216 KB Output is correct
14 Correct 87 ms 8592 KB Output is correct
15 Correct 51 ms 8212 KB Output is correct
16 Correct 23 ms 5972 KB Output is correct
17 Correct 51 ms 10332 KB Output is correct
18 Correct 38 ms 7656 KB Output is correct
19 Correct 1 ms 756 KB Output is correct
20 Correct 1 ms 752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 298 ms 18360 KB Output is correct
2 Correct 354 ms 17276 KB Output is correct
3 Correct 343 ms 16896 KB Output is correct
4 Correct 205 ms 16452 KB Output is correct
5 Correct 286 ms 18900 KB Output is correct
6 Correct 238 ms 19628 KB Output is correct
7 Correct 321 ms 20044 KB Output is correct
8 Correct 269 ms 18068 KB Output is correct
9 Correct 244 ms 17416 KB Output is correct
10 Correct 270 ms 16756 KB Output is correct
11 Correct 290 ms 17176 KB Output is correct
12 Correct 189 ms 15760 KB Output is correct
13 Correct 220 ms 16364 KB Output is correct
14 Correct 273 ms 18856 KB Output is correct
15 Correct 248 ms 16884 KB Output is correct
16 Correct 1 ms 848 KB Output is correct
17 Correct 183 ms 15956 KB Output is correct
18 Correct 183 ms 15828 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 1 ms 724 KB Output is correct
21 Correct 1801 ms 31580 KB Output is correct
22 Correct 1177 ms 45992 KB Output is correct
23 Correct 1094 ms 37984 KB Output is correct
24 Correct 571 ms 34860 KB Output is correct
25 Correct 661 ms 35564 KB Output is correct
26 Correct 447 ms 34228 KB Output is correct
27 Correct 495 ms 41680 KB Output is correct
28 Correct 584 ms 39840 KB Output is correct
29 Correct 562 ms 35456 KB Output is correct
30 Correct 501 ms 38776 KB Output is correct
31 Correct 322 ms 33660 KB Output is correct
32 Correct 358 ms 33452 KB Output is correct
33 Correct 533 ms 38120 KB Output is correct
34 Correct 432 ms 34568 KB Output is correct
35 Correct 440 ms 39788 KB Output is correct
36 Correct 61 ms 11340 KB Output is correct
37 Correct 311 ms 41932 KB Output is correct
38 Correct 213 ms 31336 KB Output is correct
39 Correct 99 ms 16396 KB Output is correct
40 Correct 113 ms 17664 KB Output is correct
41 Correct 394 ms 9640 KB Output is correct
42 Correct 227 ms 11272 KB Output is correct
43 Correct 275 ms 10552 KB Output is correct
44 Correct 89 ms 8552 KB Output is correct
45 Correct 155 ms 9348 KB Output is correct
46 Correct 108 ms 8520 KB Output is correct
47 Correct 136 ms 9164 KB Output is correct
48 Correct 131 ms 8672 KB Output is correct
49 Correct 145 ms 9112 KB Output is correct
50 Correct 92 ms 8160 KB Output is correct
51 Correct 93 ms 8532 KB Output is correct
52 Correct 73 ms 8268 KB Output is correct
53 Correct 93 ms 8216 KB Output is correct
54 Correct 87 ms 8592 KB Output is correct
55 Correct 51 ms 8212 KB Output is correct
56 Correct 23 ms 5972 KB Output is correct
57 Correct 51 ms 10332 KB Output is correct
58 Correct 38 ms 7656 KB Output is correct
59 Correct 1 ms 756 KB Output is correct
60 Correct 1 ms 752 KB Output is correct
61 Correct 1964 ms 49260 KB Output is correct
62 Correct 1424 ms 50272 KB Output is correct
63 Correct 1533 ms 47724 KB Output is correct
64 Correct 757 ms 44488 KB Output is correct
65 Correct 809 ms 46360 KB Output is correct
66 Correct 764 ms 44340 KB Output is correct
67 Correct 679 ms 43248 KB Output is correct
68 Correct 626 ms 41632 KB Output is correct
69 Correct 672 ms 44764 KB Output is correct
70 Correct 544 ms 39824 KB Output is correct
71 Correct 430 ms 41312 KB Output is correct
72 Correct 595 ms 40472 KB Output is correct
73 Correct 470 ms 40940 KB Output is correct
74 Correct 510 ms 42812 KB Output is correct
75 Correct 546 ms 41784 KB Output is correct
76 Correct 59 ms 11212 KB Output is correct
77 Correct 317 ms 42012 KB Output is correct
78 Correct 240 ms 36404 KB Output is correct
79 Correct 1 ms 724 KB Output is correct
80 Correct 1 ms 724 KB Output is correct