#include <bits/stdc++.h>
#define X first
#define Y second
using namespace std;
typedef long long llint;
const int maxn = 1e6+10;
const int base = 31337;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const int logo = 20;
const int off = 1 << logo;
const int treesiz = off << 1;
int n, q;
int niz[maxn];
int pos[maxn];
int t[maxn], p[maxn];
vector< int > qs[maxn];
int sol[maxn];
int tour[treesiz];
int l[maxn], r[maxn];
void update(int x, int val) {
x += off;
tour[x] = val;
x /= 2;
while (x > 0) tour[x] = tour[x * 2] + tour[x * 2 + 1], x /= 2;
}
int fin(int l, int r, int node, int val) {
if (l == r) return l;
int mid = (l + r) / 2;
if (tour[node * 2] >= val) return fin(l, mid, node * 2, val);
return fin(mid + 1, r, node * 2 + 1, val - tour[node * 2]);
}
int query(int a, int b, int l, int r, int node) {
if (l > b || r < a) return 0;
if (l >= a && r <= b) return tour[node];
int mid = (l + r) / 2;
return query(a, b, l, mid, node * 2) + query(a, b, mid + 1, r, node * 2 + 1);
}
int main() {
scanf("%d%d", &n, &q);
for (int i = 0; i < n; i++)
scanf("%d", niz+i);
for (int i = 0; i < q; i++) {
scanf("%d%d", t+i, p+i);
t[i] = min(t[i], n); p[i]--;
qs[t[i]].push_back(i);
}
niz[n] = inf;
stack< int > s; s.push(n);
for (int i = n - 1; i >= 0; i--) {
while (niz[s.top()] < niz[i]) s.pop();
pos[i] = s.top();
s.push(i);
}
for (int tren : qs[0]) sol[tren] = niz[p[tren]];
int ptr = 0;
memset(r, -1, sizeof r);
while (ptr < n) {
int val = niz[ptr];
l[val] = ptr;
r[val] = pos[ptr] - 1;
update(val, r[val] - l[val] + 1);
//printf("block: %d (%d %d)\n", val, l[val], r[val]);
ptr = pos[ptr];
}
update(n + 1, 100);
for (int ac = 1; ac <= n; ac++) {
int ind = fin(0, off - 1, 1, n / 2 + 1);
//printf("ind: %d\n", ind);
assert(query(1, n, 0, off - 1, 1) == n);
int prev = query(0, ind - 1, 0, off - 1, 1);
//printf("middle: %d --> %d\n", ind, prev);
if (prev != n / 2) {
int ptr = l[ind] + n / 2 - prev;
int cp = ptr;
//printf("ptr: %d\n", ptr);
while (ptr <= r[ind]) {
//printf("current ptr: %d\n", ptr);
int val = niz[ptr];
l[val] = ptr, r[val] = min(pos[ptr], r[ind] + 1) - 1;
update(val, r[val] - l[val] + 1);
//printf("block: %d (%d %d)\n", val, l[val], r[val]);
ptr = pos[ptr];
}
r[ind] = cp - 1;
update(ind, r[ind] - l[ind] + 1);
//printf("reduce block: %d --> %d %d\n", ind, l[ind], r[ind]);
}
for (int tren : qs[ac]) {
//printf("answering: %d\n", tren + 1);
int ind = fin(0, off - 1, 1, p[tren] + 1);
//assert(ind <= n);
//printf("in block: %d (%d %d)\n", ind, l[ind], r[ind]);
//assert(r[ind] - l[ind] >= 0);
int prev = query(0, ind - 1, 0, off - 1, 1);
//printf("prev size: %d\n", prev);
sol[tren] = niz[l[ind] + p[tren] - prev];
}
}
for (int i = 0; i < q; i++)
printf("%d\n", sol[i]);
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:50:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%d%d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:52:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | scanf("%d", niz+i);
| ~~~~~^~~~~~~~~~~~~
Main.cpp:55:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | scanf("%d%d", t+i, p+i);
| ~~~~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
434 ms |
49560 KB |
Output is correct |
2 |
Correct |
434 ms |
51624 KB |
Output is correct |
3 |
Correct |
410 ms |
50856 KB |
Output is correct |
4 |
Correct |
345 ms |
53308 KB |
Output is correct |
5 |
Correct |
440 ms |
54572 KB |
Output is correct |
6 |
Correct |
354 ms |
54360 KB |
Output is correct |
7 |
Correct |
418 ms |
54232 KB |
Output is correct |
8 |
Correct |
367 ms |
52980 KB |
Output is correct |
9 |
Correct |
356 ms |
53336 KB |
Output is correct |
10 |
Correct |
358 ms |
52144 KB |
Output is correct |
11 |
Correct |
381 ms |
52132 KB |
Output is correct |
12 |
Correct |
332 ms |
51076 KB |
Output is correct |
13 |
Correct |
339 ms |
51760 KB |
Output is correct |
14 |
Correct |
404 ms |
54204 KB |
Output is correct |
15 |
Correct |
361 ms |
53844 KB |
Output is correct |
16 |
Correct |
14 ms |
27732 KB |
Output is correct |
17 |
Correct |
265 ms |
52112 KB |
Output is correct |
18 |
Correct |
278 ms |
51976 KB |
Output is correct |
19 |
Correct |
16 ms |
27732 KB |
Output is correct |
20 |
Correct |
15 ms |
27728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
508 ms |
57412 KB |
Output is correct |
2 |
Correct |
495 ms |
67128 KB |
Output is correct |
3 |
Correct |
412 ms |
59620 KB |
Output is correct |
4 |
Correct |
385 ms |
57008 KB |
Output is correct |
5 |
Correct |
400 ms |
57640 KB |
Output is correct |
6 |
Correct |
390 ms |
58596 KB |
Output is correct |
7 |
Correct |
456 ms |
61972 KB |
Output is correct |
8 |
Correct |
469 ms |
63288 KB |
Output is correct |
9 |
Correct |
440 ms |
57132 KB |
Output is correct |
10 |
Correct |
417 ms |
61484 KB |
Output is correct |
11 |
Correct |
354 ms |
55256 KB |
Output is correct |
12 |
Correct |
371 ms |
54552 KB |
Output is correct |
13 |
Correct |
420 ms |
61332 KB |
Output is correct |
14 |
Correct |
367 ms |
59140 KB |
Output is correct |
15 |
Correct |
434 ms |
64904 KB |
Output is correct |
16 |
Correct |
88 ms |
32744 KB |
Output is correct |
17 |
Correct |
326 ms |
60188 KB |
Output is correct |
18 |
Correct |
345 ms |
51432 KB |
Output is correct |
19 |
Correct |
137 ms |
37980 KB |
Output is correct |
20 |
Correct |
164 ms |
38900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
33672 KB |
Output is correct |
2 |
Correct |
116 ms |
32988 KB |
Output is correct |
3 |
Correct |
118 ms |
33028 KB |
Output is correct |
4 |
Correct |
97 ms |
32304 KB |
Output is correct |
5 |
Correct |
107 ms |
32860 KB |
Output is correct |
6 |
Correct |
94 ms |
32332 KB |
Output is correct |
7 |
Correct |
97 ms |
32656 KB |
Output is correct |
8 |
Correct |
101 ms |
32312 KB |
Output is correct |
9 |
Correct |
108 ms |
32504 KB |
Output is correct |
10 |
Correct |
88 ms |
32128 KB |
Output is correct |
11 |
Correct |
89 ms |
32168 KB |
Output is correct |
12 |
Correct |
86 ms |
32076 KB |
Output is correct |
13 |
Correct |
89 ms |
31988 KB |
Output is correct |
14 |
Correct |
86 ms |
32204 KB |
Output is correct |
15 |
Correct |
88 ms |
31964 KB |
Output is correct |
16 |
Correct |
49 ms |
29748 KB |
Output is correct |
17 |
Correct |
77 ms |
32188 KB |
Output is correct |
18 |
Correct |
74 ms |
30880 KB |
Output is correct |
19 |
Correct |
13 ms |
27732 KB |
Output is correct |
20 |
Correct |
16 ms |
27776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
434 ms |
49560 KB |
Output is correct |
2 |
Correct |
434 ms |
51624 KB |
Output is correct |
3 |
Correct |
410 ms |
50856 KB |
Output is correct |
4 |
Correct |
345 ms |
53308 KB |
Output is correct |
5 |
Correct |
440 ms |
54572 KB |
Output is correct |
6 |
Correct |
354 ms |
54360 KB |
Output is correct |
7 |
Correct |
418 ms |
54232 KB |
Output is correct |
8 |
Correct |
367 ms |
52980 KB |
Output is correct |
9 |
Correct |
356 ms |
53336 KB |
Output is correct |
10 |
Correct |
358 ms |
52144 KB |
Output is correct |
11 |
Correct |
381 ms |
52132 KB |
Output is correct |
12 |
Correct |
332 ms |
51076 KB |
Output is correct |
13 |
Correct |
339 ms |
51760 KB |
Output is correct |
14 |
Correct |
404 ms |
54204 KB |
Output is correct |
15 |
Correct |
361 ms |
53844 KB |
Output is correct |
16 |
Correct |
14 ms |
27732 KB |
Output is correct |
17 |
Correct |
265 ms |
52112 KB |
Output is correct |
18 |
Correct |
278 ms |
51976 KB |
Output is correct |
19 |
Correct |
16 ms |
27732 KB |
Output is correct |
20 |
Correct |
15 ms |
27728 KB |
Output is correct |
21 |
Correct |
508 ms |
57412 KB |
Output is correct |
22 |
Correct |
495 ms |
67128 KB |
Output is correct |
23 |
Correct |
412 ms |
59620 KB |
Output is correct |
24 |
Correct |
385 ms |
57008 KB |
Output is correct |
25 |
Correct |
400 ms |
57640 KB |
Output is correct |
26 |
Correct |
390 ms |
58596 KB |
Output is correct |
27 |
Correct |
456 ms |
61972 KB |
Output is correct |
28 |
Correct |
469 ms |
63288 KB |
Output is correct |
29 |
Correct |
440 ms |
57132 KB |
Output is correct |
30 |
Correct |
417 ms |
61484 KB |
Output is correct |
31 |
Correct |
354 ms |
55256 KB |
Output is correct |
32 |
Correct |
371 ms |
54552 KB |
Output is correct |
33 |
Correct |
420 ms |
61332 KB |
Output is correct |
34 |
Correct |
367 ms |
59140 KB |
Output is correct |
35 |
Correct |
434 ms |
64904 KB |
Output is correct |
36 |
Correct |
88 ms |
32744 KB |
Output is correct |
37 |
Correct |
326 ms |
60188 KB |
Output is correct |
38 |
Correct |
345 ms |
51432 KB |
Output is correct |
39 |
Correct |
137 ms |
37980 KB |
Output is correct |
40 |
Correct |
164 ms |
38900 KB |
Output is correct |
41 |
Correct |
131 ms |
33672 KB |
Output is correct |
42 |
Correct |
116 ms |
32988 KB |
Output is correct |
43 |
Correct |
118 ms |
33028 KB |
Output is correct |
44 |
Correct |
97 ms |
32304 KB |
Output is correct |
45 |
Correct |
107 ms |
32860 KB |
Output is correct |
46 |
Correct |
94 ms |
32332 KB |
Output is correct |
47 |
Correct |
97 ms |
32656 KB |
Output is correct |
48 |
Correct |
101 ms |
32312 KB |
Output is correct |
49 |
Correct |
108 ms |
32504 KB |
Output is correct |
50 |
Correct |
88 ms |
32128 KB |
Output is correct |
51 |
Correct |
89 ms |
32168 KB |
Output is correct |
52 |
Correct |
86 ms |
32076 KB |
Output is correct |
53 |
Correct |
89 ms |
31988 KB |
Output is correct |
54 |
Correct |
86 ms |
32204 KB |
Output is correct |
55 |
Correct |
88 ms |
31964 KB |
Output is correct |
56 |
Correct |
49 ms |
29748 KB |
Output is correct |
57 |
Correct |
77 ms |
32188 KB |
Output is correct |
58 |
Correct |
74 ms |
30880 KB |
Output is correct |
59 |
Correct |
13 ms |
27732 KB |
Output is correct |
60 |
Correct |
16 ms |
27776 KB |
Output is correct |
61 |
Correct |
891 ms |
69600 KB |
Output is correct |
62 |
Correct |
840 ms |
68412 KB |
Output is correct |
63 |
Correct |
849 ms |
65596 KB |
Output is correct |
64 |
Correct |
648 ms |
64684 KB |
Output is correct |
65 |
Correct |
681 ms |
67852 KB |
Output is correct |
66 |
Correct |
648 ms |
65300 KB |
Output is correct |
67 |
Correct |
613 ms |
65396 KB |
Output is correct |
68 |
Correct |
658 ms |
64660 KB |
Output is correct |
69 |
Correct |
680 ms |
64444 KB |
Output is correct |
70 |
Correct |
607 ms |
63568 KB |
Output is correct |
71 |
Correct |
615 ms |
64408 KB |
Output is correct |
72 |
Correct |
608 ms |
61600 KB |
Output is correct |
73 |
Correct |
605 ms |
62328 KB |
Output is correct |
74 |
Correct |
595 ms |
64668 KB |
Output is correct |
75 |
Correct |
617 ms |
62632 KB |
Output is correct |
76 |
Correct |
90 ms |
32124 KB |
Output is correct |
77 |
Correct |
364 ms |
59212 KB |
Output is correct |
78 |
Correct |
408 ms |
57664 KB |
Output is correct |
79 |
Correct |
17 ms |
27716 KB |
Output is correct |
80 |
Correct |
15 ms |
27772 KB |
Output is correct |