#include <stdio.h>
#include <stdlib.h>
#define N 200000
#define N_ (1 << 18) /* N_ = pow2(ceil(log2(N))) */
#define Q 1000000
int min(int a, int b) { return a < b ? a : b; }
int *eh[N + 1], eo[N + 1];
void append(int t, int h) {
int o = eo[t]++;
if (o >= 2 && (o & o - 1) == 0)
eh[t] = (int *) realloc(eh[t], o * 2 * sizeof *eh[t]);
eh[t][o] = h;
}
int st[N_ * 2], n_;
void pul(int i) {
st[i] = st[i << 1 | 0] + st[i << 1 | 1];
}
void update(int i, int x) {
st[i += n_] = x;
while (i > 1)
pul(i >>= 1);
}
int x, y, z;
void query(int k) {
int i = 1;
while (i < n_)
if (k < st[i << 1 | 0])
i = i << 1 | 0;
else
k -= st[i << 1 | 0], i = i << 1 | 1;
x = i - n_, y = k, z = st[i];
}
int main() {
static int aa[N], ii[N], nxt[N], qu[N], ii_[Q], ans[Q];
int n, q, cnt, h, i, j, t, o;
scanf("%d%d", &n, &q);
for (i = 0; i < n; i++) {
scanf("%d", &aa[i]), aa[i]--;
ii[aa[i]] = i;
}
cnt = 0;
for (i = n - 1; i >= 0; i--) {
while (cnt && aa[i] > aa[qu[cnt - 1]])
cnt--;
nxt[i] = cnt == 0 ? n : qu[cnt - 1];
qu[cnt++] = i;
}
for (t = 0; t <= n; t++)
eh[t] = (int *) malloc(2 * sizeof *eh[t]);
for (h = 0; h < q; h++) {
scanf("%d%d", &t, &ii_[h]), ii_[h]--, t = min(t, n);
append(t, h);
}
n_ = 1;
while (n_ < n)
n_ <<= 1;
i = 0;
while (i < n)
update(aa[i], nxt[i] - i), i = nxt[i];
for (t = 0; t <= n; t++) {
for (o = eo[t]; o--; ) {
h = eh[t][o];
query(ii_[h]);
ans[h] = aa[ii[x] + y] + 1;
}
query(n / 2);
i = ii[x];
update(x, y);
for (j = i + y; j < i + z; j = nxt[j])
update(aa[j], min(nxt[j], i + z) - j);
}
for (h = 0; h < q; h++)
printf("%d\n", ans[h]);
return 0;
}
Compilation message
Main.c: In function 'append':
Main.c:15:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
15 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
Main.c: In function 'main':
Main.c:49:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf("%d%d", &n, &q);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:51:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | scanf("%d", &aa[i]), aa[i]--;
| ^~~~~~~~~~~~~~~~~~~
Main.c:64:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | scanf("%d%d", &t, &ii_[h]), ii_[h]--, t = min(t, n);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
320 ms |
25216 KB |
Output is correct |
2 |
Correct |
344 ms |
24104 KB |
Output is correct |
3 |
Correct |
323 ms |
23512 KB |
Output is correct |
4 |
Correct |
238 ms |
22836 KB |
Output is correct |
5 |
Correct |
289 ms |
24796 KB |
Output is correct |
6 |
Correct |
253 ms |
24100 KB |
Output is correct |
7 |
Correct |
289 ms |
25552 KB |
Output is correct |
8 |
Correct |
271 ms |
23428 KB |
Output is correct |
9 |
Correct |
249 ms |
23428 KB |
Output is correct |
10 |
Correct |
254 ms |
22740 KB |
Output is correct |
11 |
Correct |
286 ms |
22692 KB |
Output is correct |
12 |
Correct |
233 ms |
21160 KB |
Output is correct |
13 |
Correct |
263 ms |
22488 KB |
Output is correct |
14 |
Correct |
275 ms |
23872 KB |
Output is correct |
15 |
Correct |
248 ms |
23108 KB |
Output is correct |
16 |
Correct |
1 ms |
292 KB |
Output is correct |
17 |
Correct |
281 ms |
21164 KB |
Output is correct |
18 |
Correct |
235 ms |
21000 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
409 ms |
44604 KB |
Output is correct |
2 |
Correct |
401 ms |
44440 KB |
Output is correct |
3 |
Correct |
316 ms |
36940 KB |
Output is correct |
4 |
Correct |
314 ms |
36724 KB |
Output is correct |
5 |
Correct |
308 ms |
37584 KB |
Output is correct |
6 |
Correct |
313 ms |
36144 KB |
Output is correct |
7 |
Correct |
387 ms |
43492 KB |
Output is correct |
8 |
Correct |
348 ms |
41684 KB |
Output is correct |
9 |
Correct |
395 ms |
37380 KB |
Output is correct |
10 |
Correct |
348 ms |
40576 KB |
Output is correct |
11 |
Correct |
312 ms |
35644 KB |
Output is correct |
12 |
Correct |
299 ms |
35332 KB |
Output is correct |
13 |
Correct |
331 ms |
39936 KB |
Output is correct |
14 |
Correct |
315 ms |
36568 KB |
Output is correct |
15 |
Correct |
450 ms |
41680 KB |
Output is correct |
16 |
Correct |
52 ms |
13196 KB |
Output is correct |
17 |
Correct |
404 ms |
38948 KB |
Output is correct |
18 |
Correct |
261 ms |
31900 KB |
Output is correct |
19 |
Correct |
142 ms |
18244 KB |
Output is correct |
20 |
Correct |
118 ms |
19496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
9604 KB |
Output is correct |
2 |
Correct |
76 ms |
9936 KB |
Output is correct |
3 |
Correct |
65 ms |
9512 KB |
Output is correct |
4 |
Correct |
54 ms |
9484 KB |
Output is correct |
5 |
Correct |
59 ms |
9584 KB |
Output is correct |
6 |
Correct |
56 ms |
9324 KB |
Output is correct |
7 |
Correct |
83 ms |
9548 KB |
Output is correct |
8 |
Correct |
58 ms |
9292 KB |
Output is correct |
9 |
Correct |
58 ms |
9484 KB |
Output is correct |
10 |
Correct |
52 ms |
9280 KB |
Output is correct |
11 |
Correct |
55 ms |
9672 KB |
Output is correct |
12 |
Correct |
50 ms |
9312 KB |
Output is correct |
13 |
Correct |
53 ms |
9348 KB |
Output is correct |
14 |
Correct |
52 ms |
9640 KB |
Output is correct |
15 |
Correct |
50 ms |
9416 KB |
Output is correct |
16 |
Correct |
22 ms |
6740 KB |
Output is correct |
17 |
Correct |
57 ms |
9492 KB |
Output is correct |
18 |
Correct |
50 ms |
8388 KB |
Output is correct |
19 |
Correct |
1 ms |
292 KB |
Output is correct |
20 |
Correct |
1 ms |
288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
320 ms |
25216 KB |
Output is correct |
2 |
Correct |
344 ms |
24104 KB |
Output is correct |
3 |
Correct |
323 ms |
23512 KB |
Output is correct |
4 |
Correct |
238 ms |
22836 KB |
Output is correct |
5 |
Correct |
289 ms |
24796 KB |
Output is correct |
6 |
Correct |
253 ms |
24100 KB |
Output is correct |
7 |
Correct |
289 ms |
25552 KB |
Output is correct |
8 |
Correct |
271 ms |
23428 KB |
Output is correct |
9 |
Correct |
249 ms |
23428 KB |
Output is correct |
10 |
Correct |
254 ms |
22740 KB |
Output is correct |
11 |
Correct |
286 ms |
22692 KB |
Output is correct |
12 |
Correct |
233 ms |
21160 KB |
Output is correct |
13 |
Correct |
263 ms |
22488 KB |
Output is correct |
14 |
Correct |
275 ms |
23872 KB |
Output is correct |
15 |
Correct |
248 ms |
23108 KB |
Output is correct |
16 |
Correct |
1 ms |
292 KB |
Output is correct |
17 |
Correct |
281 ms |
21164 KB |
Output is correct |
18 |
Correct |
235 ms |
21000 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
409 ms |
44604 KB |
Output is correct |
22 |
Correct |
401 ms |
44440 KB |
Output is correct |
23 |
Correct |
316 ms |
36940 KB |
Output is correct |
24 |
Correct |
314 ms |
36724 KB |
Output is correct |
25 |
Correct |
308 ms |
37584 KB |
Output is correct |
26 |
Correct |
313 ms |
36144 KB |
Output is correct |
27 |
Correct |
387 ms |
43492 KB |
Output is correct |
28 |
Correct |
348 ms |
41684 KB |
Output is correct |
29 |
Correct |
395 ms |
37380 KB |
Output is correct |
30 |
Correct |
348 ms |
40576 KB |
Output is correct |
31 |
Correct |
312 ms |
35644 KB |
Output is correct |
32 |
Correct |
299 ms |
35332 KB |
Output is correct |
33 |
Correct |
331 ms |
39936 KB |
Output is correct |
34 |
Correct |
315 ms |
36568 KB |
Output is correct |
35 |
Correct |
450 ms |
41680 KB |
Output is correct |
36 |
Correct |
52 ms |
13196 KB |
Output is correct |
37 |
Correct |
404 ms |
38948 KB |
Output is correct |
38 |
Correct |
261 ms |
31900 KB |
Output is correct |
39 |
Correct |
142 ms |
18244 KB |
Output is correct |
40 |
Correct |
118 ms |
19496 KB |
Output is correct |
41 |
Correct |
107 ms |
9604 KB |
Output is correct |
42 |
Correct |
76 ms |
9936 KB |
Output is correct |
43 |
Correct |
65 ms |
9512 KB |
Output is correct |
44 |
Correct |
54 ms |
9484 KB |
Output is correct |
45 |
Correct |
59 ms |
9584 KB |
Output is correct |
46 |
Correct |
56 ms |
9324 KB |
Output is correct |
47 |
Correct |
83 ms |
9548 KB |
Output is correct |
48 |
Correct |
58 ms |
9292 KB |
Output is correct |
49 |
Correct |
58 ms |
9484 KB |
Output is correct |
50 |
Correct |
52 ms |
9280 KB |
Output is correct |
51 |
Correct |
55 ms |
9672 KB |
Output is correct |
52 |
Correct |
50 ms |
9312 KB |
Output is correct |
53 |
Correct |
53 ms |
9348 KB |
Output is correct |
54 |
Correct |
52 ms |
9640 KB |
Output is correct |
55 |
Correct |
50 ms |
9416 KB |
Output is correct |
56 |
Correct |
22 ms |
6740 KB |
Output is correct |
57 |
Correct |
57 ms |
9492 KB |
Output is correct |
58 |
Correct |
50 ms |
8388 KB |
Output is correct |
59 |
Correct |
1 ms |
292 KB |
Output is correct |
60 |
Correct |
1 ms |
288 KB |
Output is correct |
61 |
Correct |
673 ms |
44876 KB |
Output is correct |
62 |
Correct |
654 ms |
46172 KB |
Output is correct |
63 |
Correct |
640 ms |
43828 KB |
Output is correct |
64 |
Correct |
552 ms |
43808 KB |
Output is correct |
65 |
Correct |
587 ms |
45604 KB |
Output is correct |
66 |
Correct |
526 ms |
43584 KB |
Output is correct |
67 |
Correct |
509 ms |
44844 KB |
Output is correct |
68 |
Correct |
485 ms |
43176 KB |
Output is correct |
69 |
Correct |
521 ms |
44336 KB |
Output is correct |
70 |
Correct |
465 ms |
42196 KB |
Output is correct |
71 |
Correct |
415 ms |
43300 KB |
Output is correct |
72 |
Correct |
478 ms |
42344 KB |
Output is correct |
73 |
Correct |
440 ms |
42700 KB |
Output is correct |
74 |
Correct |
458 ms |
44336 KB |
Output is correct |
75 |
Correct |
482 ms |
43568 KB |
Output is correct |
76 |
Correct |
51 ms |
13132 KB |
Output is correct |
77 |
Correct |
435 ms |
39244 KB |
Output is correct |
78 |
Correct |
321 ms |
36856 KB |
Output is correct |
79 |
Correct |
0 ms |
288 KB |
Output is correct |
80 |
Correct |
0 ms |
340 KB |
Output is correct |