Submission #831724

# Submission time Handle Problem Language Result Execution time Memory
831724 2023-08-20T12:43:15 Z QwertyPi Abracadabra (CEOI22_abracadabra) C++14
100 / 100
799 ms 42436 KB
#include <bits/stdc++.h>

using namespace std;

vector<int> shuffle(vector<int> a){
    int n = a.size();
    vector<int> a1(a.begin(), a.begin() + n / 2), a2(a.begin() + n / 2, a.end());
    a1.push_back(1 << 30); a2.push_back(1 << 30);
    int l = 0, r = 0;
    vector<int> b;
    for(int i = 0; i < n; i++){
        if(a1[l] < a2[r]) b.push_back(a1[l++]);
        else b.push_back(a2[r++]);
    }
    return b;
}

const int MAXN = 2e5 + 11;
const int MAXQ = 1e6 + 11;
int ans[MAXQ];

struct query{
    int t, i, id;
    bool operator< (const query& o) const {
        return t < o.t;
    }
};

struct BIT{
    int bit[MAXN];
    void add(int p, int v){ p++; for(int i = p; i < MAXN; i += i & -i) bit[i] += v; }
    int qry(int p){ p++; int r = 0; for(int i = p; i; i -= i & -i) r += bit[i]; return r; }
    int bs(int v){ int r = 0, s = 0; for(int j = 19; j >= 0; j--) if(r + (1 << j) < MAXN && s + bit[r + (1 << j)] < v) s += bit[r + (1 << j)], r += (1 << j); return r; }
} bit;

int p[MAXN];

vector<int> a;
int n, q;
int nxt[MAXN];

void simulate_shuffle(){
    int pos = bit.bs(n / 2);
    int v1 = bit.qry(pos), v2 = bit.qry(pos - 1);
    if(v1 == n / 2) return;
    int x = p[pos] + (n / 2 - v2);
    vector<int> split; split.push_back(x);
    while(nxt[x] < p[pos] + (v1 - v2))
        x = nxt[x], split.push_back(x);
    split.push_back(p[pos] + (v1 - v2));

    bit.add(pos, n / 2 - v1);
    for(int i = 0; i + 1 < split.size(); i++){
        bit.add(a[split[i]], split[i + 1] - split[i]);
        p[a[split[i]]] = split[i];
    }
}

int calculate_position(int i){
    int pos = bit.bs(i);
    int ri = bit.qry(pos - 1);
    return a[p[pos] + (i - (ri + 1))];
}

int32_t main(){
    cin >> n >> q;
    for(int i = 0; i < n; i++){
        int v; cin >> v; a.push_back(v);
    }

    vector<query> Q;
    for(int id = 0; id < q; id++){
        int t, i; cin >> t >> i; Q.push_back({t, i, id});
    }
    sort(Q.begin(), Q.end());

    for(auto q : Q){
        if(q.t == 0) ans[q.id] = a[q.i - 1];
        else break;
    }

    a = shuffle(a);
    a.push_back(1 << 30);
    vector<int> geq;
    geq.push_back(n);
    for(int i = n - 1; i >= 0; i--){
        while(!geq.empty() && a[geq.back()] < a[i]) geq.pop_back();
        nxt[i] = geq.back();
        geq.push_back(i);
    }

    int ct = 1;

    vector<int> idx;
    idx.push_back(0);
    for(int i = 1; i < n; i++){
        if(a[idx.back()] < a[i]) idx.push_back(i);
    }
    idx.push_back(n);
    for(int i = 0; i + 1 < idx.size(); i++){
        bit.add(a[idx[i]], idx[i + 1] - idx[i]);
        p[a[idx[i]]] = idx[i];
    }

    for(auto q : Q){
        if(q.t == 0) continue;
        while(ct < q.t && ct < n) simulate_shuffle(), ct++;
        ans[q.id] = calculate_position(q.i);
    }
    for(int i = 0; i < q; i++){
        cout << ans[i] << '\n';
    }
}

Compilation message

Main.cpp: In function 'void simulate_shuffle()':
Main.cpp:53:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |     for(int i = 0; i + 1 < split.size(); i++){
      |                    ~~~~~~^~~~~~~~~~~~~~
Main.cpp: In function 'int32_t main()':
Main.cpp:100:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |     for(int i = 0; i + 1 < idx.size(); i++){
      |                    ~~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 543 ms 27672 KB Output is correct
2 Correct 515 ms 27376 KB Output is correct
3 Correct 465 ms 26544 KB Output is correct
4 Correct 456 ms 25256 KB Output is correct
5 Correct 466 ms 27036 KB Output is correct
6 Correct 451 ms 25568 KB Output is correct
7 Correct 470 ms 27192 KB Output is correct
8 Correct 446 ms 25688 KB Output is correct
9 Correct 435 ms 25248 KB Output is correct
10 Correct 449 ms 25904 KB Output is correct
11 Correct 448 ms 25732 KB Output is correct
12 Correct 400 ms 24656 KB Output is correct
13 Correct 437 ms 25692 KB Output is correct
14 Correct 476 ms 26296 KB Output is correct
15 Correct 452 ms 26260 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 343 ms 25140 KB Output is correct
18 Correct 420 ms 24764 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 642 ms 42436 KB Output is correct
2 Correct 726 ms 42116 KB Output is correct
3 Correct 497 ms 33672 KB Output is correct
4 Correct 500 ms 34100 KB Output is correct
5 Correct 492 ms 34936 KB Output is correct
6 Correct 501 ms 33588 KB Output is correct
7 Correct 599 ms 41720 KB Output is correct
8 Correct 587 ms 39924 KB Output is correct
9 Correct 515 ms 34752 KB Output is correct
10 Correct 619 ms 38764 KB Output is correct
11 Correct 455 ms 32976 KB Output is correct
12 Correct 472 ms 32804 KB Output is correct
13 Correct 544 ms 38340 KB Output is correct
14 Correct 513 ms 33916 KB Output is correct
15 Correct 610 ms 39852 KB Output is correct
16 Correct 46 ms 5412 KB Output is correct
17 Correct 439 ms 36824 KB Output is correct
18 Correct 437 ms 29180 KB Output is correct
19 Correct 124 ms 12064 KB Output is correct
20 Correct 186 ms 14656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 7112 KB Output is correct
2 Correct 99 ms 7260 KB Output is correct
3 Correct 95 ms 6860 KB Output is correct
4 Correct 86 ms 6812 KB Output is correct
5 Correct 95 ms 7160 KB Output is correct
6 Correct 97 ms 6800 KB Output is correct
7 Correct 92 ms 7096 KB Output is correct
8 Correct 94 ms 6828 KB Output is correct
9 Correct 90 ms 7024 KB Output is correct
10 Correct 81 ms 6720 KB Output is correct
11 Correct 84 ms 6808 KB Output is correct
12 Correct 81 ms 6688 KB Output is correct
13 Correct 83 ms 6832 KB Output is correct
14 Correct 84 ms 6896 KB Output is correct
15 Correct 81 ms 6800 KB Output is correct
16 Correct 23 ms 2892 KB Output is correct
17 Correct 80 ms 6724 KB Output is correct
18 Correct 73 ms 5864 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 543 ms 27672 KB Output is correct
2 Correct 515 ms 27376 KB Output is correct
3 Correct 465 ms 26544 KB Output is correct
4 Correct 456 ms 25256 KB Output is correct
5 Correct 466 ms 27036 KB Output is correct
6 Correct 451 ms 25568 KB Output is correct
7 Correct 470 ms 27192 KB Output is correct
8 Correct 446 ms 25688 KB Output is correct
9 Correct 435 ms 25248 KB Output is correct
10 Correct 449 ms 25904 KB Output is correct
11 Correct 448 ms 25732 KB Output is correct
12 Correct 400 ms 24656 KB Output is correct
13 Correct 437 ms 25692 KB Output is correct
14 Correct 476 ms 26296 KB Output is correct
15 Correct 452 ms 26260 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 343 ms 25140 KB Output is correct
18 Correct 420 ms 24764 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 642 ms 42436 KB Output is correct
22 Correct 726 ms 42116 KB Output is correct
23 Correct 497 ms 33672 KB Output is correct
24 Correct 500 ms 34100 KB Output is correct
25 Correct 492 ms 34936 KB Output is correct
26 Correct 501 ms 33588 KB Output is correct
27 Correct 599 ms 41720 KB Output is correct
28 Correct 587 ms 39924 KB Output is correct
29 Correct 515 ms 34752 KB Output is correct
30 Correct 619 ms 38764 KB Output is correct
31 Correct 455 ms 32976 KB Output is correct
32 Correct 472 ms 32804 KB Output is correct
33 Correct 544 ms 38340 KB Output is correct
34 Correct 513 ms 33916 KB Output is correct
35 Correct 610 ms 39852 KB Output is correct
36 Correct 46 ms 5412 KB Output is correct
37 Correct 439 ms 36824 KB Output is correct
38 Correct 437 ms 29180 KB Output is correct
39 Correct 124 ms 12064 KB Output is correct
40 Correct 186 ms 14656 KB Output is correct
41 Correct 99 ms 7112 KB Output is correct
42 Correct 99 ms 7260 KB Output is correct
43 Correct 95 ms 6860 KB Output is correct
44 Correct 86 ms 6812 KB Output is correct
45 Correct 95 ms 7160 KB Output is correct
46 Correct 97 ms 6800 KB Output is correct
47 Correct 92 ms 7096 KB Output is correct
48 Correct 94 ms 6828 KB Output is correct
49 Correct 90 ms 7024 KB Output is correct
50 Correct 81 ms 6720 KB Output is correct
51 Correct 84 ms 6808 KB Output is correct
52 Correct 81 ms 6688 KB Output is correct
53 Correct 83 ms 6832 KB Output is correct
54 Correct 84 ms 6896 KB Output is correct
55 Correct 81 ms 6800 KB Output is correct
56 Correct 23 ms 2892 KB Output is correct
57 Correct 80 ms 6724 KB Output is correct
58 Correct 73 ms 5864 KB Output is correct
59 Correct 0 ms 340 KB Output is correct
60 Correct 0 ms 340 KB Output is correct
61 Correct 799 ms 42308 KB Output is correct
62 Correct 764 ms 41912 KB Output is correct
63 Correct 721 ms 40068 KB Output is correct
64 Correct 706 ms 39876 KB Output is correct
65 Correct 674 ms 41440 KB Output is correct
66 Correct 674 ms 39780 KB Output is correct
67 Correct 693 ms 41052 KB Output is correct
68 Correct 659 ms 39552 KB Output is correct
69 Correct 685 ms 39664 KB Output is correct
70 Correct 645 ms 39248 KB Output is correct
71 Correct 617 ms 38396 KB Output is correct
72 Correct 629 ms 38920 KB Output is correct
73 Correct 666 ms 38828 KB Output is correct
74 Correct 676 ms 39932 KB Output is correct
75 Correct 638 ms 39996 KB Output is correct
76 Correct 45 ms 5312 KB Output is correct
77 Correct 501 ms 36964 KB Output is correct
78 Correct 521 ms 35004 KB Output is correct
79 Correct 0 ms 340 KB Output is correct
80 Correct 0 ms 340 KB Output is correct