Submission #1111788

# Submission time Handle Problem Language Result Execution time Memory
1111788 2024-11-12T22:51:00 Z MisterReaper Abracadabra (CEOI22_abracadabra) C++17
75 / 100
3000 ms 39328 KB
#include <bits/stdc++.h>

using i64 = long long;

// #undef DEBUG
#ifdef DEBUG
    #include "debug.h"
#else
    #define debug(...) void(23)
#endif

constexpr int max_N = int(2E5) + 5;
constexpr int max_Q = int(1E6) + 5;
constexpr int LG = 19;

int N, Q;
int A[max_N];
int T[max_Q], I[max_Q], ans[max_Q];

int inv_p[max_N];

struct node {
    int act, siz;
} tree[max_N << 2];
node unite(const node lhs, const node rhs) {
    return {lhs.act + rhs.act, lhs.siz + rhs.siz};
}
void tree_build(int v, int l, int r) {
    if (l == r) {
        tree[v] = {0, 0};
        return;
    }
    int mid = (l + r) >> 1;
    tree_build(v << 1, l, mid);
    tree_build(v << 1 | 1, mid + 1, r);
    tree[v] = unite(tree[v << 1], tree[v << 1 | 1]);
}
void tree_build() {
    tree_build(1, 0, N);
}
void tree_add(int v, int l, int r, int x, int y) {
    if (l == r) {
        tree[v] = {1, y};
        return;
    }
    int mid = (l + r) >> 1;
    if (x <= mid) {
        tree_add(v << 1, l, mid, x, y);
    } else {
        tree_add(v << 1 | 1, mid + 1, r, x, y);
    }
    tree[v] = unite(tree[v << 1], tree[v << 1 | 1]);
}
void tree_add(int x, int y) {
    tree_add(1, 0, N, x, y);
}
int tree_get_kth_act(int v, int l, int r, int k) {
    if (l == r) {
        return l;
    }
    int mid = (l + r) >> 1;
    int left_act_size = tree[v << 1].act;
    if (k < left_act_size) {
        return tree_get_kth_act(v << 1, l, mid, k);
    } else {
        return tree_get_kth_act(v << 1 | 1, mid + 1, r, k - left_act_size);
    }
}
int tree_get_kth_act(int k) {
    return tree_get_kth_act(1, 0, N, k);
}
int tree_query(int v, int l, int r, int x) {
    if (l == r) {
        return 0;
    }
    int mid = (l + r) >> 1;
    if (x <= mid) {
        return tree_query(v << 1, l, mid, x);
    } else {
        return tree_query(v << 1 | 1, mid + 1, r, x) + tree[v << 1].siz;
    }
}
int tree_query(int x) {
    return tree_query(1, 0, N, x);
}

void debug_tree() {
    #ifdef DEBUG
        int n = tree[1].act;
        for (int i = 0; i < n; ++i) {
            int x = tree_get_kth_act(i);
            std::cerr << x << ' ' << tree_query(x) << '\n';
        }
    #endif
}

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

    std::cin >> N >> Q;
    for (int i = 0; i < N; ++i) {
        std::cin >> A[i];
        --A[i];
    }

    for (int i = 0; i < N; ++i) {
        inv_p[A[i]] = i;
    }
    A[N] = N;
    inv_p[N] = N;

    debug("wtf");
    std::vector<int> nxt_big(N), stk{N};
    for (int i = N - 1; i >= 0; --i) {
        while (A[i] > A[stk.back()]) {
            stk.pop_back();
        }
        nxt_big[i] = stk.back();
        stk.emplace_back(i);
    }
    debug("holyf");
    tree_build();

    debug("ok");


    auto partition = [&](int l, int r) -> std::vector<int> {
        debug(l, r);
        std::vector<int> v;
        while (l <= r) {
            v.emplace_back(l);
            l = std::min(r + 1, nxt_big[l]);
        }
        debug(v);
        v.emplace_back(r + 1);
        return v;
    };

    auto add = [&](std::vector<int> v) {
        for (int i = 0; i + 1 < v.size(); ++i) {
            debug(A[v[i]], inv_p[A[v[i + 1]]], inv_p[A[v[i]]]);
            tree_add(A[v[i]], inv_p[A[v[i + 1]]] - inv_p[A[v[i]]]);
        }
    };

    add(partition(0, N - 1));
    tree_add(N, 0);
    debug_tree();

    auto get_kth = [&](int k) -> int {
        int lo = 0, hi = tree[1].act - 1;
        while (lo < hi) {
            int mid = (lo + hi) >> 1;
            int x = tree_get_kth_act(mid + 1);
            int siz = tree_query(x);
            if (k < siz) {
                hi = mid;
            } else {
                lo = mid + 1;
            }
        }
        int x = tree_get_kth_act(lo);
        int siz = tree_query(x);
        // debug(k, x, siz);
        return A[inv_p[x] + k - siz];
    };
    auto group_kth = [&](int k) -> int {
        int lo = 0, hi = tree[1].act - 1;
        while (lo < hi) {
            int mid = (lo + hi) >> 1;
            int x = tree_get_kth_act(mid + 1);
            int siz = tree_query(x);
            if (k < siz) {
                hi = mid;
            } else {
                lo = mid + 1;
            }
        }
        int x = tree_get_kth_act(lo);
        debug("group", k, tree_query(x));
        return x;
    };

    #ifdef DEBUG
        for (int i = 0; i < N; ++i) {
            std::cerr << get_kth(i) + 1 << " \n"[i == N - 1];
        }
    #endif

    for (int i = 0; i < Q; ++i) {
        std::cin >> T[i] >> I[i];
        --I[i];
        T[i] = std::min(T[i], N);
    }

    std::vector<int> ord(Q);
    std::iota(ord.begin(), ord.end(), 0);
    std::sort(ord.begin(), ord.end(), [&](auto lhs, auto rhs) {
        return T[lhs] < T[rhs];
    });

    int curq = 0; 

    std::vector<int> a(N);
    for (int t = 0; curq < Q; ++t) {
        while (curq < Q && T[ord[curq]] == t) {
            ans[ord[curq]] = get_kth(I[ord[curq]]);
            ++curq;
        }
        int mid = get_kth(N / 2);
        int g = group_kth(N / 2);
        int size = tree_query(g + 1) - tree_query(g);
        debug(t, mid, g, size);
        if (g == mid) {
            continue;
        }
        int left_size = inv_p[mid] - inv_p[g], right_size = size - left_size;
        debug(left_size, right_size);
        auto v = partition(inv_p[mid], inv_p[mid] + right_size - 1);
        tree_add(g, inv_p[mid] - inv_p[g]);
        add(v);
        debug_tree();
        debug();
        #ifdef DEBUG
            for (int i = 0; i < N; ++i) {
                std::cerr << get_kth(i) + 1 << " \n"[i == N - 1];
            }
        #endif
    }

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

    return 0;
}

Compilation message

Main.cpp: In lambda function:
Main.cpp:141:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |         for (int i = 0; i + 1 < v.size(); ++i) {
      |                         ~~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 508 ms 26856 KB Output is correct
2 Correct 646 ms 22600 KB Output is correct
3 Correct 624 ms 26192 KB Output is correct
4 Correct 333 ms 22088 KB Output is correct
5 Correct 422 ms 26440 KB Output is correct
6 Correct 372 ms 25752 KB Output is correct
7 Correct 440 ms 26692 KB Output is correct
8 Correct 414 ms 25944 KB Output is correct
9 Correct 401 ms 25444 KB Output is correct
10 Correct 428 ms 22344 KB Output is correct
11 Correct 381 ms 22344 KB Output is correct
12 Correct 285 ms 25160 KB Output is correct
13 Correct 337 ms 22352 KB Output is correct
14 Correct 417 ms 22344 KB Output is correct
15 Correct 354 ms 26056 KB Output is correct
16 Correct 1 ms 8528 KB Output is correct
17 Correct 515 ms 25416 KB Output is correct
18 Correct 230 ms 22344 KB Output is correct
19 Correct 1 ms 8528 KB Output is correct
20 Correct 2 ms 8528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1420 ms 39328 KB Output is correct
2 Correct 1131 ms 32496 KB Output is correct
3 Correct 909 ms 29900 KB Output is correct
4 Correct 834 ms 34952 KB Output is correct
5 Correct 885 ms 29688 KB Output is correct
6 Correct 734 ms 29224 KB Output is correct
7 Correct 872 ms 31864 KB Output is correct
8 Correct 876 ms 31220 KB Output is correct
9 Correct 808 ms 29668 KB Output is correct
10 Correct 736 ms 31048 KB Output is correct
11 Correct 524 ms 34512 KB Output is correct
12 Correct 589 ms 29256 KB Output is correct
13 Correct 696 ms 31304 KB Output is correct
14 Correct 617 ms 34836 KB Output is correct
15 Correct 745 ms 37960 KB Output is correct
16 Correct 19 ms 15184 KB Output is correct
17 Correct 945 ms 36884 KB Output is correct
18 Correct 248 ms 29308 KB Output is correct
19 Correct 86 ms 17224 KB Output is correct
20 Correct 374 ms 16968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 536 ms 13996 KB Output is correct
2 Correct 470 ms 13344 KB Output is correct
3 Correct 477 ms 14268 KB Output is correct
4 Correct 234 ms 12824 KB Output is correct
5 Correct 305 ms 12872 KB Output is correct
6 Correct 236 ms 13896 KB Output is correct
7 Correct 276 ms 12872 KB Output is correct
8 Correct 260 ms 13932 KB Output is correct
9 Correct 302 ms 12872 KB Output is correct
10 Correct 189 ms 13752 KB Output is correct
11 Correct 205 ms 12880 KB Output is correct
12 Correct 189 ms 13896 KB Output is correct
13 Correct 202 ms 12872 KB Output is correct
14 Correct 202 ms 13896 KB Output is correct
15 Correct 187 ms 13968 KB Output is correct
16 Correct 14 ms 12368 KB Output is correct
17 Correct 311 ms 13296 KB Output is correct
18 Correct 63 ms 13896 KB Output is correct
19 Correct 2 ms 8528 KB Output is correct
20 Correct 2 ms 8528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 508 ms 26856 KB Output is correct
2 Correct 646 ms 22600 KB Output is correct
3 Correct 624 ms 26192 KB Output is correct
4 Correct 333 ms 22088 KB Output is correct
5 Correct 422 ms 26440 KB Output is correct
6 Correct 372 ms 25752 KB Output is correct
7 Correct 440 ms 26692 KB Output is correct
8 Correct 414 ms 25944 KB Output is correct
9 Correct 401 ms 25444 KB Output is correct
10 Correct 428 ms 22344 KB Output is correct
11 Correct 381 ms 22344 KB Output is correct
12 Correct 285 ms 25160 KB Output is correct
13 Correct 337 ms 22352 KB Output is correct
14 Correct 417 ms 22344 KB Output is correct
15 Correct 354 ms 26056 KB Output is correct
16 Correct 1 ms 8528 KB Output is correct
17 Correct 515 ms 25416 KB Output is correct
18 Correct 230 ms 22344 KB Output is correct
19 Correct 1 ms 8528 KB Output is correct
20 Correct 2 ms 8528 KB Output is correct
21 Correct 1420 ms 39328 KB Output is correct
22 Correct 1131 ms 32496 KB Output is correct
23 Correct 909 ms 29900 KB Output is correct
24 Correct 834 ms 34952 KB Output is correct
25 Correct 885 ms 29688 KB Output is correct
26 Correct 734 ms 29224 KB Output is correct
27 Correct 872 ms 31864 KB Output is correct
28 Correct 876 ms 31220 KB Output is correct
29 Correct 808 ms 29668 KB Output is correct
30 Correct 736 ms 31048 KB Output is correct
31 Correct 524 ms 34512 KB Output is correct
32 Correct 589 ms 29256 KB Output is correct
33 Correct 696 ms 31304 KB Output is correct
34 Correct 617 ms 34836 KB Output is correct
35 Correct 745 ms 37960 KB Output is correct
36 Correct 19 ms 15184 KB Output is correct
37 Correct 945 ms 36884 KB Output is correct
38 Correct 248 ms 29308 KB Output is correct
39 Correct 86 ms 17224 KB Output is correct
40 Correct 374 ms 16968 KB Output is correct
41 Correct 536 ms 13996 KB Output is correct
42 Correct 470 ms 13344 KB Output is correct
43 Correct 477 ms 14268 KB Output is correct
44 Correct 234 ms 12824 KB Output is correct
45 Correct 305 ms 12872 KB Output is correct
46 Correct 236 ms 13896 KB Output is correct
47 Correct 276 ms 12872 KB Output is correct
48 Correct 260 ms 13932 KB Output is correct
49 Correct 302 ms 12872 KB Output is correct
50 Correct 189 ms 13752 KB Output is correct
51 Correct 205 ms 12880 KB Output is correct
52 Correct 189 ms 13896 KB Output is correct
53 Correct 202 ms 12872 KB Output is correct
54 Correct 202 ms 13896 KB Output is correct
55 Correct 187 ms 13968 KB Output is correct
56 Correct 14 ms 12368 KB Output is correct
57 Correct 311 ms 13296 KB Output is correct
58 Correct 63 ms 13896 KB Output is correct
59 Correct 2 ms 8528 KB Output is correct
60 Correct 2 ms 8528 KB Output is correct
61 Correct 2750 ms 31908 KB Output is correct
62 Execution timed out 3067 ms 33004 KB Time limit exceeded
63 Halted 0 ms 0 KB -