Submission #1111789

# Submission time Handle Problem Language Result Execution time Memory
1111789 2024-11-12T23:11:51 Z MisterReaper Abracadabra (CEOI22_abracadabra) C++17
100 / 100
729 ms 45060 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);
}
int tree_walk(int v, int l, int r, int k) {
    debug(v, l, r, k);
    if (l == r) {
        return l;
    }
    int mid = (l + r) >> 1;
    if (tree[v << 1].siz <= k) {
        return tree_walk(v << 1 | 1, mid + 1, r, k - tree[v << 1].siz);
    } else {
        return tree_walk(v << 1, l, mid, k);
    }
}
int tree_walk(int k) {
    return tree_walk(1, 0, N, k);
}
int tree_get_index(int v, int l, int r, int k) {
    if (l == r) {
        return tree[v].act;
    }
    int mid = (l + r) >> 1;
    if (k <= mid) {
        return tree_get_index(v << 1, l, mid, k);
    } else {
        return tree_get_index(v << 1 | 1, mid + 1, r, k) + tree[v << 1].act;
    }
}
int tree_get_index(int k) {
    return tree_get_index(1, 0, N, k);
}

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 x = tree_get_kth_act(tree_get_index(tree_walk(k)) - 1);
        int siz = tree_query(x);
        debug(k, x, siz);
        return A[inv_p[x] + k - siz];
    };
    auto group_kth = [&](int k) -> int {
        int x = tree_get_kth_act(tree_get_index(tree_walk(k)) - 1);
        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:170:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  170 |         for (int i = 0; i + 1 < v.size(); ++i) {
      |                         ~~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 288 ms 22600 KB Output is correct
2 Correct 336 ms 22612 KB Output is correct
3 Correct 324 ms 22344 KB Output is correct
4 Correct 244 ms 22088 KB Output is correct
5 Correct 260 ms 22520 KB Output is correct
6 Correct 243 ms 22088 KB Output is correct
7 Correct 269 ms 22600 KB Output is correct
8 Correct 253 ms 22260 KB Output is correct
9 Correct 240 ms 22088 KB Output is correct
10 Correct 251 ms 22256 KB Output is correct
11 Correct 246 ms 22384 KB Output is correct
12 Correct 236 ms 22088 KB Output is correct
13 Correct 245 ms 22344 KB Output is correct
14 Correct 270 ms 22344 KB Output is correct
15 Correct 290 ms 22772 KB Output is correct
16 Correct 2 ms 8528 KB Output is correct
17 Correct 311 ms 22344 KB Output is correct
18 Correct 215 ms 22348 KB Output is correct
19 Correct 2 ms 8696 KB Output is correct
20 Correct 2 ms 8528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 454 ms 31912 KB Output is correct
2 Correct 394 ms 32384 KB Output is correct
3 Correct 296 ms 29904 KB Output is correct
4 Correct 283 ms 29324 KB Output is correct
5 Correct 282 ms 29612 KB Output is correct
6 Correct 246 ms 29260 KB Output is correct
7 Correct 311 ms 31816 KB Output is correct
8 Correct 343 ms 31304 KB Output is correct
9 Correct 270 ms 29512 KB Output is correct
10 Correct 274 ms 31048 KB Output is correct
11 Correct 248 ms 29640 KB Output is correct
12 Correct 253 ms 29256 KB Output is correct
13 Correct 274 ms 31056 KB Output is correct
14 Correct 256 ms 29644 KB Output is correct
15 Correct 277 ms 31364 KB Output is correct
16 Correct 18 ms 15184 KB Output is correct
17 Correct 360 ms 32080 KB Output is correct
18 Correct 214 ms 29304 KB Output is correct
19 Correct 77 ms 17224 KB Output is correct
20 Correct 114 ms 17004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 111 ms 12872 KB Output is correct
2 Correct 99 ms 13092 KB Output is correct
3 Correct 119 ms 13192 KB Output is correct
4 Correct 71 ms 12936 KB Output is correct
5 Correct 83 ms 12844 KB Output is correct
6 Correct 71 ms 12872 KB Output is correct
7 Correct 80 ms 12976 KB Output is correct
8 Correct 74 ms 12872 KB Output is correct
9 Correct 90 ms 12952 KB Output is correct
10 Correct 71 ms 12872 KB Output is correct
11 Correct 67 ms 12872 KB Output is correct
12 Correct 60 ms 12920 KB Output is correct
13 Correct 68 ms 12872 KB Output is correct
14 Correct 63 ms 12872 KB Output is correct
15 Correct 61 ms 12872 KB Output is correct
16 Correct 9 ms 11884 KB Output is correct
17 Correct 86 ms 13260 KB Output is correct
18 Correct 52 ms 12872 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 288 ms 22600 KB Output is correct
2 Correct 336 ms 22612 KB Output is correct
3 Correct 324 ms 22344 KB Output is correct
4 Correct 244 ms 22088 KB Output is correct
5 Correct 260 ms 22520 KB Output is correct
6 Correct 243 ms 22088 KB Output is correct
7 Correct 269 ms 22600 KB Output is correct
8 Correct 253 ms 22260 KB Output is correct
9 Correct 240 ms 22088 KB Output is correct
10 Correct 251 ms 22256 KB Output is correct
11 Correct 246 ms 22384 KB Output is correct
12 Correct 236 ms 22088 KB Output is correct
13 Correct 245 ms 22344 KB Output is correct
14 Correct 270 ms 22344 KB Output is correct
15 Correct 290 ms 22772 KB Output is correct
16 Correct 2 ms 8528 KB Output is correct
17 Correct 311 ms 22344 KB Output is correct
18 Correct 215 ms 22348 KB Output is correct
19 Correct 2 ms 8696 KB Output is correct
20 Correct 2 ms 8528 KB Output is correct
21 Correct 454 ms 31912 KB Output is correct
22 Correct 394 ms 32384 KB Output is correct
23 Correct 296 ms 29904 KB Output is correct
24 Correct 283 ms 29324 KB Output is correct
25 Correct 282 ms 29612 KB Output is correct
26 Correct 246 ms 29260 KB Output is correct
27 Correct 311 ms 31816 KB Output is correct
28 Correct 343 ms 31304 KB Output is correct
29 Correct 270 ms 29512 KB Output is correct
30 Correct 274 ms 31048 KB Output is correct
31 Correct 248 ms 29640 KB Output is correct
32 Correct 253 ms 29256 KB Output is correct
33 Correct 274 ms 31056 KB Output is correct
34 Correct 256 ms 29644 KB Output is correct
35 Correct 277 ms 31364 KB Output is correct
36 Correct 18 ms 15184 KB Output is correct
37 Correct 360 ms 32080 KB Output is correct
38 Correct 214 ms 29304 KB Output is correct
39 Correct 77 ms 17224 KB Output is correct
40 Correct 114 ms 17004 KB Output is correct
41 Correct 111 ms 12872 KB Output is correct
42 Correct 99 ms 13092 KB Output is correct
43 Correct 119 ms 13192 KB Output is correct
44 Correct 71 ms 12936 KB Output is correct
45 Correct 83 ms 12844 KB Output is correct
46 Correct 71 ms 12872 KB Output is correct
47 Correct 80 ms 12976 KB Output is correct
48 Correct 74 ms 12872 KB Output is correct
49 Correct 90 ms 12952 KB Output is correct
50 Correct 71 ms 12872 KB Output is correct
51 Correct 67 ms 12872 KB Output is correct
52 Correct 60 ms 12920 KB Output is correct
53 Correct 68 ms 12872 KB Output is correct
54 Correct 63 ms 12872 KB Output is correct
55 Correct 61 ms 12872 KB Output is correct
56 Correct 9 ms 11884 KB Output is correct
57 Correct 86 ms 13260 KB Output is correct
58 Correct 52 ms 12872 KB Output is correct
59 Correct 2 ms 8528 KB Output is correct
60 Correct 2 ms 8528 KB Output is correct
61 Correct 713 ms 31976 KB Output is correct
62 Correct 729 ms 32384 KB Output is correct
63 Correct 703 ms 44520 KB Output is correct
64 Correct 556 ms 43908 KB Output is correct
65 Correct 594 ms 45060 KB Output is correct
66 Correct 494 ms 43696 KB Output is correct
67 Correct 514 ms 44620 KB Output is correct
68 Correct 568 ms 42824 KB Output is correct
69 Correct 590 ms 43592 KB Output is correct
70 Correct 503 ms 43092 KB Output is correct
71 Correct 482 ms 42312 KB Output is correct
72 Correct 472 ms 42996 KB Output is correct
73 Correct 489 ms 42568 KB Output is correct
74 Correct 471 ms 43540 KB Output is correct
75 Correct 464 ms 43560 KB Output is correct
76 Correct 18 ms 16464 KB Output is correct
77 Correct 604 ms 41308 KB Output is correct
78 Correct 317 ms 40276 KB Output is correct
79 Correct 2 ms 8528 KB Output is correct
80 Correct 2 ms 8528 KB Output is correct