#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef long long ll;
#define int ll
const int N = 2e5 + 5;
struct ST{
int tree[N << 2];
void add(int i, int v, int lx = 0, int rx = N, int x = 1){
if(lx == rx){
tree[x] += v; return;
}
int m = (lx + rx) >> 1;
if(i <= m) add(i, v, lx, m, x << 1);
else add(i, v, m + 1, rx, x << 1 | 1);
tree[x] = tree[x << 1] + tree[x << 1 | 1];
}
int sum(int l, int r, int lx = 0, int rx = N, int x = 1){
if(lx > r || rx < l) return 0;
if(lx >= l && rx <= r) return tree[x];
int m = (lx + rx) >> 1;
return sum(l, r, lx, m, x << 1) + sum(l, r, m + 1, rx, x << 1 | 1);
}
int get(int v, int lx = 0, int rx = N, int x = 1){
if(lx == rx) return lx;
int m = (lx + rx) >> 1;
if(tree[x << 1] > v) return get(v, lx, m, x << 1);
else return get(v - tree[x << 1], m + 1, rx, x << 1 | 1);
}
}tree;
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, q; cin >> n >> q;
vector<int> a(n);
for(int i=0;i<n;i++){
cin >> a[i]; a[i]--;
}
vector<int> t(q), id(q);
vector<int> p(q), res(q, -1);
for(int i=0;i<q;i++){
cin >> t[i] >> id[i]; --id[i];
p[i] = i;
}
sort(p.begin(), p.end(), [&](int i, int j){
if(t[i] != t[j]) return t[i] < t[j];
else return id[i] < id[j];
});
int j = 0;
while(j < q && t[p[j]] == 0){
res[p[j]] = a[id[p[j]]];
j++;
}
vector<int> cnt(n), pos(n), par(n, n), ss;
for(int i=n-1;~i;i--){
pos[a[i]] = i;
while(!ss.empty() && a[ss.back()] < a[i]) ss.pop_back();
if(!ss.empty()) par[i] = ss.back();
ss.push_back(i);
}
{
int i = 0, m = n / 2;
while(i < m){
cnt[a[i]] = min(m, par[i]) - i;
tree.add(a[i], cnt[a[i]]);
i = par[i];
}
i = m;
while(i < n){
cnt[a[i]] = par[i] - i;
tree.add(a[i], cnt[a[i]]);
i = par[i];
}
}
//~ for(int i=0;i<n;i++){
//~ cout<<cnt[i]<<" ";
//~ } cout<<"\n";
for(int i=1;i<=n;i++){
while(j < q && t[p[j]] == i){
int k = tree.get(id[p[j]]), sum = (k ? tree.sum(0, k - 1) : 0);
res[p[j]] = a[pos[k] + (id[p[j]] - sum)];
j++;
}
int P = tree.get(n / 2 - 1);
int rem = tree.sum(0, P) - n / 2;
if(!rem){
break;
}
//~ cout<<P<<" "<<rem<<endl;
cnt[P] -= rem;
tree.add(P, -rem);
P = pos[P] + cnt[P];
int k = P; P += rem;
while(k < P){
//~ assert(!cnt[a[k]]);
cnt[a[k]] = min(P, par[k]) - k;
//~ assert(!tree.sum(a[k], a[k]));
tree.add(a[k], cnt[a[k]] - tree.sum(a[k], a[k]));
k = par[k];
}
//~ for(int i=0;i<n;i++){
//~ cout<<cnt[i]<<" ";
//~ } cout<<"\n";
}
while(j < q){
int k = tree.get(id[p[j]]), sum = (k ? tree.sum(0, k - 1) : 0);
res[p[j]] = a[pos[k] + (id[p[j]] - sum)];
j++;
}
for(int i=0;i<q;i++){
cout<<++res[i]<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
679 ms |
35876 KB |
Output is correct |
2 |
Correct |
701 ms |
35848 KB |
Output is correct |
3 |
Correct |
598 ms |
34460 KB |
Output is correct |
4 |
Correct |
569 ms |
34252 KB |
Output is correct |
5 |
Correct |
614 ms |
35724 KB |
Output is correct |
6 |
Correct |
557 ms |
34240 KB |
Output is correct |
7 |
Correct |
613 ms |
35788 KB |
Output is correct |
8 |
Correct |
582 ms |
34408 KB |
Output is correct |
9 |
Correct |
571 ms |
34044 KB |
Output is correct |
10 |
Correct |
613 ms |
34716 KB |
Output is correct |
11 |
Correct |
543 ms |
34644 KB |
Output is correct |
12 |
Correct |
482 ms |
34164 KB |
Output is correct |
13 |
Correct |
599 ms |
34884 KB |
Output is correct |
14 |
Correct |
603 ms |
35076 KB |
Output is correct |
15 |
Correct |
605 ms |
35676 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
335 ms |
34700 KB |
Output is correct |
18 |
Correct |
438 ms |
34400 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 |
493 ms |
49120 KB |
Output is correct |
2 |
Correct |
486 ms |
63140 KB |
Output is correct |
3 |
Correct |
441 ms |
51364 KB |
Output is correct |
4 |
Correct |
400 ms |
50684 KB |
Output is correct |
5 |
Correct |
391 ms |
51768 KB |
Output is correct |
6 |
Correct |
349 ms |
49836 KB |
Output is correct |
7 |
Correct |
407 ms |
61560 KB |
Output is correct |
8 |
Correct |
434 ms |
59056 KB |
Output is correct |
9 |
Correct |
374 ms |
51552 KB |
Output is correct |
10 |
Correct |
379 ms |
57804 KB |
Output is correct |
11 |
Correct |
325 ms |
49088 KB |
Output is correct |
12 |
Correct |
361 ms |
49040 KB |
Output is correct |
13 |
Correct |
412 ms |
57280 KB |
Output is correct |
14 |
Correct |
341 ms |
50564 KB |
Output is correct |
15 |
Correct |
418 ms |
59456 KB |
Output is correct |
16 |
Correct |
35 ms |
11080 KB |
Output is correct |
17 |
Correct |
285 ms |
57372 KB |
Output is correct |
18 |
Correct |
345 ms |
43184 KB |
Output is correct |
19 |
Correct |
79 ms |
19524 KB |
Output is correct |
20 |
Correct |
106 ms |
21656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
135 ms |
9528 KB |
Output is correct |
2 |
Correct |
109 ms |
11620 KB |
Output is correct |
3 |
Correct |
110 ms |
11148 KB |
Output is correct |
4 |
Correct |
64 ms |
10564 KB |
Output is correct |
5 |
Correct |
111 ms |
10888 KB |
Output is correct |
6 |
Correct |
75 ms |
10472 KB |
Output is correct |
7 |
Correct |
83 ms |
10880 KB |
Output is correct |
8 |
Correct |
75 ms |
10444 KB |
Output is correct |
9 |
Correct |
88 ms |
10612 KB |
Output is correct |
10 |
Correct |
65 ms |
10368 KB |
Output is correct |
11 |
Correct |
65 ms |
10568 KB |
Output is correct |
12 |
Correct |
71 ms |
10196 KB |
Output is correct |
13 |
Correct |
59 ms |
10436 KB |
Output is correct |
14 |
Correct |
64 ms |
10592 KB |
Output is correct |
15 |
Correct |
65 ms |
10024 KB |
Output is correct |
16 |
Correct |
16 ms |
5968 KB |
Output is correct |
17 |
Correct |
49 ms |
11080 KB |
Output is correct |
18 |
Correct |
47 ms |
8220 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
679 ms |
35876 KB |
Output is correct |
2 |
Correct |
701 ms |
35848 KB |
Output is correct |
3 |
Correct |
598 ms |
34460 KB |
Output is correct |
4 |
Correct |
569 ms |
34252 KB |
Output is correct |
5 |
Correct |
614 ms |
35724 KB |
Output is correct |
6 |
Correct |
557 ms |
34240 KB |
Output is correct |
7 |
Correct |
613 ms |
35788 KB |
Output is correct |
8 |
Correct |
582 ms |
34408 KB |
Output is correct |
9 |
Correct |
571 ms |
34044 KB |
Output is correct |
10 |
Correct |
613 ms |
34716 KB |
Output is correct |
11 |
Correct |
543 ms |
34644 KB |
Output is correct |
12 |
Correct |
482 ms |
34164 KB |
Output is correct |
13 |
Correct |
599 ms |
34884 KB |
Output is correct |
14 |
Correct |
603 ms |
35076 KB |
Output is correct |
15 |
Correct |
605 ms |
35676 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
335 ms |
34700 KB |
Output is correct |
18 |
Correct |
438 ms |
34400 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
493 ms |
49120 KB |
Output is correct |
22 |
Correct |
486 ms |
63140 KB |
Output is correct |
23 |
Correct |
441 ms |
51364 KB |
Output is correct |
24 |
Correct |
400 ms |
50684 KB |
Output is correct |
25 |
Correct |
391 ms |
51768 KB |
Output is correct |
26 |
Correct |
349 ms |
49836 KB |
Output is correct |
27 |
Correct |
407 ms |
61560 KB |
Output is correct |
28 |
Correct |
434 ms |
59056 KB |
Output is correct |
29 |
Correct |
374 ms |
51552 KB |
Output is correct |
30 |
Correct |
379 ms |
57804 KB |
Output is correct |
31 |
Correct |
325 ms |
49088 KB |
Output is correct |
32 |
Correct |
361 ms |
49040 KB |
Output is correct |
33 |
Correct |
412 ms |
57280 KB |
Output is correct |
34 |
Correct |
341 ms |
50564 KB |
Output is correct |
35 |
Correct |
418 ms |
59456 KB |
Output is correct |
36 |
Correct |
35 ms |
11080 KB |
Output is correct |
37 |
Correct |
285 ms |
57372 KB |
Output is correct |
38 |
Correct |
345 ms |
43184 KB |
Output is correct |
39 |
Correct |
79 ms |
19524 KB |
Output is correct |
40 |
Correct |
106 ms |
21656 KB |
Output is correct |
41 |
Correct |
135 ms |
9528 KB |
Output is correct |
42 |
Correct |
109 ms |
11620 KB |
Output is correct |
43 |
Correct |
110 ms |
11148 KB |
Output is correct |
44 |
Correct |
64 ms |
10564 KB |
Output is correct |
45 |
Correct |
111 ms |
10888 KB |
Output is correct |
46 |
Correct |
75 ms |
10472 KB |
Output is correct |
47 |
Correct |
83 ms |
10880 KB |
Output is correct |
48 |
Correct |
75 ms |
10444 KB |
Output is correct |
49 |
Correct |
88 ms |
10612 KB |
Output is correct |
50 |
Correct |
65 ms |
10368 KB |
Output is correct |
51 |
Correct |
65 ms |
10568 KB |
Output is correct |
52 |
Correct |
71 ms |
10196 KB |
Output is correct |
53 |
Correct |
59 ms |
10436 KB |
Output is correct |
54 |
Correct |
64 ms |
10592 KB |
Output is correct |
55 |
Correct |
65 ms |
10024 KB |
Output is correct |
56 |
Correct |
16 ms |
5968 KB |
Output is correct |
57 |
Correct |
49 ms |
11080 KB |
Output is correct |
58 |
Correct |
47 ms |
8220 KB |
Output is correct |
59 |
Correct |
1 ms |
332 KB |
Output is correct |
60 |
Correct |
1 ms |
332 KB |
Output is correct |
61 |
Correct |
904 ms |
62264 KB |
Output is correct |
62 |
Correct |
1012 ms |
62972 KB |
Output is correct |
63 |
Correct |
960 ms |
60292 KB |
Output is correct |
64 |
Correct |
745 ms |
59076 KB |
Output is correct |
65 |
Correct |
814 ms |
61488 KB |
Output is correct |
66 |
Correct |
781 ms |
58776 KB |
Output is correct |
67 |
Correct |
724 ms |
61132 KB |
Output is correct |
68 |
Correct |
856 ms |
58776 KB |
Output is correct |
69 |
Correct |
823 ms |
58952 KB |
Output is correct |
70 |
Correct |
786 ms |
58536 KB |
Output is correct |
71 |
Correct |
731 ms |
57280 KB |
Output is correct |
72 |
Correct |
730 ms |
58072 KB |
Output is correct |
73 |
Correct |
723 ms |
58208 KB |
Output is correct |
74 |
Correct |
809 ms |
59652 KB |
Output is correct |
75 |
Correct |
837 ms |
59892 KB |
Output is correct |
76 |
Correct |
29 ms |
11080 KB |
Output is correct |
77 |
Correct |
565 ms |
57572 KB |
Output is correct |
78 |
Correct |
647 ms |
51804 KB |
Output is correct |
79 |
Correct |
1 ms |
340 KB |
Output is correct |
80 |
Correct |
1 ms |
332 KB |
Output is correct |