Submission #654037

# Submission time Handle Problem Language Result Execution time Memory
654037 2022-10-29T15:40:58 Z Vladth11 Abracadabra (CEOI22_abracadabra) C++17
100 / 100
656 ms 34916 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 << " "
 
#pragma GCC optimize("Ofast")
 
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 309 ms 23228 KB Output is correct
2 Correct 308 ms 22096 KB Output is correct
3 Correct 300 ms 21568 KB Output is correct
4 Correct 294 ms 21304 KB Output is correct
5 Correct 301 ms 23580 KB Output is correct
6 Correct 299 ms 24228 KB Output is correct
7 Correct 329 ms 24528 KB Output is correct
8 Correct 292 ms 22812 KB Output is correct
9 Correct 284 ms 21776 KB Output is correct
10 Correct 299 ms 21540 KB Output is correct
11 Correct 297 ms 21864 KB Output is correct
12 Correct 281 ms 20164 KB Output is correct
13 Correct 296 ms 20968 KB Output is correct
14 Correct 306 ms 23112 KB Output is correct
15 Correct 307 ms 21372 KB Output is correct
16 Correct 24 ms 5076 KB Output is correct
17 Correct 281 ms 20344 KB Output is correct
18 Correct 278 ms 20132 KB Output is correct
19 Correct 19 ms 5076 KB Output is correct
20 Correct 19 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 375 ms 28228 KB Output is correct
2 Correct 361 ms 28164 KB Output is correct
3 Correct 294 ms 23932 KB Output is correct
4 Correct 313 ms 24096 KB Output is correct
5 Correct 292 ms 24388 KB Output is correct
6 Correct 297 ms 23664 KB Output is correct
7 Correct 334 ms 27952 KB Output is correct
8 Correct 348 ms 27080 KB Output is correct
9 Correct 282 ms 24220 KB Output is correct
10 Correct 339 ms 26860 KB Output is correct
11 Correct 276 ms 23940 KB Output is correct
12 Correct 303 ms 23808 KB Output is correct
13 Correct 342 ms 27052 KB Output is correct
14 Correct 282 ms 24236 KB Output is correct
15 Correct 358 ms 27536 KB Output is correct
16 Correct 38 ms 9408 KB Output is correct
17 Correct 320 ms 27136 KB Output is correct
18 Correct 256 ms 22152 KB Output is correct
19 Correct 101 ms 12868 KB Output is correct
20 Correct 115 ms 13080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 97 ms 10428 KB Output is correct
2 Correct 78 ms 10012 KB Output is correct
3 Correct 83 ms 9868 KB Output is correct
4 Correct 72 ms 9668 KB Output is correct
5 Correct 80 ms 10012 KB Output is correct
6 Correct 76 ms 9612 KB Output is correct
7 Correct 77 ms 9980 KB Output is correct
8 Correct 72 ms 9600 KB Output is correct
9 Correct 75 ms 9852 KB Output is correct
10 Correct 64 ms 9400 KB Output is correct
11 Correct 70 ms 9624 KB Output is correct
12 Correct 67 ms 9520 KB Output is correct
13 Correct 64 ms 9292 KB Output is correct
14 Correct 66 ms 9560 KB Output is correct
15 Correct 65 ms 9240 KB Output is correct
16 Correct 32 ms 7380 KB Output is correct
17 Correct 67 ms 9072 KB Output is correct
18 Correct 57 ms 8348 KB Output is correct
19 Correct 20 ms 5076 KB Output is correct
20 Correct 18 ms 5080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 309 ms 23228 KB Output is correct
2 Correct 308 ms 22096 KB Output is correct
3 Correct 300 ms 21568 KB Output is correct
4 Correct 294 ms 21304 KB Output is correct
5 Correct 301 ms 23580 KB Output is correct
6 Correct 299 ms 24228 KB Output is correct
7 Correct 329 ms 24528 KB Output is correct
8 Correct 292 ms 22812 KB Output is correct
9 Correct 284 ms 21776 KB Output is correct
10 Correct 299 ms 21540 KB Output is correct
11 Correct 297 ms 21864 KB Output is correct
12 Correct 281 ms 20164 KB Output is correct
13 Correct 296 ms 20968 KB Output is correct
14 Correct 306 ms 23112 KB Output is correct
15 Correct 307 ms 21372 KB Output is correct
16 Correct 24 ms 5076 KB Output is correct
17 Correct 281 ms 20344 KB Output is correct
18 Correct 278 ms 20132 KB Output is correct
19 Correct 19 ms 5076 KB Output is correct
20 Correct 19 ms 5076 KB Output is correct
21 Correct 375 ms 28228 KB Output is correct
22 Correct 361 ms 28164 KB Output is correct
23 Correct 294 ms 23932 KB Output is correct
24 Correct 313 ms 24096 KB Output is correct
25 Correct 292 ms 24388 KB Output is correct
26 Correct 297 ms 23664 KB Output is correct
27 Correct 334 ms 27952 KB Output is correct
28 Correct 348 ms 27080 KB Output is correct
29 Correct 282 ms 24220 KB Output is correct
30 Correct 339 ms 26860 KB Output is correct
31 Correct 276 ms 23940 KB Output is correct
32 Correct 303 ms 23808 KB Output is correct
33 Correct 342 ms 27052 KB Output is correct
34 Correct 282 ms 24236 KB Output is correct
35 Correct 358 ms 27536 KB Output is correct
36 Correct 38 ms 9408 KB Output is correct
37 Correct 320 ms 27136 KB Output is correct
38 Correct 256 ms 22152 KB Output is correct
39 Correct 101 ms 12868 KB Output is correct
40 Correct 115 ms 13080 KB Output is correct
41 Correct 97 ms 10428 KB Output is correct
42 Correct 78 ms 10012 KB Output is correct
43 Correct 83 ms 9868 KB Output is correct
44 Correct 72 ms 9668 KB Output is correct
45 Correct 80 ms 10012 KB Output is correct
46 Correct 76 ms 9612 KB Output is correct
47 Correct 77 ms 9980 KB Output is correct
48 Correct 72 ms 9600 KB Output is correct
49 Correct 75 ms 9852 KB Output is correct
50 Correct 64 ms 9400 KB Output is correct
51 Correct 70 ms 9624 KB Output is correct
52 Correct 67 ms 9520 KB Output is correct
53 Correct 64 ms 9292 KB Output is correct
54 Correct 66 ms 9560 KB Output is correct
55 Correct 65 ms 9240 KB Output is correct
56 Correct 32 ms 7380 KB Output is correct
57 Correct 67 ms 9072 KB Output is correct
58 Correct 57 ms 8348 KB Output is correct
59 Correct 20 ms 5076 KB Output is correct
60 Correct 18 ms 5080 KB Output is correct
61 Correct 656 ms 34916 KB Output is correct
62 Correct 601 ms 33780 KB Output is correct
63 Correct 595 ms 32844 KB Output is correct
64 Correct 482 ms 32636 KB Output is correct
65 Correct 500 ms 33992 KB Output is correct
66 Correct 524 ms 32720 KB Output is correct
67 Correct 417 ms 32072 KB Output is correct
68 Correct 435 ms 30896 KB Output is correct
69 Correct 451 ms 32916 KB Output is correct
70 Correct 386 ms 29716 KB Output is correct
71 Correct 349 ms 31880 KB Output is correct
72 Correct 415 ms 30188 KB Output is correct
73 Correct 382 ms 31116 KB Output is correct
74 Correct 385 ms 32468 KB Output is correct
75 Correct 396 ms 31284 KB Output is correct
76 Correct 37 ms 9428 KB Output is correct
77 Correct 404 ms 27232 KB Output is correct
78 Correct 336 ms 25640 KB Output is correct
79 Correct 19 ms 5076 KB Output is correct
80 Correct 18 ms 5076 KB Output is correct