#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 * 4;
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
385 ms |
37392 KB |
Output is correct |
2 |
Correct |
392 ms |
36152 KB |
Output is correct |
3 |
Correct |
392 ms |
35592 KB |
Output is correct |
4 |
Correct |
364 ms |
35372 KB |
Output is correct |
5 |
Correct |
372 ms |
37780 KB |
Output is correct |
6 |
Correct |
372 ms |
38256 KB |
Output is correct |
7 |
Correct |
386 ms |
38588 KB |
Output is correct |
8 |
Correct |
380 ms |
36948 KB |
Output is correct |
9 |
Correct |
369 ms |
35864 KB |
Output is correct |
10 |
Correct |
371 ms |
35560 KB |
Output is correct |
11 |
Correct |
375 ms |
36032 KB |
Output is correct |
12 |
Correct |
353 ms |
34232 KB |
Output is correct |
13 |
Correct |
389 ms |
35112 KB |
Output is correct |
14 |
Correct |
391 ms |
37140 KB |
Output is correct |
15 |
Correct |
374 ms |
35436 KB |
Output is correct |
16 |
Correct |
99 ms |
19184 KB |
Output is correct |
17 |
Correct |
351 ms |
34388 KB |
Output is correct |
18 |
Correct |
348 ms |
34304 KB |
Output is correct |
19 |
Correct |
81 ms |
19160 KB |
Output is correct |
20 |
Correct |
86 ms |
19156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
466 ms |
42128 KB |
Output is correct |
2 |
Correct |
446 ms |
42284 KB |
Output is correct |
3 |
Correct |
366 ms |
37912 KB |
Output is correct |
4 |
Correct |
360 ms |
38080 KB |
Output is correct |
5 |
Correct |
367 ms |
38676 KB |
Output is correct |
6 |
Correct |
374 ms |
37796 KB |
Output is correct |
7 |
Correct |
450 ms |
42028 KB |
Output is correct |
8 |
Correct |
427 ms |
41048 KB |
Output is correct |
9 |
Correct |
332 ms |
38424 KB |
Output is correct |
10 |
Correct |
424 ms |
40912 KB |
Output is correct |
11 |
Correct |
341 ms |
38096 KB |
Output is correct |
12 |
Correct |
343 ms |
37676 KB |
Output is correct |
13 |
Correct |
422 ms |
41132 KB |
Output is correct |
14 |
Correct |
344 ms |
38344 KB |
Output is correct |
15 |
Correct |
487 ms |
41548 KB |
Output is correct |
16 |
Correct |
88 ms |
23580 KB |
Output is correct |
17 |
Correct |
395 ms |
41240 KB |
Output is correct |
18 |
Correct |
338 ms |
36284 KB |
Output is correct |
19 |
Correct |
170 ms |
26908 KB |
Output is correct |
20 |
Correct |
175 ms |
27232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
152 ms |
24512 KB |
Output is correct |
2 |
Correct |
140 ms |
24048 KB |
Output is correct |
3 |
Correct |
159 ms |
23988 KB |
Output is correct |
4 |
Correct |
144 ms |
23744 KB |
Output is correct |
5 |
Correct |
146 ms |
24100 KB |
Output is correct |
6 |
Correct |
141 ms |
23712 KB |
Output is correct |
7 |
Correct |
160 ms |
23980 KB |
Output is correct |
8 |
Correct |
144 ms |
23688 KB |
Output is correct |
9 |
Correct |
130 ms |
23936 KB |
Output is correct |
10 |
Correct |
132 ms |
23568 KB |
Output is correct |
11 |
Correct |
133 ms |
23800 KB |
Output is correct |
12 |
Correct |
124 ms |
23500 KB |
Output is correct |
13 |
Correct |
129 ms |
23372 KB |
Output is correct |
14 |
Correct |
148 ms |
23652 KB |
Output is correct |
15 |
Correct |
118 ms |
23300 KB |
Output is correct |
16 |
Correct |
94 ms |
21488 KB |
Output is correct |
17 |
Correct |
136 ms |
23288 KB |
Output is correct |
18 |
Correct |
127 ms |
22448 KB |
Output is correct |
19 |
Correct |
84 ms |
19156 KB |
Output is correct |
20 |
Correct |
85 ms |
19156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
385 ms |
37392 KB |
Output is correct |
2 |
Correct |
392 ms |
36152 KB |
Output is correct |
3 |
Correct |
392 ms |
35592 KB |
Output is correct |
4 |
Correct |
364 ms |
35372 KB |
Output is correct |
5 |
Correct |
372 ms |
37780 KB |
Output is correct |
6 |
Correct |
372 ms |
38256 KB |
Output is correct |
7 |
Correct |
386 ms |
38588 KB |
Output is correct |
8 |
Correct |
380 ms |
36948 KB |
Output is correct |
9 |
Correct |
369 ms |
35864 KB |
Output is correct |
10 |
Correct |
371 ms |
35560 KB |
Output is correct |
11 |
Correct |
375 ms |
36032 KB |
Output is correct |
12 |
Correct |
353 ms |
34232 KB |
Output is correct |
13 |
Correct |
389 ms |
35112 KB |
Output is correct |
14 |
Correct |
391 ms |
37140 KB |
Output is correct |
15 |
Correct |
374 ms |
35436 KB |
Output is correct |
16 |
Correct |
99 ms |
19184 KB |
Output is correct |
17 |
Correct |
351 ms |
34388 KB |
Output is correct |
18 |
Correct |
348 ms |
34304 KB |
Output is correct |
19 |
Correct |
81 ms |
19160 KB |
Output is correct |
20 |
Correct |
86 ms |
19156 KB |
Output is correct |
21 |
Correct |
466 ms |
42128 KB |
Output is correct |
22 |
Correct |
446 ms |
42284 KB |
Output is correct |
23 |
Correct |
366 ms |
37912 KB |
Output is correct |
24 |
Correct |
360 ms |
38080 KB |
Output is correct |
25 |
Correct |
367 ms |
38676 KB |
Output is correct |
26 |
Correct |
374 ms |
37796 KB |
Output is correct |
27 |
Correct |
450 ms |
42028 KB |
Output is correct |
28 |
Correct |
427 ms |
41048 KB |
Output is correct |
29 |
Correct |
332 ms |
38424 KB |
Output is correct |
30 |
Correct |
424 ms |
40912 KB |
Output is correct |
31 |
Correct |
341 ms |
38096 KB |
Output is correct |
32 |
Correct |
343 ms |
37676 KB |
Output is correct |
33 |
Correct |
422 ms |
41132 KB |
Output is correct |
34 |
Correct |
344 ms |
38344 KB |
Output is correct |
35 |
Correct |
487 ms |
41548 KB |
Output is correct |
36 |
Correct |
88 ms |
23580 KB |
Output is correct |
37 |
Correct |
395 ms |
41240 KB |
Output is correct |
38 |
Correct |
338 ms |
36284 KB |
Output is correct |
39 |
Correct |
170 ms |
26908 KB |
Output is correct |
40 |
Correct |
175 ms |
27232 KB |
Output is correct |
41 |
Correct |
152 ms |
24512 KB |
Output is correct |
42 |
Correct |
140 ms |
24048 KB |
Output is correct |
43 |
Correct |
159 ms |
23988 KB |
Output is correct |
44 |
Correct |
144 ms |
23744 KB |
Output is correct |
45 |
Correct |
146 ms |
24100 KB |
Output is correct |
46 |
Correct |
141 ms |
23712 KB |
Output is correct |
47 |
Correct |
160 ms |
23980 KB |
Output is correct |
48 |
Correct |
144 ms |
23688 KB |
Output is correct |
49 |
Correct |
130 ms |
23936 KB |
Output is correct |
50 |
Correct |
132 ms |
23568 KB |
Output is correct |
51 |
Correct |
133 ms |
23800 KB |
Output is correct |
52 |
Correct |
124 ms |
23500 KB |
Output is correct |
53 |
Correct |
129 ms |
23372 KB |
Output is correct |
54 |
Correct |
148 ms |
23652 KB |
Output is correct |
55 |
Correct |
118 ms |
23300 KB |
Output is correct |
56 |
Correct |
94 ms |
21488 KB |
Output is correct |
57 |
Correct |
136 ms |
23288 KB |
Output is correct |
58 |
Correct |
127 ms |
22448 KB |
Output is correct |
59 |
Correct |
84 ms |
19156 KB |
Output is correct |
60 |
Correct |
85 ms |
19156 KB |
Output is correct |
61 |
Correct |
808 ms |
49208 KB |
Output is correct |
62 |
Correct |
629 ms |
47840 KB |
Output is correct |
63 |
Correct |
685 ms |
47088 KB |
Output is correct |
64 |
Correct |
641 ms |
46832 KB |
Output is correct |
65 |
Correct |
618 ms |
48080 KB |
Output is correct |
66 |
Correct |
619 ms |
46744 KB |
Output is correct |
67 |
Correct |
528 ms |
46172 KB |
Output is correct |
68 |
Correct |
572 ms |
44960 KB |
Output is correct |
69 |
Correct |
510 ms |
46984 KB |
Output is correct |
70 |
Correct |
498 ms |
43992 KB |
Output is correct |
71 |
Correct |
436 ms |
46020 KB |
Output is correct |
72 |
Correct |
461 ms |
44272 KB |
Output is correct |
73 |
Correct |
451 ms |
45340 KB |
Output is correct |
74 |
Correct |
491 ms |
46428 KB |
Output is correct |
75 |
Correct |
473 ms |
45564 KB |
Output is correct |
76 |
Correct |
83 ms |
23576 KB |
Output is correct |
77 |
Correct |
495 ms |
41416 KB |
Output is correct |
78 |
Correct |
439 ms |
39712 KB |
Output is correct |
79 |
Correct |
73 ms |
19156 KB |
Output is correct |
80 |
Correct |
78 ms |
19240 KB |
Output is correct |