#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef pair<int, int> pii;
const int MAXN = 1 << 18, MAXQ = 1e6 + 5;
struct TourSum
{
int nodes[2 * MAXN];
void update(int ind, int val, int node = 1, int l = 0, int r = MAXN){
if(r - l == 1){
nodes[node] = val;
return;
}
int m = (l + r) / 2;
if(ind < m) update(ind, val, 2 * node, l, m);
else update(ind, val, 2 * node + 1, m, r);
nodes[node] = nodes[2 * node] + nodes[2 * node + 1];
}
int binsearch(int val, int node = 1, int l = 0, int r = MAXN){
if(r - l == 1) return l;
int m = (l + r) / 2;
if(nodes[2 * node] >= val) return binsearch(val, 2 * node, l, m);
return binsearch(val - nodes[2 * node], 2 * node + 1, m, r);
}
int query(int lq, int rq, int node = 1, int l = 0, int r = MAXN){
if(l >= lq && r <= rq) return nodes[node];
if(l >= rq || r <= lq) return 0;
int m = (l + r) / 2;
return query(lq, rq, 2 * node, l, m) + query(lq, rq, 2 * node + 1, m, r);
}
};
int n, q, arr[MAXN], pos[MAXN], maxrt[MAXN];
set<int> posset;
TourSum tour;
vector<pair<pii, int>> qs;
int sol[MAXQ];
int main()
{
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> arr[i];
pos[arr[i]] = i;
}
posset.insert(n + 1);
for(int i = n; i >= 1; i--){
maxrt[pos[i]] = *posset.lower_bound(pos[i]);
posset.insert(pos[i]);
}
for(int i = 1; i <= n; i = maxrt[i]){
tour.update(arr[i], maxrt[i] - i);
}
for(int i = 0; i < q; i++){
int t, p;
cin >> t >> p;
qs.push_back({{t, p}, i});
}
sort(qs.begin(), qs.end());
int qpos = 0;
for(int t = 0; t <= n; t++){
while(qpos < qs.size() && qs[qpos].f.f == t){
int num = tour.binsearch(qs[qpos].f.s);
sol[qs[qpos].s] = arr[pos[num] + qs[qpos].f.s - tour.query(0, num) - 1];
qpos++;
}
if(qpos == qs.size()) break;
int num = tour.binsearch(n / 2);
int realpos = tour.query(0, num) + 1;
int amount = tour.nodes[MAXN + num];
if(realpos + amount == n / 2 + 1) break;
tour.update(num, n / 2 + 1 - realpos);
for(int i = pos[num] + n / 2 + 1 - realpos; i < pos[num] + amount; i = maxrt[i])
tour.update(arr[i], min(maxrt[i], pos[num] + amount) - i);
}
while(qpos < qs.size()){
int num = tour.binsearch(qs[qpos].f.s);
sol[qs[qpos].s] = arr[pos[num] + qs[qpos].f.s - tour.query(0, num) - 1];
qpos++;
}
for(int i = 0; i < q; i++) cout << sol[i] << '\n';
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:82:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | while(qpos < qs.size() && qs[qpos].f.f == t){
| ~~~~~^~~~~~~~~~~
Main.cpp:88:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | if(qpos == qs.size()) break;
| ~~~~~^~~~~~~~~~~~
Main.cpp:102:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | while(qpos < qs.size()){
| ~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
698 ms |
20124 KB |
Output is correct |
2 |
Correct |
704 ms |
23116 KB |
Output is correct |
3 |
Correct |
688 ms |
22376 KB |
Output is correct |
4 |
Correct |
616 ms |
22852 KB |
Output is correct |
5 |
Correct |
675 ms |
23528 KB |
Output is correct |
6 |
Correct |
634 ms |
22768 KB |
Output is correct |
7 |
Correct |
679 ms |
23668 KB |
Output is correct |
8 |
Correct |
637 ms |
22936 KB |
Output is correct |
9 |
Correct |
679 ms |
22728 KB |
Output is correct |
10 |
Correct |
638 ms |
23032 KB |
Output is correct |
11 |
Correct |
647 ms |
22984 KB |
Output is correct |
12 |
Correct |
666 ms |
22804 KB |
Output is correct |
13 |
Correct |
636 ms |
23084 KB |
Output is correct |
14 |
Correct |
644 ms |
23408 KB |
Output is correct |
15 |
Correct |
652 ms |
23712 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
653 ms |
23052 KB |
Output is correct |
18 |
Correct |
622 ms |
22864 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1027 ms |
35564 KB |
Output is correct |
2 |
Correct |
981 ms |
49396 KB |
Output is correct |
3 |
Correct |
837 ms |
40840 KB |
Output is correct |
4 |
Correct |
776 ms |
41488 KB |
Output is correct |
5 |
Correct |
845 ms |
42404 KB |
Output is correct |
6 |
Correct |
784 ms |
40744 KB |
Output is correct |
7 |
Correct |
934 ms |
49000 KB |
Output is correct |
8 |
Correct |
911 ms |
47016 KB |
Output is correct |
9 |
Correct |
847 ms |
42128 KB |
Output is correct |
10 |
Correct |
869 ms |
45856 KB |
Output is correct |
11 |
Correct |
785 ms |
40292 KB |
Output is correct |
12 |
Correct |
770 ms |
39976 KB |
Output is correct |
13 |
Correct |
921 ms |
45264 KB |
Output is correct |
14 |
Correct |
793 ms |
41236 KB |
Output is correct |
15 |
Correct |
883 ms |
47152 KB |
Output is correct |
16 |
Correct |
131 ms |
14256 KB |
Output is correct |
17 |
Correct |
809 ms |
43528 KB |
Output is correct |
18 |
Correct |
682 ms |
36500 KB |
Output is correct |
19 |
Correct |
267 ms |
19212 KB |
Output is correct |
20 |
Correct |
300 ms |
21988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
182 ms |
9336 KB |
Output is correct |
2 |
Correct |
158 ms |
10996 KB |
Output is correct |
3 |
Correct |
157 ms |
10660 KB |
Output is correct |
4 |
Correct |
135 ms |
10576 KB |
Output is correct |
5 |
Correct |
163 ms |
10936 KB |
Output is correct |
6 |
Correct |
134 ms |
10492 KB |
Output is correct |
7 |
Correct |
173 ms |
10908 KB |
Output is correct |
8 |
Correct |
144 ms |
10484 KB |
Output is correct |
9 |
Correct |
147 ms |
10796 KB |
Output is correct |
10 |
Correct |
126 ms |
10276 KB |
Output is correct |
11 |
Correct |
146 ms |
10748 KB |
Output is correct |
12 |
Correct |
127 ms |
10284 KB |
Output is correct |
13 |
Correct |
133 ms |
10448 KB |
Output is correct |
14 |
Correct |
143 ms |
10796 KB |
Output is correct |
15 |
Correct |
128 ms |
10488 KB |
Output is correct |
16 |
Correct |
55 ms |
7488 KB |
Output is correct |
17 |
Correct |
135 ms |
10412 KB |
Output is correct |
18 |
Correct |
132 ms |
9532 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
698 ms |
20124 KB |
Output is correct |
2 |
Correct |
704 ms |
23116 KB |
Output is correct |
3 |
Correct |
688 ms |
22376 KB |
Output is correct |
4 |
Correct |
616 ms |
22852 KB |
Output is correct |
5 |
Correct |
675 ms |
23528 KB |
Output is correct |
6 |
Correct |
634 ms |
22768 KB |
Output is correct |
7 |
Correct |
679 ms |
23668 KB |
Output is correct |
8 |
Correct |
637 ms |
22936 KB |
Output is correct |
9 |
Correct |
679 ms |
22728 KB |
Output is correct |
10 |
Correct |
638 ms |
23032 KB |
Output is correct |
11 |
Correct |
647 ms |
22984 KB |
Output is correct |
12 |
Correct |
666 ms |
22804 KB |
Output is correct |
13 |
Correct |
636 ms |
23084 KB |
Output is correct |
14 |
Correct |
644 ms |
23408 KB |
Output is correct |
15 |
Correct |
652 ms |
23712 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
653 ms |
23052 KB |
Output is correct |
18 |
Correct |
622 ms |
22864 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1027 ms |
35564 KB |
Output is correct |
22 |
Correct |
981 ms |
49396 KB |
Output is correct |
23 |
Correct |
837 ms |
40840 KB |
Output is correct |
24 |
Correct |
776 ms |
41488 KB |
Output is correct |
25 |
Correct |
845 ms |
42404 KB |
Output is correct |
26 |
Correct |
784 ms |
40744 KB |
Output is correct |
27 |
Correct |
934 ms |
49000 KB |
Output is correct |
28 |
Correct |
911 ms |
47016 KB |
Output is correct |
29 |
Correct |
847 ms |
42128 KB |
Output is correct |
30 |
Correct |
869 ms |
45856 KB |
Output is correct |
31 |
Correct |
785 ms |
40292 KB |
Output is correct |
32 |
Correct |
770 ms |
39976 KB |
Output is correct |
33 |
Correct |
921 ms |
45264 KB |
Output is correct |
34 |
Correct |
793 ms |
41236 KB |
Output is correct |
35 |
Correct |
883 ms |
47152 KB |
Output is correct |
36 |
Correct |
131 ms |
14256 KB |
Output is correct |
37 |
Correct |
809 ms |
43528 KB |
Output is correct |
38 |
Correct |
682 ms |
36500 KB |
Output is correct |
39 |
Correct |
267 ms |
19212 KB |
Output is correct |
40 |
Correct |
300 ms |
21988 KB |
Output is correct |
41 |
Correct |
182 ms |
9336 KB |
Output is correct |
42 |
Correct |
158 ms |
10996 KB |
Output is correct |
43 |
Correct |
157 ms |
10660 KB |
Output is correct |
44 |
Correct |
135 ms |
10576 KB |
Output is correct |
45 |
Correct |
163 ms |
10936 KB |
Output is correct |
46 |
Correct |
134 ms |
10492 KB |
Output is correct |
47 |
Correct |
173 ms |
10908 KB |
Output is correct |
48 |
Correct |
144 ms |
10484 KB |
Output is correct |
49 |
Correct |
147 ms |
10796 KB |
Output is correct |
50 |
Correct |
126 ms |
10276 KB |
Output is correct |
51 |
Correct |
146 ms |
10748 KB |
Output is correct |
52 |
Correct |
127 ms |
10284 KB |
Output is correct |
53 |
Correct |
133 ms |
10448 KB |
Output is correct |
54 |
Correct |
143 ms |
10796 KB |
Output is correct |
55 |
Correct |
128 ms |
10488 KB |
Output is correct |
56 |
Correct |
55 ms |
7488 KB |
Output is correct |
57 |
Correct |
135 ms |
10412 KB |
Output is correct |
58 |
Correct |
132 ms |
9532 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
1109 ms |
49956 KB |
Output is correct |
62 |
Correct |
1152 ms |
49164 KB |
Output is correct |
63 |
Correct |
1164 ms |
47472 KB |
Output is correct |
64 |
Correct |
985 ms |
47236 KB |
Output is correct |
65 |
Correct |
1006 ms |
48956 KB |
Output is correct |
66 |
Correct |
948 ms |
46984 KB |
Output is correct |
67 |
Correct |
970 ms |
48572 KB |
Output is correct |
68 |
Correct |
961 ms |
46672 KB |
Output is correct |
69 |
Correct |
948 ms |
47280 KB |
Output is correct |
70 |
Correct |
949 ms |
46272 KB |
Output is correct |
71 |
Correct |
868 ms |
45832 KB |
Output is correct |
72 |
Correct |
926 ms |
46100 KB |
Output is correct |
73 |
Correct |
894 ms |
46092 KB |
Output is correct |
74 |
Correct |
943 ms |
47444 KB |
Output is correct |
75 |
Correct |
923 ms |
47628 KB |
Output is correct |
76 |
Correct |
139 ms |
14284 KB |
Output is correct |
77 |
Correct |
830 ms |
43988 KB |
Output is correct |
78 |
Correct |
782 ms |
42204 KB |
Output is correct |
79 |
Correct |
0 ms |
340 KB |
Output is correct |
80 |
Correct |
0 ms |
340 KB |
Output is correct |