#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);
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
308 ms |
23144 KB |
Output is correct |
2 |
Correct |
306 ms |
22000 KB |
Output is correct |
3 |
Correct |
295 ms |
21500 KB |
Output is correct |
4 |
Correct |
316 ms |
21356 KB |
Output is correct |
5 |
Correct |
312 ms |
23612 KB |
Output is correct |
6 |
Correct |
291 ms |
24320 KB |
Output is correct |
7 |
Correct |
298 ms |
24372 KB |
Output is correct |
8 |
Correct |
297 ms |
22860 KB |
Output is correct |
9 |
Correct |
300 ms |
21788 KB |
Output is correct |
10 |
Correct |
286 ms |
21544 KB |
Output is correct |
11 |
Correct |
296 ms |
21952 KB |
Output is correct |
12 |
Correct |
291 ms |
20080 KB |
Output is correct |
13 |
Correct |
294 ms |
21068 KB |
Output is correct |
14 |
Correct |
300 ms |
22996 KB |
Output is correct |
15 |
Correct |
295 ms |
21452 KB |
Output is correct |
16 |
Correct |
23 ms |
5076 KB |
Output is correct |
17 |
Correct |
323 ms |
20292 KB |
Output is correct |
18 |
Correct |
321 ms |
20248 KB |
Output is correct |
19 |
Correct |
19 ms |
5076 KB |
Output is correct |
20 |
Correct |
24 ms |
5088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
417 ms |
28048 KB |
Output is correct |
2 |
Correct |
392 ms |
28260 KB |
Output is correct |
3 |
Correct |
396 ms |
23892 KB |
Output is correct |
4 |
Correct |
349 ms |
24008 KB |
Output is correct |
5 |
Correct |
328 ms |
24344 KB |
Output is correct |
6 |
Correct |
342 ms |
23644 KB |
Output is correct |
7 |
Correct |
389 ms |
28056 KB |
Output is correct |
8 |
Correct |
362 ms |
26904 KB |
Output is correct |
9 |
Correct |
339 ms |
24364 KB |
Output is correct |
10 |
Correct |
543 ms |
26860 KB |
Output is correct |
11 |
Correct |
327 ms |
23908 KB |
Output is correct |
12 |
Correct |
320 ms |
23580 KB |
Output is correct |
13 |
Correct |
374 ms |
27060 KB |
Output is correct |
14 |
Correct |
338 ms |
24312 KB |
Output is correct |
15 |
Correct |
397 ms |
27508 KB |
Output is correct |
16 |
Correct |
55 ms |
9392 KB |
Output is correct |
17 |
Correct |
339 ms |
27140 KB |
Output is correct |
18 |
Correct |
287 ms |
22268 KB |
Output is correct |
19 |
Correct |
106 ms |
12896 KB |
Output is correct |
20 |
Correct |
143 ms |
13068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
10468 KB |
Output is correct |
2 |
Correct |
116 ms |
10012 KB |
Output is correct |
3 |
Correct |
83 ms |
9944 KB |
Output is correct |
4 |
Correct |
85 ms |
9616 KB |
Output is correct |
5 |
Correct |
79 ms |
10060 KB |
Output is correct |
6 |
Correct |
76 ms |
9548 KB |
Output is correct |
7 |
Correct |
113 ms |
9916 KB |
Output is correct |
8 |
Correct |
87 ms |
9596 KB |
Output is correct |
9 |
Correct |
80 ms |
9820 KB |
Output is correct |
10 |
Correct |
69 ms |
9408 KB |
Output is correct |
11 |
Correct |
101 ms |
9688 KB |
Output is correct |
12 |
Correct |
67 ms |
9420 KB |
Output is correct |
13 |
Correct |
82 ms |
9292 KB |
Output is correct |
14 |
Correct |
67 ms |
9560 KB |
Output is correct |
15 |
Correct |
77 ms |
9252 KB |
Output is correct |
16 |
Correct |
37 ms |
7384 KB |
Output is correct |
17 |
Correct |
67 ms |
9144 KB |
Output is correct |
18 |
Correct |
60 ms |
8288 KB |
Output is correct |
19 |
Correct |
19 ms |
5088 KB |
Output is correct |
20 |
Correct |
20 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
308 ms |
23144 KB |
Output is correct |
2 |
Correct |
306 ms |
22000 KB |
Output is correct |
3 |
Correct |
295 ms |
21500 KB |
Output is correct |
4 |
Correct |
316 ms |
21356 KB |
Output is correct |
5 |
Correct |
312 ms |
23612 KB |
Output is correct |
6 |
Correct |
291 ms |
24320 KB |
Output is correct |
7 |
Correct |
298 ms |
24372 KB |
Output is correct |
8 |
Correct |
297 ms |
22860 KB |
Output is correct |
9 |
Correct |
300 ms |
21788 KB |
Output is correct |
10 |
Correct |
286 ms |
21544 KB |
Output is correct |
11 |
Correct |
296 ms |
21952 KB |
Output is correct |
12 |
Correct |
291 ms |
20080 KB |
Output is correct |
13 |
Correct |
294 ms |
21068 KB |
Output is correct |
14 |
Correct |
300 ms |
22996 KB |
Output is correct |
15 |
Correct |
295 ms |
21452 KB |
Output is correct |
16 |
Correct |
23 ms |
5076 KB |
Output is correct |
17 |
Correct |
323 ms |
20292 KB |
Output is correct |
18 |
Correct |
321 ms |
20248 KB |
Output is correct |
19 |
Correct |
19 ms |
5076 KB |
Output is correct |
20 |
Correct |
24 ms |
5088 KB |
Output is correct |
21 |
Correct |
417 ms |
28048 KB |
Output is correct |
22 |
Correct |
392 ms |
28260 KB |
Output is correct |
23 |
Correct |
396 ms |
23892 KB |
Output is correct |
24 |
Correct |
349 ms |
24008 KB |
Output is correct |
25 |
Correct |
328 ms |
24344 KB |
Output is correct |
26 |
Correct |
342 ms |
23644 KB |
Output is correct |
27 |
Correct |
389 ms |
28056 KB |
Output is correct |
28 |
Correct |
362 ms |
26904 KB |
Output is correct |
29 |
Correct |
339 ms |
24364 KB |
Output is correct |
30 |
Correct |
543 ms |
26860 KB |
Output is correct |
31 |
Correct |
327 ms |
23908 KB |
Output is correct |
32 |
Correct |
320 ms |
23580 KB |
Output is correct |
33 |
Correct |
374 ms |
27060 KB |
Output is correct |
34 |
Correct |
338 ms |
24312 KB |
Output is correct |
35 |
Correct |
397 ms |
27508 KB |
Output is correct |
36 |
Correct |
55 ms |
9392 KB |
Output is correct |
37 |
Correct |
339 ms |
27140 KB |
Output is correct |
38 |
Correct |
287 ms |
22268 KB |
Output is correct |
39 |
Correct |
106 ms |
12896 KB |
Output is correct |
40 |
Correct |
143 ms |
13068 KB |
Output is correct |
41 |
Correct |
99 ms |
10468 KB |
Output is correct |
42 |
Correct |
116 ms |
10012 KB |
Output is correct |
43 |
Correct |
83 ms |
9944 KB |
Output is correct |
44 |
Correct |
85 ms |
9616 KB |
Output is correct |
45 |
Correct |
79 ms |
10060 KB |
Output is correct |
46 |
Correct |
76 ms |
9548 KB |
Output is correct |
47 |
Correct |
113 ms |
9916 KB |
Output is correct |
48 |
Correct |
87 ms |
9596 KB |
Output is correct |
49 |
Correct |
80 ms |
9820 KB |
Output is correct |
50 |
Correct |
69 ms |
9408 KB |
Output is correct |
51 |
Correct |
101 ms |
9688 KB |
Output is correct |
52 |
Correct |
67 ms |
9420 KB |
Output is correct |
53 |
Correct |
82 ms |
9292 KB |
Output is correct |
54 |
Correct |
67 ms |
9560 KB |
Output is correct |
55 |
Correct |
77 ms |
9252 KB |
Output is correct |
56 |
Correct |
37 ms |
7384 KB |
Output is correct |
57 |
Correct |
67 ms |
9144 KB |
Output is correct |
58 |
Correct |
60 ms |
8288 KB |
Output is correct |
59 |
Correct |
19 ms |
5088 KB |
Output is correct |
60 |
Correct |
20 ms |
5068 KB |
Output is correct |
61 |
Correct |
746 ms |
34940 KB |
Output is correct |
62 |
Correct |
660 ms |
33748 KB |
Output is correct |
63 |
Correct |
596 ms |
32832 KB |
Output is correct |
64 |
Correct |
552 ms |
32668 KB |
Output is correct |
65 |
Correct |
679 ms |
34044 KB |
Output is correct |
66 |
Correct |
647 ms |
32644 KB |
Output is correct |
67 |
Correct |
458 ms |
32052 KB |
Output is correct |
68 |
Correct |
512 ms |
31000 KB |
Output is correct |
69 |
Correct |
574 ms |
33040 KB |
Output is correct |
70 |
Correct |
482 ms |
29708 KB |
Output is correct |
71 |
Correct |
402 ms |
31748 KB |
Output is correct |
72 |
Correct |
500 ms |
30216 KB |
Output is correct |
73 |
Correct |
430 ms |
31168 KB |
Output is correct |
74 |
Correct |
465 ms |
32352 KB |
Output is correct |
75 |
Correct |
579 ms |
31240 KB |
Output is correct |
76 |
Correct |
40 ms |
9408 KB |
Output is correct |
77 |
Correct |
442 ms |
27184 KB |
Output is correct |
78 |
Correct |
342 ms |
25624 KB |
Output is correct |
79 |
Correct |
21 ms |
5076 KB |
Output is correct |
80 |
Correct |
19 ms |
5084 KB |
Output is correct |