#include <iostream>
#include <vector>
#include <algorithm>
#pragma GCC optimize("Ofast")
#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];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
384 ms |
23204 KB |
Output is correct |
2 |
Correct |
344 ms |
22016 KB |
Output is correct |
3 |
Correct |
315 ms |
21540 KB |
Output is correct |
4 |
Correct |
358 ms |
21356 KB |
Output is correct |
5 |
Correct |
328 ms |
23580 KB |
Output is correct |
6 |
Correct |
317 ms |
24240 KB |
Output is correct |
7 |
Correct |
399 ms |
24364 KB |
Output is correct |
8 |
Correct |
421 ms |
22868 KB |
Output is correct |
9 |
Correct |
341 ms |
21708 KB |
Output is correct |
10 |
Correct |
308 ms |
21484 KB |
Output is correct |
11 |
Correct |
315 ms |
21864 KB |
Output is correct |
12 |
Correct |
329 ms |
20084 KB |
Output is correct |
13 |
Correct |
348 ms |
21004 KB |
Output is correct |
14 |
Correct |
330 ms |
23120 KB |
Output is correct |
15 |
Correct |
329 ms |
21348 KB |
Output is correct |
16 |
Correct |
23 ms |
5076 KB |
Output is correct |
17 |
Correct |
305 ms |
20284 KB |
Output is correct |
18 |
Correct |
318 ms |
20184 KB |
Output is correct |
19 |
Correct |
20 ms |
5076 KB |
Output is correct |
20 |
Correct |
20 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
486 ms |
28040 KB |
Output is correct |
2 |
Correct |
395 ms |
28180 KB |
Output is correct |
3 |
Correct |
333 ms |
23816 KB |
Output is correct |
4 |
Correct |
351 ms |
23956 KB |
Output is correct |
5 |
Correct |
333 ms |
24452 KB |
Output is correct |
6 |
Correct |
315 ms |
23696 KB |
Output is correct |
7 |
Correct |
375 ms |
27984 KB |
Output is correct |
8 |
Correct |
391 ms |
26992 KB |
Output is correct |
9 |
Correct |
321 ms |
24268 KB |
Output is correct |
10 |
Correct |
354 ms |
26964 KB |
Output is correct |
11 |
Correct |
301 ms |
23964 KB |
Output is correct |
12 |
Correct |
341 ms |
23588 KB |
Output is correct |
13 |
Correct |
496 ms |
27032 KB |
Output is correct |
14 |
Correct |
382 ms |
24132 KB |
Output is correct |
15 |
Correct |
464 ms |
27504 KB |
Output is correct |
16 |
Correct |
50 ms |
9420 KB |
Output is correct |
17 |
Correct |
372 ms |
27180 KB |
Output is correct |
18 |
Correct |
289 ms |
22248 KB |
Output is correct |
19 |
Correct |
107 ms |
12816 KB |
Output is correct |
20 |
Correct |
172 ms |
13008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
10388 KB |
Output is correct |
2 |
Correct |
90 ms |
10008 KB |
Output is correct |
3 |
Correct |
124 ms |
9888 KB |
Output is correct |
4 |
Correct |
75 ms |
9604 KB |
Output is correct |
5 |
Correct |
83 ms |
10088 KB |
Output is correct |
6 |
Correct |
76 ms |
9548 KB |
Output is correct |
7 |
Correct |
92 ms |
9920 KB |
Output is correct |
8 |
Correct |
76 ms |
9596 KB |
Output is correct |
9 |
Correct |
85 ms |
9840 KB |
Output is correct |
10 |
Correct |
78 ms |
9448 KB |
Output is correct |
11 |
Correct |
72 ms |
9644 KB |
Output is correct |
12 |
Correct |
64 ms |
9436 KB |
Output is correct |
13 |
Correct |
68 ms |
9292 KB |
Output is correct |
14 |
Correct |
69 ms |
9556 KB |
Output is correct |
15 |
Correct |
62 ms |
9312 KB |
Output is correct |
16 |
Correct |
36 ms |
7380 KB |
Output is correct |
17 |
Correct |
73 ms |
9048 KB |
Output is correct |
18 |
Correct |
71 ms |
8380 KB |
Output is correct |
19 |
Correct |
20 ms |
5076 KB |
Output is correct |
20 |
Correct |
19 ms |
5084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
384 ms |
23204 KB |
Output is correct |
2 |
Correct |
344 ms |
22016 KB |
Output is correct |
3 |
Correct |
315 ms |
21540 KB |
Output is correct |
4 |
Correct |
358 ms |
21356 KB |
Output is correct |
5 |
Correct |
328 ms |
23580 KB |
Output is correct |
6 |
Correct |
317 ms |
24240 KB |
Output is correct |
7 |
Correct |
399 ms |
24364 KB |
Output is correct |
8 |
Correct |
421 ms |
22868 KB |
Output is correct |
9 |
Correct |
341 ms |
21708 KB |
Output is correct |
10 |
Correct |
308 ms |
21484 KB |
Output is correct |
11 |
Correct |
315 ms |
21864 KB |
Output is correct |
12 |
Correct |
329 ms |
20084 KB |
Output is correct |
13 |
Correct |
348 ms |
21004 KB |
Output is correct |
14 |
Correct |
330 ms |
23120 KB |
Output is correct |
15 |
Correct |
329 ms |
21348 KB |
Output is correct |
16 |
Correct |
23 ms |
5076 KB |
Output is correct |
17 |
Correct |
305 ms |
20284 KB |
Output is correct |
18 |
Correct |
318 ms |
20184 KB |
Output is correct |
19 |
Correct |
20 ms |
5076 KB |
Output is correct |
20 |
Correct |
20 ms |
5076 KB |
Output is correct |
21 |
Correct |
486 ms |
28040 KB |
Output is correct |
22 |
Correct |
395 ms |
28180 KB |
Output is correct |
23 |
Correct |
333 ms |
23816 KB |
Output is correct |
24 |
Correct |
351 ms |
23956 KB |
Output is correct |
25 |
Correct |
333 ms |
24452 KB |
Output is correct |
26 |
Correct |
315 ms |
23696 KB |
Output is correct |
27 |
Correct |
375 ms |
27984 KB |
Output is correct |
28 |
Correct |
391 ms |
26992 KB |
Output is correct |
29 |
Correct |
321 ms |
24268 KB |
Output is correct |
30 |
Correct |
354 ms |
26964 KB |
Output is correct |
31 |
Correct |
301 ms |
23964 KB |
Output is correct |
32 |
Correct |
341 ms |
23588 KB |
Output is correct |
33 |
Correct |
496 ms |
27032 KB |
Output is correct |
34 |
Correct |
382 ms |
24132 KB |
Output is correct |
35 |
Correct |
464 ms |
27504 KB |
Output is correct |
36 |
Correct |
50 ms |
9420 KB |
Output is correct |
37 |
Correct |
372 ms |
27180 KB |
Output is correct |
38 |
Correct |
289 ms |
22248 KB |
Output is correct |
39 |
Correct |
107 ms |
12816 KB |
Output is correct |
40 |
Correct |
172 ms |
13008 KB |
Output is correct |
41 |
Correct |
101 ms |
10388 KB |
Output is correct |
42 |
Correct |
90 ms |
10008 KB |
Output is correct |
43 |
Correct |
124 ms |
9888 KB |
Output is correct |
44 |
Correct |
75 ms |
9604 KB |
Output is correct |
45 |
Correct |
83 ms |
10088 KB |
Output is correct |
46 |
Correct |
76 ms |
9548 KB |
Output is correct |
47 |
Correct |
92 ms |
9920 KB |
Output is correct |
48 |
Correct |
76 ms |
9596 KB |
Output is correct |
49 |
Correct |
85 ms |
9840 KB |
Output is correct |
50 |
Correct |
78 ms |
9448 KB |
Output is correct |
51 |
Correct |
72 ms |
9644 KB |
Output is correct |
52 |
Correct |
64 ms |
9436 KB |
Output is correct |
53 |
Correct |
68 ms |
9292 KB |
Output is correct |
54 |
Correct |
69 ms |
9556 KB |
Output is correct |
55 |
Correct |
62 ms |
9312 KB |
Output is correct |
56 |
Correct |
36 ms |
7380 KB |
Output is correct |
57 |
Correct |
73 ms |
9048 KB |
Output is correct |
58 |
Correct |
71 ms |
8380 KB |
Output is correct |
59 |
Correct |
20 ms |
5076 KB |
Output is correct |
60 |
Correct |
19 ms |
5084 KB |
Output is correct |
61 |
Correct |
756 ms |
35032 KB |
Output is correct |
62 |
Correct |
605 ms |
33764 KB |
Output is correct |
63 |
Correct |
677 ms |
32888 KB |
Output is correct |
64 |
Correct |
570 ms |
32668 KB |
Output is correct |
65 |
Correct |
643 ms |
33932 KB |
Output is correct |
66 |
Correct |
521 ms |
32624 KB |
Output is correct |
67 |
Correct |
449 ms |
32032 KB |
Output is correct |
68 |
Correct |
489 ms |
30872 KB |
Output is correct |
69 |
Correct |
472 ms |
33008 KB |
Output is correct |
70 |
Correct |
408 ms |
29740 KB |
Output is correct |
71 |
Correct |
357 ms |
31764 KB |
Output is correct |
72 |
Correct |
395 ms |
30184 KB |
Output is correct |
73 |
Correct |
425 ms |
31236 KB |
Output is correct |
74 |
Correct |
377 ms |
32456 KB |
Output is correct |
75 |
Correct |
407 ms |
31212 KB |
Output is correct |
76 |
Correct |
39 ms |
9476 KB |
Output is correct |
77 |
Correct |
407 ms |
27220 KB |
Output is correct |
78 |
Correct |
323 ms |
25824 KB |
Output is correct |
79 |
Correct |
18 ms |
5076 KB |
Output is correct |
80 |
Correct |
19 ms |
5076 KB |
Output is correct |