답안 #683100

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
683100 2023-01-17T16:53:35 Z nutella Abracadabra (CEOI22_abracadabra) C++17
100 / 100
715 ms 56036 KB
#include <bits/stdc++.h>

using namespace std;

void riffle_shuffle(vector<int> &a) {
    vector<int> L(a.begin(), a.begin() + a.size() / 2);
    vector<int> R(a.begin() + a.size() / 2, a.end());

    int i = 0, j = 0;
    int n = a.size() / 2;

    while (i < n || j < n) {
        if (i != n && (j == n || L[i] < R[j])) {
            a[j + i] = L[i];
            i += 1;
        } else {
            a[j + i] = R[j];
            j += 1;
        }
    }
}

struct Fenwick {
    vector<int> t;
    int n, S = 0;

    Fenwick() = default;

    Fenwick(int n) : n(n), t(n + 1) {}

    void modify(int i, int v) {
        S += v;
        for (int x = i + 1; x <= n; x += x & -x) {
            t[x] += v;
        }
    }

    int sum(int i) {
        int ans = 0;
        for (int x = i + 1; x > 0; x -= x & -x) {
            ans += t[x];
        }
        return ans;
    }

    int lower_bound(int k) {
        int x = 0;
        for (int i = 1 << __lg(n); i > 0; i >>= 1) {
            if (x + i <= n && t[x + i] < k) {
                x += i;
                k -= t[x];
            }
        }
        return x;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, q;
    cin >> n >> q;


    vector<int> a(n);

    for (int &x: a) {
        cin >> x;
        x -= 1;
    }

    vector<int> ans(q, -1);

    vector<vector<array<int, 2>>> queries(n + 1);

    for (int i = 0; i < q; ++i) {
        int t, p;
        cin >> t >> p;

        p -= 1;

        if (t == 0) {
            ans[i] = a[p];
            continue;
        }

        queries[min(t, n)].push_back({i, p});
    }

    const int logn = __lg(n) + 1;

    auto comp = [&](int i, int j) {
        return a[i] > a[j] ? i : j;
    };

    vector<vector<int>> mx(logn);

    mx[0].resize(n);
    iota(mx[0].begin(), mx[0].end(), 0);

    for (int l = 1; l < logn; ++l) {
        mx[l].resize(n - (1 << l) + 1);

        for (int i = 0; i + (1 << l) <= n; ++i) {
            mx[l][i] = comp(mx[l - 1][i], mx[l - 1][i + (1 << l - 1)]);
        }
    }

    auto rangeMax = [&](int l, int r) {
        int lg = __lg(r - l);

        return comp(mx[lg][l], mx[lg][r - (1 << lg)]);
    };

    Fenwick fn(n);

    vector<pair<int, int>> segment(n, {-1, -1});

    int pref_mx = -1;
    for (int i = 0; i < n / 2; ++i) {
        if (pref_mx < a[i]) {
            if (pref_mx != -1) {
                segment[pref_mx].second = i;
            }
            pref_mx = a[i];
            segment[pref_mx].first = i;
        }
    }
    segment[pref_mx].second = n / 2;

    pref_mx = -1;
    for (int i = n / 2; i < n; ++i) {
        if (pref_mx < a[i]) {
            if (pref_mx != -1) {
                segment[pref_mx].second = i;
            }
            pref_mx = a[i];
            segment[pref_mx].first = i;
        }
    }
    segment[pref_mx].second = n;

    for (int x = 0; x < n; ++x) {
        if (segment[x].first != -1) {
            fn.modify(x, segment[x].second - segment[x].first);
        }
    }

    auto find_next = [&](int l, int r) {
        int mx = rangeMax(l, r);
        if (l == mx) {
            return -1;
        }

        int lo = l, hi = r;
        while (lo + 1 < hi) {
            int mid = (lo + hi) >> 1;

            if (rangeMax(l, mid + 1) == l) {
                lo = mid;
            } else {
                hi = mid;
            }
        }

        return hi;
    };

    auto getValue = [&](int p) {
        p += 1;

        int x = fn.lower_bound(p);
        int sum_l = fn.sum(x - 1);

        return a[segment[x].first + (p - sum_l) - 1];
    };

    for (int _ = 1; _ <= n; ++_) {
        for (auto [i, p]: queries[_]) {
            ans[i] = getValue(p);
        }

        int x = fn.lower_bound(n / 2);

        int sum = fn.sum(x);
        
        if (sum == n / 2) {
            continue;
        }
        
        int sum_l = sum - (segment[x].second - segment[x].first);

        int cut = segment[x].first + (n / 2 - sum_l);

        fn.modify(x, cut - segment[x].second);


        int l = cut, r = segment[x].second;

        segment[x].second = cut;

        while (l < r) {
            int mid = find_next(l, r);

            if (mid == -1) {
                segment[a[l]].first = l, segment[a[l]].second = r;
            } else {
                segment[a[l]].first = l, segment[a[l]].second = mid;
            }

            fn.modify(a[l], segment[a[l]].second - segment[a[l]].first);

            l = segment[a[l]].second;
        }

    }

    for (int i = 0; i < q; ++i) {
        cout << ans[i] + 1 << '\n';
    }

    return 0;
}

Compilation message

Main.cpp: In constructor 'Fenwick::Fenwick(int)':
Main.cpp:25:9: warning: 'Fenwick::n' will be initialized after [-Wreorder]
   25 |     int n, S = 0;
      |         ^
Main.cpp:24:17: warning:   'std::vector<int> Fenwick::t' [-Wreorder]
   24 |     vector<int> t;
      |                 ^
Main.cpp:29:5: warning:   when initialized here [-Wreorder]
   29 |     Fenwick(int n) : n(n), t(n + 1) {}
      |     ^~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:106:65: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
  106 |             mx[l][i] = comp(mx[l - 1][i], mx[l - 1][i + (1 << l - 1)]);
      |                                                               ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 239 ms 26000 KB Output is correct
2 Correct 260 ms 24444 KB Output is correct
3 Correct 248 ms 23888 KB Output is correct
4 Correct 196 ms 21536 KB Output is correct
5 Correct 240 ms 25900 KB Output is correct
6 Correct 207 ms 25720 KB Output is correct
7 Correct 224 ms 27204 KB Output is correct
8 Correct 212 ms 24200 KB Output is correct
9 Correct 211 ms 22856 KB Output is correct
10 Correct 213 ms 22860 KB Output is correct
11 Correct 201 ms 23304 KB Output is correct
12 Correct 203 ms 19468 KB Output is correct
13 Correct 217 ms 21776 KB Output is correct
14 Correct 232 ms 24928 KB Output is correct
15 Correct 222 ms 22096 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 159 ms 13636 KB Output is correct
18 Correct 214 ms 17364 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 355 ms 45756 KB Output is correct
2 Correct 320 ms 47420 KB Output is correct
3 Correct 266 ms 42856 KB Output is correct
4 Correct 241 ms 43160 KB Output is correct
5 Correct 277 ms 43892 KB Output is correct
6 Correct 244 ms 42608 KB Output is correct
7 Correct 304 ms 47408 KB Output is correct
8 Correct 304 ms 45876 KB Output is correct
9 Correct 269 ms 43820 KB Output is correct
10 Correct 307 ms 45436 KB Output is correct
11 Correct 260 ms 43788 KB Output is correct
12 Correct 234 ms 42416 KB Output is correct
13 Correct 279 ms 45448 KB Output is correct
14 Correct 263 ms 43400 KB Output is correct
15 Correct 295 ms 45964 KB Output is correct
16 Correct 36 ms 22220 KB Output is correct
17 Correct 219 ms 37980 KB Output is correct
18 Correct 222 ms 42040 KB Output is correct
19 Correct 75 ms 25676 KB Output is correct
20 Correct 99 ms 28040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 15268 KB Output is correct
2 Correct 63 ms 14616 KB Output is correct
3 Correct 78 ms 14292 KB Output is correct
4 Correct 60 ms 14032 KB Output is correct
5 Correct 80 ms 14728 KB Output is correct
6 Correct 48 ms 13808 KB Output is correct
7 Correct 54 ms 14608 KB Output is correct
8 Correct 50 ms 13864 KB Output is correct
9 Correct 57 ms 14416 KB Output is correct
10 Correct 45 ms 13484 KB Output is correct
11 Correct 51 ms 14156 KB Output is correct
12 Correct 48 ms 13576 KB Output is correct
13 Correct 51 ms 13532 KB Output is correct
14 Correct 44 ms 14080 KB Output is correct
15 Correct 42 ms 13692 KB Output is correct
16 Correct 17 ms 10812 KB Output is correct
17 Correct 41 ms 12224 KB Output is correct
18 Correct 36 ms 12652 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 239 ms 26000 KB Output is correct
2 Correct 260 ms 24444 KB Output is correct
3 Correct 248 ms 23888 KB Output is correct
4 Correct 196 ms 21536 KB Output is correct
5 Correct 240 ms 25900 KB Output is correct
6 Correct 207 ms 25720 KB Output is correct
7 Correct 224 ms 27204 KB Output is correct
8 Correct 212 ms 24200 KB Output is correct
9 Correct 211 ms 22856 KB Output is correct
10 Correct 213 ms 22860 KB Output is correct
11 Correct 201 ms 23304 KB Output is correct
12 Correct 203 ms 19468 KB Output is correct
13 Correct 217 ms 21776 KB Output is correct
14 Correct 232 ms 24928 KB Output is correct
15 Correct 222 ms 22096 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 159 ms 13636 KB Output is correct
18 Correct 214 ms 17364 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 355 ms 45756 KB Output is correct
22 Correct 320 ms 47420 KB Output is correct
23 Correct 266 ms 42856 KB Output is correct
24 Correct 241 ms 43160 KB Output is correct
25 Correct 277 ms 43892 KB Output is correct
26 Correct 244 ms 42608 KB Output is correct
27 Correct 304 ms 47408 KB Output is correct
28 Correct 304 ms 45876 KB Output is correct
29 Correct 269 ms 43820 KB Output is correct
30 Correct 307 ms 45436 KB Output is correct
31 Correct 260 ms 43788 KB Output is correct
32 Correct 234 ms 42416 KB Output is correct
33 Correct 279 ms 45448 KB Output is correct
34 Correct 263 ms 43400 KB Output is correct
35 Correct 295 ms 45964 KB Output is correct
36 Correct 36 ms 22220 KB Output is correct
37 Correct 219 ms 37980 KB Output is correct
38 Correct 222 ms 42040 KB Output is correct
39 Correct 75 ms 25676 KB Output is correct
40 Correct 99 ms 28040 KB Output is correct
41 Correct 65 ms 15268 KB Output is correct
42 Correct 63 ms 14616 KB Output is correct
43 Correct 78 ms 14292 KB Output is correct
44 Correct 60 ms 14032 KB Output is correct
45 Correct 80 ms 14728 KB Output is correct
46 Correct 48 ms 13808 KB Output is correct
47 Correct 54 ms 14608 KB Output is correct
48 Correct 50 ms 13864 KB Output is correct
49 Correct 57 ms 14416 KB Output is correct
50 Correct 45 ms 13484 KB Output is correct
51 Correct 51 ms 14156 KB Output is correct
52 Correct 48 ms 13576 KB Output is correct
53 Correct 51 ms 13532 KB Output is correct
54 Correct 44 ms 14080 KB Output is correct
55 Correct 42 ms 13692 KB Output is correct
56 Correct 17 ms 10812 KB Output is correct
57 Correct 41 ms 12224 KB Output is correct
58 Correct 36 ms 12652 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 320 KB Output is correct
61 Correct 715 ms 56036 KB Output is correct
62 Correct 565 ms 54712 KB Output is correct
63 Correct 569 ms 53396 KB Output is correct
64 Correct 442 ms 53296 KB Output is correct
65 Correct 480 ms 55116 KB Output is correct
66 Correct 513 ms 53136 KB Output is correct
67 Correct 391 ms 52956 KB Output is correct
68 Correct 383 ms 51076 KB Output is correct
69 Correct 410 ms 54020 KB Output is correct
70 Correct 368 ms 49944 KB Output is correct
71 Correct 319 ms 53240 KB Output is correct
72 Correct 368 ms 50244 KB Output is correct
73 Correct 339 ms 50764 KB Output is correct
74 Correct 354 ms 52644 KB Output is correct
75 Correct 349 ms 51064 KB Output is correct
76 Correct 44 ms 22220 KB Output is correct
77 Correct 231 ms 39344 KB Output is correct
78 Correct 255 ms 42820 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 1 ms 212 KB Output is correct