#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
#include <algorithm>
#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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
318 ms |
23180 KB |
Output is correct |
2 |
Correct |
331 ms |
21964 KB |
Output is correct |
3 |
Correct |
332 ms |
21452 KB |
Output is correct |
4 |
Correct |
296 ms |
21304 KB |
Output is correct |
5 |
Correct |
336 ms |
23628 KB |
Output is correct |
6 |
Correct |
304 ms |
24216 KB |
Output is correct |
7 |
Correct |
302 ms |
24400 KB |
Output is correct |
8 |
Correct |
285 ms |
22792 KB |
Output is correct |
9 |
Correct |
302 ms |
21788 KB |
Output is correct |
10 |
Correct |
294 ms |
21460 KB |
Output is correct |
11 |
Correct |
290 ms |
21868 KB |
Output is correct |
12 |
Correct |
309 ms |
20168 KB |
Output is correct |
13 |
Correct |
305 ms |
21132 KB |
Output is correct |
14 |
Correct |
332 ms |
23044 KB |
Output is correct |
15 |
Correct |
354 ms |
21380 KB |
Output is correct |
16 |
Correct |
25 ms |
5076 KB |
Output is correct |
17 |
Correct |
293 ms |
20280 KB |
Output is correct |
18 |
Correct |
294 ms |
20136 KB |
Output is correct |
19 |
Correct |
24 ms |
5076 KB |
Output is correct |
20 |
Correct |
23 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
407 ms |
28024 KB |
Output is correct |
2 |
Correct |
380 ms |
28224 KB |
Output is correct |
3 |
Correct |
318 ms |
23844 KB |
Output is correct |
4 |
Correct |
317 ms |
23980 KB |
Output is correct |
5 |
Correct |
287 ms |
24428 KB |
Output is correct |
6 |
Correct |
322 ms |
23708 KB |
Output is correct |
7 |
Correct |
350 ms |
28000 KB |
Output is correct |
8 |
Correct |
358 ms |
27040 KB |
Output is correct |
9 |
Correct |
291 ms |
24340 KB |
Output is correct |
10 |
Correct |
358 ms |
26888 KB |
Output is correct |
11 |
Correct |
272 ms |
24000 KB |
Output is correct |
12 |
Correct |
300 ms |
23652 KB |
Output is correct |
13 |
Correct |
342 ms |
27128 KB |
Output is correct |
14 |
Correct |
317 ms |
24356 KB |
Output is correct |
15 |
Correct |
394 ms |
27412 KB |
Output is correct |
16 |
Correct |
38 ms |
9428 KB |
Output is correct |
17 |
Correct |
330 ms |
27132 KB |
Output is correct |
18 |
Correct |
258 ms |
22256 KB |
Output is correct |
19 |
Correct |
114 ms |
12892 KB |
Output is correct |
20 |
Correct |
117 ms |
13044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
10500 KB |
Output is correct |
2 |
Correct |
99 ms |
10012 KB |
Output is correct |
3 |
Correct |
97 ms |
9888 KB |
Output is correct |
4 |
Correct |
71 ms |
9548 KB |
Output is correct |
5 |
Correct |
79 ms |
10036 KB |
Output is correct |
6 |
Correct |
71 ms |
9664 KB |
Output is correct |
7 |
Correct |
85 ms |
9932 KB |
Output is correct |
8 |
Correct |
72 ms |
9652 KB |
Output is correct |
9 |
Correct |
76 ms |
9812 KB |
Output is correct |
10 |
Correct |
65 ms |
9380 KB |
Output is correct |
11 |
Correct |
65 ms |
9720 KB |
Output is correct |
12 |
Correct |
75 ms |
9532 KB |
Output is correct |
13 |
Correct |
67 ms |
9292 KB |
Output is correct |
14 |
Correct |
67 ms |
9588 KB |
Output is correct |
15 |
Correct |
61 ms |
9240 KB |
Output is correct |
16 |
Correct |
32 ms |
7380 KB |
Output is correct |
17 |
Correct |
69 ms |
9024 KB |
Output is correct |
18 |
Correct |
60 ms |
8340 KB |
Output is correct |
19 |
Correct |
19 ms |
5076 KB |
Output is correct |
20 |
Correct |
21 ms |
5040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
318 ms |
23180 KB |
Output is correct |
2 |
Correct |
331 ms |
21964 KB |
Output is correct |
3 |
Correct |
332 ms |
21452 KB |
Output is correct |
4 |
Correct |
296 ms |
21304 KB |
Output is correct |
5 |
Correct |
336 ms |
23628 KB |
Output is correct |
6 |
Correct |
304 ms |
24216 KB |
Output is correct |
7 |
Correct |
302 ms |
24400 KB |
Output is correct |
8 |
Correct |
285 ms |
22792 KB |
Output is correct |
9 |
Correct |
302 ms |
21788 KB |
Output is correct |
10 |
Correct |
294 ms |
21460 KB |
Output is correct |
11 |
Correct |
290 ms |
21868 KB |
Output is correct |
12 |
Correct |
309 ms |
20168 KB |
Output is correct |
13 |
Correct |
305 ms |
21132 KB |
Output is correct |
14 |
Correct |
332 ms |
23044 KB |
Output is correct |
15 |
Correct |
354 ms |
21380 KB |
Output is correct |
16 |
Correct |
25 ms |
5076 KB |
Output is correct |
17 |
Correct |
293 ms |
20280 KB |
Output is correct |
18 |
Correct |
294 ms |
20136 KB |
Output is correct |
19 |
Correct |
24 ms |
5076 KB |
Output is correct |
20 |
Correct |
23 ms |
5076 KB |
Output is correct |
21 |
Correct |
407 ms |
28024 KB |
Output is correct |
22 |
Correct |
380 ms |
28224 KB |
Output is correct |
23 |
Correct |
318 ms |
23844 KB |
Output is correct |
24 |
Correct |
317 ms |
23980 KB |
Output is correct |
25 |
Correct |
287 ms |
24428 KB |
Output is correct |
26 |
Correct |
322 ms |
23708 KB |
Output is correct |
27 |
Correct |
350 ms |
28000 KB |
Output is correct |
28 |
Correct |
358 ms |
27040 KB |
Output is correct |
29 |
Correct |
291 ms |
24340 KB |
Output is correct |
30 |
Correct |
358 ms |
26888 KB |
Output is correct |
31 |
Correct |
272 ms |
24000 KB |
Output is correct |
32 |
Correct |
300 ms |
23652 KB |
Output is correct |
33 |
Correct |
342 ms |
27128 KB |
Output is correct |
34 |
Correct |
317 ms |
24356 KB |
Output is correct |
35 |
Correct |
394 ms |
27412 KB |
Output is correct |
36 |
Correct |
38 ms |
9428 KB |
Output is correct |
37 |
Correct |
330 ms |
27132 KB |
Output is correct |
38 |
Correct |
258 ms |
22256 KB |
Output is correct |
39 |
Correct |
114 ms |
12892 KB |
Output is correct |
40 |
Correct |
117 ms |
13044 KB |
Output is correct |
41 |
Correct |
93 ms |
10500 KB |
Output is correct |
42 |
Correct |
99 ms |
10012 KB |
Output is correct |
43 |
Correct |
97 ms |
9888 KB |
Output is correct |
44 |
Correct |
71 ms |
9548 KB |
Output is correct |
45 |
Correct |
79 ms |
10036 KB |
Output is correct |
46 |
Correct |
71 ms |
9664 KB |
Output is correct |
47 |
Correct |
85 ms |
9932 KB |
Output is correct |
48 |
Correct |
72 ms |
9652 KB |
Output is correct |
49 |
Correct |
76 ms |
9812 KB |
Output is correct |
50 |
Correct |
65 ms |
9380 KB |
Output is correct |
51 |
Correct |
65 ms |
9720 KB |
Output is correct |
52 |
Correct |
75 ms |
9532 KB |
Output is correct |
53 |
Correct |
67 ms |
9292 KB |
Output is correct |
54 |
Correct |
67 ms |
9588 KB |
Output is correct |
55 |
Correct |
61 ms |
9240 KB |
Output is correct |
56 |
Correct |
32 ms |
7380 KB |
Output is correct |
57 |
Correct |
69 ms |
9024 KB |
Output is correct |
58 |
Correct |
60 ms |
8340 KB |
Output is correct |
59 |
Correct |
19 ms |
5076 KB |
Output is correct |
60 |
Correct |
21 ms |
5040 KB |
Output is correct |
61 |
Correct |
794 ms |
35028 KB |
Output is correct |
62 |
Correct |
607 ms |
33820 KB |
Output is correct |
63 |
Correct |
654 ms |
32820 KB |
Output is correct |
64 |
Correct |
534 ms |
32716 KB |
Output is correct |
65 |
Correct |
562 ms |
33960 KB |
Output is correct |
66 |
Correct |
525 ms |
32584 KB |
Output is correct |
67 |
Correct |
470 ms |
31996 KB |
Output is correct |
68 |
Correct |
486 ms |
31040 KB |
Output is correct |
69 |
Correct |
440 ms |
32876 KB |
Output is correct |
70 |
Correct |
438 ms |
29912 KB |
Output is correct |
71 |
Correct |
365 ms |
31808 KB |
Output is correct |
72 |
Correct |
494 ms |
30240 KB |
Output is correct |
73 |
Correct |
417 ms |
31256 KB |
Output is correct |
74 |
Correct |
384 ms |
32424 KB |
Output is correct |
75 |
Correct |
417 ms |
31252 KB |
Output is correct |
76 |
Correct |
51 ms |
9416 KB |
Output is correct |
77 |
Correct |
402 ms |
27308 KB |
Output is correct |
78 |
Correct |
329 ms |
25704 KB |
Output is correct |
79 |
Correct |
19 ms |
5076 KB |
Output is correct |
80 |
Correct |
22 ms |
5076 KB |
Output is correct |