// heiy ... ye rooze jadid ...
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) x.begin(), x.end()
#define pb push_back
#define fi first
#define se second
const int maxn5 = 1e6 + 10;
const int maxnt = 1e6 + 10;
int sum[maxnt], a[maxn5], rev[maxn5], out[maxn5], per[maxn5];
int rig[maxn5], sz[maxn5], t[maxn5], ind[maxn5], last[maxn5];
bool mark[maxn5];
vector <int> av;
inline bool cmp(int i, int j){return t[i] < t[j];}
void upd(int l, int r, int id, int val, int v){
if(r - l == 1){
sum[v] = val;
return;
}
int mid = (l + r) >> 1;
if(id < mid)
upd(l, mid, id, val, v * 2);
else
upd(mid, r, id, val, v * 2 + 1);
sum[v] = sum[v * 2] + sum[v * 2 + 1];
}
pair<int, int> find_first(int l, int r, int val, int v){
if(r - l == 1)
return {l, sum[v]};
int mid = (l + r) >> 1;
if(sum[v * 2] >= val)
return find_first(l, mid, val, v * 2);
auto ans = find_first(mid, r, val - sum[v * 2], v * 2 + 1);
return {ans.fi, ans.se + sum[v * 2]};
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n, q; cin >> n >> q;
for(int i = 0; i < n; i++){
cin >> a[i];
a[i]--;
rev[a[i]] = i;
}
for(int i = 0; i < q; i++){
cin >> t[i] >> ind[i];
per[i] = i;
}
fill(rig, rig + n + 10, n);
for(int i = n - 1; i >= 0; i--){
while(av.size() && a[av.back()] < a[i])
av.pop_back();
if(av.size())
rig[i] = av.back();
av.pb(i);
}
for(int i = 0; i != n; i = rig[i]){
upd(0, n, a[i], rig[i] - i, 1);
sz[i] = rig[i] - i;
mark[i] = true;
//cout << "ok " << i << ' ' << rig[i] << ' ' << sz[i] << endl;
last[i] = rig[i];
}
sort(per, per + q, cmp); // bar hasbe t[i]
int done = 0;
bool re = false;
for(int i = 0; i < q; i++){
while(done < t[per[i]] && !re){
done++;
auto tmp = find_first(0, n, n / 2, 1);
int p = tmp.fi, r = tmp.se - n / 2;
if(r == 0){
re = true;
break;
}
int v = rev[p] + sz[rev[p]] - r;
int vv = v;
//cout << "now " << done << ' ' << p << ' ' << r << ' ' << v << ' ' << rev[p] << ' ' << sz[rev[p]] << ' ' << tmp.se << endl;
sz[rev[p]] -= r;
upd(0, n, p, sz[rev[p]], 1);
for(; !mark[v] && v != n ; v = rig[v]){
mark[v] = true;
sz[v] = min(last[rev[p]], rig[v]) - v;
last[v] = min(last[rev[p]], rig[v]);
//cout << "it's " << v << ' ' << rig[v] << ' ' << sz[v] << endl;
upd(0, n, a[v], sz[v], 1);
}
last[rev[p]] = vv;
}
auto tmp = find_first(0, n, ind[per[i]], 1);
int p = tmp.fi, r = tmp.se - ind[per[i]];
r = sz[rev[p]] - r - 1;
out[per[i]] = a[rev[p] + r] + 1;
}
for(int i = 0; i < q; i++)
cout << out[i] << '\n';
}
/*
8 1
6 8 3 5 4 7 2 1
3 5
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
364 ms |
19896 KB |
Output is correct |
2 |
Correct |
381 ms |
19844 KB |
Output is correct |
3 |
Correct |
377 ms |
19100 KB |
Output is correct |
4 |
Correct |
316 ms |
19012 KB |
Output is correct |
5 |
Correct |
364 ms |
19920 KB |
Output is correct |
6 |
Correct |
322 ms |
19032 KB |
Output is correct |
7 |
Correct |
364 ms |
19916 KB |
Output is correct |
8 |
Correct |
335 ms |
19080 KB |
Output is correct |
9 |
Correct |
335 ms |
19104 KB |
Output is correct |
10 |
Correct |
327 ms |
19248 KB |
Output is correct |
11 |
Correct |
327 ms |
19276 KB |
Output is correct |
12 |
Correct |
260 ms |
18984 KB |
Output is correct |
13 |
Correct |
321 ms |
19396 KB |
Output is correct |
14 |
Correct |
343 ms |
19508 KB |
Output is correct |
15 |
Correct |
345 ms |
19792 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
268 ms |
19272 KB |
Output is correct |
18 |
Correct |
242 ms |
19148 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
363 ms |
27772 KB |
Output is correct |
2 |
Correct |
375 ms |
42100 KB |
Output is correct |
3 |
Correct |
291 ms |
33828 KB |
Output is correct |
4 |
Correct |
298 ms |
30948 KB |
Output is correct |
5 |
Correct |
307 ms |
31656 KB |
Output is correct |
6 |
Correct |
291 ms |
31920 KB |
Output is correct |
7 |
Correct |
366 ms |
36256 KB |
Output is correct |
8 |
Correct |
356 ms |
37352 KB |
Output is correct |
9 |
Correct |
295 ms |
31600 KB |
Output is correct |
10 |
Correct |
330 ms |
36160 KB |
Output is correct |
11 |
Correct |
268 ms |
29772 KB |
Output is correct |
12 |
Correct |
263 ms |
29256 KB |
Output is correct |
13 |
Correct |
323 ms |
35700 KB |
Output is correct |
14 |
Correct |
273 ms |
32620 KB |
Output is correct |
15 |
Correct |
331 ms |
38572 KB |
Output is correct |
16 |
Correct |
21 ms |
5728 KB |
Output is correct |
17 |
Correct |
308 ms |
35676 KB |
Output is correct |
18 |
Correct |
246 ms |
25204 KB |
Output is correct |
19 |
Correct |
77 ms |
9508 KB |
Output is correct |
20 |
Correct |
95 ms |
13056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
5592 KB |
Output is correct |
2 |
Correct |
68 ms |
7348 KB |
Output is correct |
3 |
Correct |
76 ms |
7340 KB |
Output is correct |
4 |
Correct |
52 ms |
5952 KB |
Output is correct |
5 |
Correct |
70 ms |
6124 KB |
Output is correct |
6 |
Correct |
52 ms |
6220 KB |
Output is correct |
7 |
Correct |
59 ms |
5964 KB |
Output is correct |
8 |
Correct |
59 ms |
6116 KB |
Output is correct |
9 |
Correct |
58 ms |
6472 KB |
Output is correct |
10 |
Correct |
46 ms |
6336 KB |
Output is correct |
11 |
Correct |
48 ms |
6008 KB |
Output is correct |
12 |
Correct |
46 ms |
6316 KB |
Output is correct |
13 |
Correct |
50 ms |
6388 KB |
Output is correct |
14 |
Correct |
48 ms |
6456 KB |
Output is correct |
15 |
Correct |
44 ms |
5824 KB |
Output is correct |
16 |
Correct |
12 ms |
3156 KB |
Output is correct |
17 |
Correct |
55 ms |
6992 KB |
Output is correct |
18 |
Correct |
37 ms |
4680 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
364 ms |
19896 KB |
Output is correct |
2 |
Correct |
381 ms |
19844 KB |
Output is correct |
3 |
Correct |
377 ms |
19100 KB |
Output is correct |
4 |
Correct |
316 ms |
19012 KB |
Output is correct |
5 |
Correct |
364 ms |
19920 KB |
Output is correct |
6 |
Correct |
322 ms |
19032 KB |
Output is correct |
7 |
Correct |
364 ms |
19916 KB |
Output is correct |
8 |
Correct |
335 ms |
19080 KB |
Output is correct |
9 |
Correct |
335 ms |
19104 KB |
Output is correct |
10 |
Correct |
327 ms |
19248 KB |
Output is correct |
11 |
Correct |
327 ms |
19276 KB |
Output is correct |
12 |
Correct |
260 ms |
18984 KB |
Output is correct |
13 |
Correct |
321 ms |
19396 KB |
Output is correct |
14 |
Correct |
343 ms |
19508 KB |
Output is correct |
15 |
Correct |
345 ms |
19792 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
268 ms |
19272 KB |
Output is correct |
18 |
Correct |
242 ms |
19148 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
363 ms |
27772 KB |
Output is correct |
22 |
Correct |
375 ms |
42100 KB |
Output is correct |
23 |
Correct |
291 ms |
33828 KB |
Output is correct |
24 |
Correct |
298 ms |
30948 KB |
Output is correct |
25 |
Correct |
307 ms |
31656 KB |
Output is correct |
26 |
Correct |
291 ms |
31920 KB |
Output is correct |
27 |
Correct |
366 ms |
36256 KB |
Output is correct |
28 |
Correct |
356 ms |
37352 KB |
Output is correct |
29 |
Correct |
295 ms |
31600 KB |
Output is correct |
30 |
Correct |
330 ms |
36160 KB |
Output is correct |
31 |
Correct |
268 ms |
29772 KB |
Output is correct |
32 |
Correct |
263 ms |
29256 KB |
Output is correct |
33 |
Correct |
323 ms |
35700 KB |
Output is correct |
34 |
Correct |
273 ms |
32620 KB |
Output is correct |
35 |
Correct |
331 ms |
38572 KB |
Output is correct |
36 |
Correct |
21 ms |
5728 KB |
Output is correct |
37 |
Correct |
308 ms |
35676 KB |
Output is correct |
38 |
Correct |
246 ms |
25204 KB |
Output is correct |
39 |
Correct |
77 ms |
9508 KB |
Output is correct |
40 |
Correct |
95 ms |
13056 KB |
Output is correct |
41 |
Correct |
75 ms |
5592 KB |
Output is correct |
42 |
Correct |
68 ms |
7348 KB |
Output is correct |
43 |
Correct |
76 ms |
7340 KB |
Output is correct |
44 |
Correct |
52 ms |
5952 KB |
Output is correct |
45 |
Correct |
70 ms |
6124 KB |
Output is correct |
46 |
Correct |
52 ms |
6220 KB |
Output is correct |
47 |
Correct |
59 ms |
5964 KB |
Output is correct |
48 |
Correct |
59 ms |
6116 KB |
Output is correct |
49 |
Correct |
58 ms |
6472 KB |
Output is correct |
50 |
Correct |
46 ms |
6336 KB |
Output is correct |
51 |
Correct |
48 ms |
6008 KB |
Output is correct |
52 |
Correct |
46 ms |
6316 KB |
Output is correct |
53 |
Correct |
50 ms |
6388 KB |
Output is correct |
54 |
Correct |
48 ms |
6456 KB |
Output is correct |
55 |
Correct |
44 ms |
5824 KB |
Output is correct |
56 |
Correct |
12 ms |
3156 KB |
Output is correct |
57 |
Correct |
55 ms |
6992 KB |
Output is correct |
58 |
Correct |
37 ms |
4680 KB |
Output is correct |
59 |
Correct |
1 ms |
328 KB |
Output is correct |
60 |
Correct |
0 ms |
340 KB |
Output is correct |
61 |
Correct |
675 ms |
39564 KB |
Output is correct |
62 |
Correct |
653 ms |
40720 KB |
Output is correct |
63 |
Correct |
623 ms |
37672 KB |
Output is correct |
64 |
Correct |
539 ms |
36236 KB |
Output is correct |
65 |
Correct |
603 ms |
38496 KB |
Output is correct |
66 |
Correct |
583 ms |
35988 KB |
Output is correct |
67 |
Correct |
649 ms |
37080 KB |
Output is correct |
68 |
Correct |
626 ms |
36928 KB |
Output is correct |
69 |
Correct |
637 ms |
36108 KB |
Output is correct |
70 |
Correct |
525 ms |
36648 KB |
Output is correct |
71 |
Correct |
520 ms |
35852 KB |
Output is correct |
72 |
Correct |
554 ms |
34688 KB |
Output is correct |
73 |
Correct |
523 ms |
35292 KB |
Output is correct |
74 |
Correct |
523 ms |
36740 KB |
Output is correct |
75 |
Correct |
512 ms |
35812 KB |
Output is correct |
76 |
Correct |
21 ms |
5264 KB |
Output is correct |
77 |
Correct |
421 ms |
34936 KB |
Output is correct |
78 |
Correct |
328 ms |
31424 KB |
Output is correct |
79 |
Correct |
0 ms |
324 KB |
Output is correct |
80 |
Correct |
1 ms |
408 KB |
Output is correct |