Submission #1116311

# Submission time Handle Problem Language Result Execution time Memory
1116311 2024-11-21T13:26:44 Z Zero_OP Abracadabra (CEOI22_abracadabra) C++14
100 / 100
433 ms 48200 KB
#include <bits/stdc++.h>

using namespace std;

const int MAX = 2e5 + 5;

int N, Q, p[MAX], q[MAX], nxt[MAX];
vector<pair<int, int>> queries[MAX];

struct fenwick_tree{
    int L;
    vector<int> bit, ar;
    fenwick_tree(int n) : bit(n + 1, 0), ar(n + 1, 0), L(31 - __builtin_clz(n)) {}

    void update(int i, int v){
        for(; i < (int)bit.size(); i += i & (-i)) bit[i] += v;
    }

    int query(int i){
        int sum = 0;
        for(; i > 0; i -= i & (-i)) sum += bit[i];
        return sum;
    }

    int walk_first_lower(int k){ //first lower
        int pos = 0;
        for(int i = L; i >= 0; --i){
            if(pos + (1 << i) < (int)bit.size() && bit[pos + (1 << i)] < k){
                k -= bit[pos + (1 << i)];
                pos += (1 << i);
            }
        }
        return pos;
    }
};

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

#ifdef LOCAL
    freopen("task.inp", "r", stdin);
    freopen("task.out", "w", stdout);
#endif // LOCAL

    cin >> N >> Q;
    for(int i = 1; i <= N; ++i){
        cin >> p[i];
        q[p[i]] = i;
    }

    for(int i = 0; i < Q; ++i){
        int t, v;
        cin >> t >> v;
        queries[min(t, N)].emplace_back(v, i);
    }

    stack<int> st;
    for(int i = N; i >= 1; --i){
        while(!st.empty() && p[i] > p[st.top()]) st.pop();
        nxt[i] = (st.empty() ? N + 1 : st.top());
        st.push(i);
    }

    fenwick_tree ft(N);

    for(int i = 1; i <= N; i = nxt[i]){
        ft.update(p[i], nxt[i] - i);
    }

    auto get_kth = [&](int kth){
        int head = ft.walk_first_lower(kth) + 1;
        int sum_sizes = ft.query(head - 1);
        assert(sum_sizes < kth);
        return p[q[head] + kth - sum_sizes - 1];
    };

    bool demand = true;

    auto simulate = [&](){
        //info p[N/2]
        int head = ft.walk_first_lower(N / 2) + 1;
        int sum_sizes = ft.query(head);
        if(sum_sizes == N / 2) {
            demand = false;
            return; //[N/2] and [N/2+1] are in different blocks
        }

        //phase 1 : cut the left part
        int sum_previous_sizes = ft.query(head - 1);
        int current_block_size = sum_sizes - sum_previous_sizes;
        int position_head = q[head];
        int left_part_cut_size = N / 2 - sum_previous_sizes;
        ft.update(head, -current_block_size + left_part_cut_size);

        //phase 2 : divide the right part into some smaller blocks
        int new_head = p[q[head] + left_part_cut_size];
        int cur = N / 2;
        int old_bound = sum_sizes;

        int i;
        for(i = q[new_head]; cur + nxt[i] - i < old_bound; i = nxt[i]){
            ft.update(p[i], nxt[i] - i);
            cur += nxt[i] - i;
        }

        ft.update(p[i], old_bound - cur);
    };

    vector<int> ans(Q);
    for(int t = 0; t <= N; ++t){
        for(auto [kth, id] : queries[t])
            ans[id] = get_kth(kth);
        if(demand) simulate();
    }

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

    return 0;
}

Compilation message

Main.cpp: In constructor 'fenwick_tree::fenwick_tree(int)':
Main.cpp:12:22: warning: 'fenwick_tree::ar' will be initialized after [-Wreorder]
   12 |     vector<int> bit, ar;
      |                      ^~
Main.cpp:11:9: warning:   'int fenwick_tree::L' [-Wreorder]
   11 |     int L;
      |         ^
Main.cpp:13:5: warning:   when initialized here [-Wreorder]
   13 |     fenwick_tree(int n) : bit(n + 1, 0), ar(n + 1, 0), L(31 - __builtin_clz(n)) {}
      |     ^~~~~~~~~~~~
Main.cpp: In lambda function:
Main.cpp:91:13: warning: unused variable 'position_head' [-Wunused-variable]
   91 |         int position_head = q[head];
      |             ^~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:111:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  111 |         for(auto [kth, id] : queries[t])
      |                  ^
# Verdict Execution time Memory Grader output
1 Correct 198 ms 31304 KB Output is correct
2 Correct 212 ms 30660 KB Output is correct
3 Correct 201 ms 27008 KB Output is correct
4 Correct 189 ms 28468 KB Output is correct
5 Correct 229 ms 31816 KB Output is correct
6 Correct 161 ms 31816 KB Output is correct
7 Correct 180 ms 29768 KB Output is correct
8 Correct 173 ms 30880 KB Output is correct
9 Correct 149 ms 29148 KB Output is correct
10 Correct 163 ms 29256 KB Output is correct
11 Correct 163 ms 27124 KB Output is correct
12 Correct 144 ms 29944 KB Output is correct
13 Correct 170 ms 28268 KB Output is correct
14 Correct 156 ms 31048 KB Output is correct
15 Correct 160 ms 28788 KB Output is correct
16 Correct 2 ms 6976 KB Output is correct
17 Correct 195 ms 25696 KB Output is correct
18 Correct 146 ms 28068 KB Output is correct
19 Correct 2 ms 6736 KB Output is correct
20 Correct 2 ms 6736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 256 ms 41972 KB Output is correct
2 Correct 248 ms 41648 KB Output is correct
3 Correct 196 ms 34224 KB Output is correct
4 Correct 206 ms 33956 KB Output is correct
5 Correct 184 ms 36528 KB Output is correct
6 Correct 177 ms 33408 KB Output is correct
7 Correct 214 ms 40880 KB Output is correct
8 Correct 202 ms 39856 KB Output is correct
9 Correct 179 ms 36404 KB Output is correct
10 Correct 214 ms 38832 KB Output is correct
11 Correct 170 ms 34884 KB Output is correct
12 Correct 207 ms 32680 KB Output is correct
13 Correct 192 ms 38064 KB Output is correct
14 Correct 174 ms 35540 KB Output is correct
15 Correct 198 ms 39472 KB Output is correct
16 Correct 18 ms 10064 KB Output is correct
17 Correct 208 ms 37040 KB Output is correct
18 Correct 150 ms 30640 KB Output is correct
19 Correct 54 ms 15808 KB Output is correct
20 Correct 63 ms 17344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 12616 KB Output is correct
2 Correct 46 ms 12360 KB Output is correct
3 Correct 49 ms 12468 KB Output is correct
4 Correct 34 ms 11600 KB Output is correct
5 Correct 39 ms 12104 KB Output is correct
6 Correct 34 ms 11612 KB Output is correct
7 Correct 38 ms 12108 KB Output is correct
8 Correct 36 ms 11600 KB Output is correct
9 Correct 37 ms 11848 KB Output is correct
10 Correct 31 ms 11344 KB Output is correct
11 Correct 30 ms 11600 KB Output is correct
12 Correct 32 ms 11340 KB Output is correct
13 Correct 30 ms 11344 KB Output is correct
14 Correct 32 ms 11592 KB Output is correct
15 Correct 27 ms 11404 KB Output is correct
16 Correct 9 ms 8272 KB Output is correct
17 Correct 35 ms 11204 KB Output is correct
18 Correct 25 ms 10952 KB Output is correct
19 Correct 2 ms 6904 KB Output is correct
20 Correct 2 ms 6736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 198 ms 31304 KB Output is correct
2 Correct 212 ms 30660 KB Output is correct
3 Correct 201 ms 27008 KB Output is correct
4 Correct 189 ms 28468 KB Output is correct
5 Correct 229 ms 31816 KB Output is correct
6 Correct 161 ms 31816 KB Output is correct
7 Correct 180 ms 29768 KB Output is correct
8 Correct 173 ms 30880 KB Output is correct
9 Correct 149 ms 29148 KB Output is correct
10 Correct 163 ms 29256 KB Output is correct
11 Correct 163 ms 27124 KB Output is correct
12 Correct 144 ms 29944 KB Output is correct
13 Correct 170 ms 28268 KB Output is correct
14 Correct 156 ms 31048 KB Output is correct
15 Correct 160 ms 28788 KB Output is correct
16 Correct 2 ms 6976 KB Output is correct
17 Correct 195 ms 25696 KB Output is correct
18 Correct 146 ms 28068 KB Output is correct
19 Correct 2 ms 6736 KB Output is correct
20 Correct 2 ms 6736 KB Output is correct
21 Correct 256 ms 41972 KB Output is correct
22 Correct 248 ms 41648 KB Output is correct
23 Correct 196 ms 34224 KB Output is correct
24 Correct 206 ms 33956 KB Output is correct
25 Correct 184 ms 36528 KB Output is correct
26 Correct 177 ms 33408 KB Output is correct
27 Correct 214 ms 40880 KB Output is correct
28 Correct 202 ms 39856 KB Output is correct
29 Correct 179 ms 36404 KB Output is correct
30 Correct 214 ms 38832 KB Output is correct
31 Correct 170 ms 34884 KB Output is correct
32 Correct 207 ms 32680 KB Output is correct
33 Correct 192 ms 38064 KB Output is correct
34 Correct 174 ms 35540 KB Output is correct
35 Correct 198 ms 39472 KB Output is correct
36 Correct 18 ms 10064 KB Output is correct
37 Correct 208 ms 37040 KB Output is correct
38 Correct 150 ms 30640 KB Output is correct
39 Correct 54 ms 15808 KB Output is correct
40 Correct 63 ms 17344 KB Output is correct
41 Correct 47 ms 12616 KB Output is correct
42 Correct 46 ms 12360 KB Output is correct
43 Correct 49 ms 12468 KB Output is correct
44 Correct 34 ms 11600 KB Output is correct
45 Correct 39 ms 12104 KB Output is correct
46 Correct 34 ms 11612 KB Output is correct
47 Correct 38 ms 12108 KB Output is correct
48 Correct 36 ms 11600 KB Output is correct
49 Correct 37 ms 11848 KB Output is correct
50 Correct 31 ms 11344 KB Output is correct
51 Correct 30 ms 11600 KB Output is correct
52 Correct 32 ms 11340 KB Output is correct
53 Correct 30 ms 11344 KB Output is correct
54 Correct 32 ms 11592 KB Output is correct
55 Correct 27 ms 11404 KB Output is correct
56 Correct 9 ms 8272 KB Output is correct
57 Correct 35 ms 11204 KB Output is correct
58 Correct 25 ms 10952 KB Output is correct
59 Correct 2 ms 6904 KB Output is correct
60 Correct 2 ms 6736 KB Output is correct
61 Correct 419 ms 48200 KB Output is correct
62 Correct 433 ms 46808 KB Output is correct
63 Correct 429 ms 45384 KB Output is correct
64 Correct 347 ms 44616 KB Output is correct
65 Correct 300 ms 46408 KB Output is correct
66 Correct 330 ms 44580 KB Output is correct
67 Correct 287 ms 44104 KB Output is correct
68 Correct 306 ms 42836 KB Output is correct
69 Correct 300 ms 44616 KB Output is correct
70 Correct 281 ms 41032 KB Output is correct
71 Correct 256 ms 42388 KB Output is correct
72 Correct 286 ms 41388 KB Output is correct
73 Correct 241 ms 41800 KB Output is correct
74 Correct 249 ms 43716 KB Output is correct
75 Correct 246 ms 42312 KB Output is correct
76 Correct 19 ms 10064 KB Output is correct
77 Correct 323 ms 37148 KB Output is correct
78 Correct 189 ms 36524 KB Output is correct
79 Correct 2 ms 6736 KB Output is correct
80 Correct 2 ms 6736 KB Output is correct