답안 #698243

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
698243 2023-02-12T23:40:06 Z Kahou Abracadabra (CEOI22_abracadabra) C++14
100 / 100
583 ms 47276 KB
/* In the name of God, aka Allah */
// Believe me - Takeshi Abo
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define endl '\n'
#define mk make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 2e5 + 50, M = 1e6 + 50, LG = 20;
int n, q, par[N], out[M];
vector<pii> vc[N];
int p[N], pp[N];
int fen[N];

void upd(int i, int x) {
        for (; i <= n; i += i&-i) fen[i] += x;
}
int get(int i) {
        int out = 0;
        for (; i > 0; i -= i&-i) out += fen[i];
        return out;
}
int getpos(int x) {
        int pos = 0;
        for (int k = LG-1; k >= 0; k--) {
                if (pos+(1<<k) <= n && fen[pos+(1<<k)] < x) {
                        pos ^= 1<<k;
                        x -= fen[pos];
                }
        }
        return pos+1;
}

void solve() {
        cin >> n >> q;
        for (int i = 1; i <= n; i++) {
                cin >> p[i];
                pp[p[i]] = i;
        }
        for (int i = 1; i <= q; i++) {
                int t, k;
                cin >> t >> k;
                t = min(t, n);
                vc[t].push_back({k, i});
        }
        for (pii x:vc[0]) out[x.S] = p[x.F];
        for (int i = n; i >= 1; i--) {
                par[i] = i+1;
                while (par[i] <= n && p[i] > p[par[i]]) {
                        par[i] = par[par[i]];
                }
        }
        int pt = 1;
        while (pt <= n/2) {
                upd(p[pt], min(n/2+1, par[pt])-pt);
                pt = par[pt];
        }
        pt = n/2+1;
        while (pt <= n) {
                upd(p[pt], par[pt]-pt);
                pt = par[pt];
        }

        for (int tt = 1; tt <= n; tt++) {
                for (pii x:vc[tt]) {
                        int id = getpos(x.F);
                        out[x.S] = p[x.F-get(id-1)+pp[id]-1];
                }
                int id = getpos(n/2);
                if (get(id) <= n/2) continue;

                int pt = n/2-get(id-1)+pp[id];
                int ft = get(id)-get(id-1)+pp[id]-1;
                upd(id, n/2-get(id));
                while (pt <= ft) {
                        upd(p[pt], min(par[pt], ft+1)-pt);
                        pt = par[pt];
                }
        }

        for (int i = 1; i <= q; i++) {
                cout << out[i] << endl;
        }
}
int main() {
        ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        solve();
        return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 252 ms 23256 KB Output is correct
2 Correct 278 ms 22416 KB Output is correct
3 Correct 272 ms 21972 KB Output is correct
4 Correct 203 ms 21840 KB Output is correct
5 Correct 241 ms 24044 KB Output is correct
6 Correct 223 ms 24524 KB Output is correct
7 Correct 227 ms 24896 KB Output is correct
8 Correct 230 ms 23248 KB Output is correct
9 Correct 215 ms 22276 KB Output is correct
10 Correct 215 ms 21956 KB Output is correct
11 Correct 231 ms 22364 KB Output is correct
12 Correct 211 ms 20624 KB Output is correct
13 Correct 214 ms 21432 KB Output is correct
14 Correct 226 ms 23452 KB Output is correct
15 Correct 232 ms 21768 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 174 ms 20768 KB Output is correct
18 Correct 187 ms 20548 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 318 ms 26504 KB Output is correct
2 Correct 311 ms 39872 KB Output is correct
3 Correct 307 ms 33012 KB Output is correct
4 Correct 253 ms 33204 KB Output is correct
5 Correct 250 ms 33700 KB Output is correct
6 Correct 245 ms 32684 KB Output is correct
7 Correct 294 ms 39696 KB Output is correct
8 Correct 291 ms 38220 KB Output is correct
9 Correct 255 ms 33584 KB Output is correct
10 Correct 298 ms 37212 KB Output is correct
11 Correct 240 ms 31876 KB Output is correct
12 Correct 235 ms 31896 KB Output is correct
13 Correct 279 ms 36524 KB Output is correct
14 Correct 293 ms 32748 KB Output is correct
15 Correct 320 ms 38008 KB Output is correct
16 Correct 29 ms 9292 KB Output is correct
17 Correct 237 ms 34704 KB Output is correct
18 Correct 211 ms 29048 KB Output is correct
19 Correct 69 ms 14652 KB Output is correct
20 Correct 95 ms 16040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 9928 KB Output is correct
2 Correct 75 ms 10744 KB Output is correct
3 Correct 64 ms 10816 KB Output is correct
4 Correct 46 ms 10388 KB Output is correct
5 Correct 63 ms 10984 KB Output is correct
6 Correct 50 ms 10388 KB Output is correct
7 Correct 58 ms 10828 KB Output is correct
8 Correct 57 ms 10476 KB Output is correct
9 Correct 50 ms 10644 KB Output is correct
10 Correct 41 ms 10188 KB Output is correct
11 Correct 44 ms 10392 KB Output is correct
12 Correct 41 ms 10128 KB Output is correct
13 Correct 48 ms 10096 KB Output is correct
14 Correct 43 ms 10260 KB Output is correct
15 Correct 41 ms 10116 KB Output is correct
16 Correct 14 ms 7096 KB Output is correct
17 Correct 35 ms 9640 KB Output is correct
18 Correct 34 ms 9280 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 252 ms 23256 KB Output is correct
2 Correct 278 ms 22416 KB Output is correct
3 Correct 272 ms 21972 KB Output is correct
4 Correct 203 ms 21840 KB Output is correct
5 Correct 241 ms 24044 KB Output is correct
6 Correct 223 ms 24524 KB Output is correct
7 Correct 227 ms 24896 KB Output is correct
8 Correct 230 ms 23248 KB Output is correct
9 Correct 215 ms 22276 KB Output is correct
10 Correct 215 ms 21956 KB Output is correct
11 Correct 231 ms 22364 KB Output is correct
12 Correct 211 ms 20624 KB Output is correct
13 Correct 214 ms 21432 KB Output is correct
14 Correct 226 ms 23452 KB Output is correct
15 Correct 232 ms 21768 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 174 ms 20768 KB Output is correct
18 Correct 187 ms 20548 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 318 ms 26504 KB Output is correct
22 Correct 311 ms 39872 KB Output is correct
23 Correct 307 ms 33012 KB Output is correct
24 Correct 253 ms 33204 KB Output is correct
25 Correct 250 ms 33700 KB Output is correct
26 Correct 245 ms 32684 KB Output is correct
27 Correct 294 ms 39696 KB Output is correct
28 Correct 291 ms 38220 KB Output is correct
29 Correct 255 ms 33584 KB Output is correct
30 Correct 298 ms 37212 KB Output is correct
31 Correct 240 ms 31876 KB Output is correct
32 Correct 235 ms 31896 KB Output is correct
33 Correct 279 ms 36524 KB Output is correct
34 Correct 293 ms 32748 KB Output is correct
35 Correct 320 ms 38008 KB Output is correct
36 Correct 29 ms 9292 KB Output is correct
37 Correct 237 ms 34704 KB Output is correct
38 Correct 211 ms 29048 KB Output is correct
39 Correct 69 ms 14652 KB Output is correct
40 Correct 95 ms 16040 KB Output is correct
41 Correct 74 ms 9928 KB Output is correct
42 Correct 75 ms 10744 KB Output is correct
43 Correct 64 ms 10816 KB Output is correct
44 Correct 46 ms 10388 KB Output is correct
45 Correct 63 ms 10984 KB Output is correct
46 Correct 50 ms 10388 KB Output is correct
47 Correct 58 ms 10828 KB Output is correct
48 Correct 57 ms 10476 KB Output is correct
49 Correct 50 ms 10644 KB Output is correct
50 Correct 41 ms 10188 KB Output is correct
51 Correct 44 ms 10392 KB Output is correct
52 Correct 41 ms 10128 KB Output is correct
53 Correct 48 ms 10096 KB Output is correct
54 Correct 43 ms 10260 KB Output is correct
55 Correct 41 ms 10116 KB Output is correct
56 Correct 14 ms 7096 KB Output is correct
57 Correct 35 ms 9640 KB Output is correct
58 Correct 34 ms 9280 KB Output is correct
59 Correct 3 ms 4948 KB Output is correct
60 Correct 3 ms 4948 KB Output is correct
61 Correct 583 ms 47276 KB Output is correct
62 Correct 502 ms 45232 KB Output is correct
63 Correct 579 ms 44144 KB Output is correct
64 Correct 470 ms 43704 KB Output is correct
65 Correct 425 ms 45532 KB Output is correct
66 Correct 402 ms 43724 KB Output is correct
67 Correct 386 ms 43216 KB Output is correct
68 Correct 402 ms 41664 KB Output is correct
69 Correct 461 ms 43816 KB Output is correct
70 Correct 348 ms 40012 KB Output is correct
71 Correct 324 ms 41332 KB Output is correct
72 Correct 331 ms 40452 KB Output is correct
73 Correct 327 ms 41068 KB Output is correct
74 Correct 355 ms 42912 KB Output is correct
75 Correct 338 ms 41616 KB Output is correct
76 Correct 27 ms 9292 KB Output is correct
77 Correct 234 ms 34912 KB Output is correct
78 Correct 273 ms 34044 KB Output is correct
79 Correct 3 ms 5028 KB Output is correct
80 Correct 3 ms 4968 KB Output is correct