Submission #654038

# Submission time Handle Problem Language Result Execution time Memory
654038 2022-10-29T15:42:18 Z Vladth11 Abracadabra (CEOI22_abracadabra) C++17
100 / 100
773 ms 34912 KB
#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
#include <algorithm>
 
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
 
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
 
const int bSize = 11;
const int bits = 17;
const int NMAX = 200001;
const int LIMIT = NMAX;
 
vector <pii> qq[LIMIT + 1];
int sol[NMAX * 5];
int v[NMAX];
int poz[NMAX];
int stiva[NMAX];
int vf;
int nxt[NMAX];
pii interval[NMAX];
int aib[NMAX];
 
void update(int node, int x) {
    for(; node < NMAX; node += node&(-node))
        aib[node] += x;
}
 
int query(int node) {
    int x = 0;
    for(; node > 0; node -= node&(-node))
        x += aib[node];
    return x;
}
 
int cb(int k){
    int r = 0, pas = (1 << bits), sum = 0;
    while(pas){
        if(r + pas < NMAX && aib[r + pas] + sum < k){
            sum += aib[r + pas];
            r += pas;
        }
        pas /= 2;
    }
    if(sum < k)
        r++;
    return r;
}
 
 
int sz(pii x) {
    if(x.second == 0)
        return 0;
    return x.second - x.first + 1;
}
 
int n;
 
void mShuffle() {
    int cine = cb(n / 2);
    if(query(cine) == n / 2) {
        return;
    }
    int dr = interval[cine].second;
    update(cine, -sz(interval[cine]));
    int pana = query(cine - 1);
    int node = n / 2;
    node -= pana;
    interval[cine] = {poz[cine], poz[cine] + node - 1};
    update(cine, sz(interval[cine]));
    int i = v[poz[cine] + node];
    while(sz(interval[i]) == 0 && poz[i] <= dr) {
        int urm = nxt[poz[i]];
        if(urm == 0)
            urm = n + 1;
        urm = min(urm, dr + 1);
        interval[i] = {poz[i], urm - 1};
        update(i, sz(interval[i]));
        if(urm != n + 1)
            i = v[urm];
        else
            break;
    }
}
 
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int q, i;
    cin >> n >> q;
    for(i = 1; i <= n; i++) {
        cin >> v[i];
        poz[v[i]] = i;
        while(vf && v[stiva[vf]] < v[i]) {
            nxt[stiva[vf]] = i;
            vf--;
        }
        stiva[++vf] = i;
    }
    i = 1;
    while(nxt[i] != 0) {
        interval[v[i]] = {i, nxt[i] - 1};
        i = nxt[i];
    }
    interval[v[i]] = {i, n};
    for(i = 1; i <= n; i++) {
        update(i, sz(interval[i]));
    }
    for(i = 1; i <= q; i++) {
        int x, y;
        cin >> x >> y;
        x = min(x, LIMIT);
        qq[x].push_back({y, i});
    }
    for(i = 0; i <= LIMIT; i++) {
        for(auto x : qq[i]) {
            int node = x.first;
            int idx = x.second;
            int cine = cb(node);
            int pana = query(cine - 1);
            node -= pana;
            sol[idx] = v[poz[cine] + node - 1];
        }
        mShuffle();
    }
    for(i = 1; i <= q; i++) {
        cout << sol[i] << "\n";
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 346 ms 23184 KB Output is correct
2 Correct 330 ms 21980 KB Output is correct
3 Correct 330 ms 21508 KB Output is correct
4 Correct 325 ms 21268 KB Output is correct
5 Correct 325 ms 23688 KB Output is correct
6 Correct 315 ms 24276 KB Output is correct
7 Correct 332 ms 24392 KB Output is correct
8 Correct 309 ms 22756 KB Output is correct
9 Correct 288 ms 21784 KB Output is correct
10 Correct 292 ms 21504 KB Output is correct
11 Correct 311 ms 21972 KB Output is correct
12 Correct 320 ms 20140 KB Output is correct
13 Correct 340 ms 21016 KB Output is correct
14 Correct 299 ms 22980 KB Output is correct
15 Correct 323 ms 21384 KB Output is correct
16 Correct 27 ms 5076 KB Output is correct
17 Correct 292 ms 20292 KB Output is correct
18 Correct 341 ms 20252 KB Output is correct
19 Correct 18 ms 5076 KB Output is correct
20 Correct 20 ms 5088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 434 ms 27984 KB Output is correct
2 Correct 362 ms 28300 KB Output is correct
3 Correct 302 ms 23900 KB Output is correct
4 Correct 326 ms 23980 KB Output is correct
5 Correct 331 ms 24464 KB Output is correct
6 Correct 301 ms 23672 KB Output is correct
7 Correct 379 ms 27972 KB Output is correct
8 Correct 350 ms 26920 KB Output is correct
9 Correct 341 ms 24280 KB Output is correct
10 Correct 374 ms 26928 KB Output is correct
11 Correct 288 ms 24068 KB Output is correct
12 Correct 314 ms 23680 KB Output is correct
13 Correct 374 ms 27196 KB Output is correct
14 Correct 271 ms 24236 KB Output is correct
15 Correct 378 ms 27500 KB Output is correct
16 Correct 42 ms 9416 KB Output is correct
17 Correct 317 ms 27172 KB Output is correct
18 Correct 260 ms 22184 KB Output is correct
19 Correct 108 ms 12924 KB Output is correct
20 Correct 126 ms 13140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 10380 KB Output is correct
2 Correct 78 ms 9972 KB Output is correct
3 Correct 105 ms 9836 KB Output is correct
4 Correct 75 ms 9676 KB Output is correct
5 Correct 76 ms 10012 KB Output is correct
6 Correct 72 ms 9644 KB Output is correct
7 Correct 78 ms 9920 KB Output is correct
8 Correct 72 ms 9656 KB Output is correct
9 Correct 96 ms 9852 KB Output is correct
10 Correct 66 ms 9452 KB Output is correct
11 Correct 64 ms 9672 KB Output is correct
12 Correct 61 ms 9508 KB Output is correct
13 Correct 64 ms 9292 KB Output is correct
14 Correct 87 ms 9600 KB Output is correct
15 Correct 77 ms 9244 KB Output is correct
16 Correct 33 ms 7380 KB Output is correct
17 Correct 76 ms 9132 KB Output is correct
18 Correct 72 ms 8348 KB Output is correct
19 Correct 20 ms 5088 KB Output is correct
20 Correct 20 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 346 ms 23184 KB Output is correct
2 Correct 330 ms 21980 KB Output is correct
3 Correct 330 ms 21508 KB Output is correct
4 Correct 325 ms 21268 KB Output is correct
5 Correct 325 ms 23688 KB Output is correct
6 Correct 315 ms 24276 KB Output is correct
7 Correct 332 ms 24392 KB Output is correct
8 Correct 309 ms 22756 KB Output is correct
9 Correct 288 ms 21784 KB Output is correct
10 Correct 292 ms 21504 KB Output is correct
11 Correct 311 ms 21972 KB Output is correct
12 Correct 320 ms 20140 KB Output is correct
13 Correct 340 ms 21016 KB Output is correct
14 Correct 299 ms 22980 KB Output is correct
15 Correct 323 ms 21384 KB Output is correct
16 Correct 27 ms 5076 KB Output is correct
17 Correct 292 ms 20292 KB Output is correct
18 Correct 341 ms 20252 KB Output is correct
19 Correct 18 ms 5076 KB Output is correct
20 Correct 20 ms 5088 KB Output is correct
21 Correct 434 ms 27984 KB Output is correct
22 Correct 362 ms 28300 KB Output is correct
23 Correct 302 ms 23900 KB Output is correct
24 Correct 326 ms 23980 KB Output is correct
25 Correct 331 ms 24464 KB Output is correct
26 Correct 301 ms 23672 KB Output is correct
27 Correct 379 ms 27972 KB Output is correct
28 Correct 350 ms 26920 KB Output is correct
29 Correct 341 ms 24280 KB Output is correct
30 Correct 374 ms 26928 KB Output is correct
31 Correct 288 ms 24068 KB Output is correct
32 Correct 314 ms 23680 KB Output is correct
33 Correct 374 ms 27196 KB Output is correct
34 Correct 271 ms 24236 KB Output is correct
35 Correct 378 ms 27500 KB Output is correct
36 Correct 42 ms 9416 KB Output is correct
37 Correct 317 ms 27172 KB Output is correct
38 Correct 260 ms 22184 KB Output is correct
39 Correct 108 ms 12924 KB Output is correct
40 Correct 126 ms 13140 KB Output is correct
41 Correct 90 ms 10380 KB Output is correct
42 Correct 78 ms 9972 KB Output is correct
43 Correct 105 ms 9836 KB Output is correct
44 Correct 75 ms 9676 KB Output is correct
45 Correct 76 ms 10012 KB Output is correct
46 Correct 72 ms 9644 KB Output is correct
47 Correct 78 ms 9920 KB Output is correct
48 Correct 72 ms 9656 KB Output is correct
49 Correct 96 ms 9852 KB Output is correct
50 Correct 66 ms 9452 KB Output is correct
51 Correct 64 ms 9672 KB Output is correct
52 Correct 61 ms 9508 KB Output is correct
53 Correct 64 ms 9292 KB Output is correct
54 Correct 87 ms 9600 KB Output is correct
55 Correct 77 ms 9244 KB Output is correct
56 Correct 33 ms 7380 KB Output is correct
57 Correct 76 ms 9132 KB Output is correct
58 Correct 72 ms 8348 KB Output is correct
59 Correct 20 ms 5088 KB Output is correct
60 Correct 20 ms 5204 KB Output is correct
61 Correct 698 ms 34912 KB Output is correct
62 Correct 773 ms 33832 KB Output is correct
63 Correct 617 ms 32924 KB Output is correct
64 Correct 582 ms 32668 KB Output is correct
65 Correct 546 ms 33936 KB Output is correct
66 Correct 587 ms 32584 KB Output is correct
67 Correct 401 ms 31984 KB Output is correct
68 Correct 455 ms 31008 KB Output is correct
69 Correct 446 ms 32928 KB Output is correct
70 Correct 471 ms 29836 KB Output is correct
71 Correct 374 ms 31836 KB Output is correct
72 Correct 426 ms 30176 KB Output is correct
73 Correct 406 ms 31184 KB Output is correct
74 Correct 393 ms 32408 KB Output is correct
75 Correct 454 ms 31244 KB Output is correct
76 Correct 38 ms 9428 KB Output is correct
77 Correct 408 ms 27252 KB Output is correct
78 Correct 353 ms 25656 KB Output is correct
79 Correct 24 ms 5076 KB Output is correct
80 Correct 20 ms 5084 KB Output is correct