#include<bits/stdc++.h>
using namespace std;
mt19937 rng(time(nullptr));
using T = int;
struct node {
T val, ma;
int size;
size_t prior;
node *left, *right;
node(T v) : val(v), ma(v), size(1),
prior(rng()), left(0), right(0) {}
node* fix() {
ma = val, size = 1;
if (left) {
ma = max(ma, left->ma);
size += left->size;
}
if (right) {
ma = max(ma, right->ma);
size += right->size;
}
return this;
}
};
node* merge(node* a, node* b) {
if (!a) return b; a->fix();
if (!b) return a; b->fix();
if (a->prior > b->prior) {
a->right = merge(a->right, b);
return a->fix();
} else {
b->left = merge(a, b->left);
return b->fix();
}
}
pair<node*, node*> split(node* p, int k) {
if (!p) return {nullptr, nullptr};
p->fix();
int sl = p->left ? p->left->size : 0;
if (k <= sl) {
auto [a, b] = split(p->left, k);
p->left = b;
return {a, p->fix()};
} else {
auto [a, b] = split(p->right, k-sl-1);
p->right = a;
return {p->fix(), b};
}
}
int find_kth(node* p,int pos){
p->fix();
int sl = p->left ? p->left->size : 0;
if (p->left && pos < sl) {
return find_kth(p->left,pos);
} else if (p->right && pos > sl) {
return find_kth(p->right,pos - sl - 1);
} else {
return p->val;
}
}
int find_first(node *p,int val){
int sl = p->left ? p->left->size : 0;
if (p->left && p->left->ma > val) {
return find_first(p->left,val);
} else if (p->val > val){
return sl;
} else if (p->right && p->right->ma > val) {
return sl + 1 + find_first(p->right,val);
} else {
return p->size;
}
}
node* build(vector<T>& A, int l, int r) {
if (l + 1 == r) return new node(A[l]);
int m = (l + r) / 2;
node* a = build(A, l, m);
node* b = build(A, m, r);
return merge(a, b);
}
const int MAXN = 200005, MAXQ = 1000006;
int n,q,ans[MAXQ],tmp[MAXN],cont;
vector<T> v;
vector<pair<int,int> > opz[MAXN];
node* root;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>q;
v.resize(n);
for(int i=0;i<n;i++){
cin>>v[i];
}
for(int i=0;i<q;i++){
int t,x;
cin>>t>>x;
opz[min(t,n)].emplace_back(x-1,i);
}
root = build(v,0,n);
for(int i=0;i<=n;i++){
for(auto [a,b] : opz[i]){
ans[b]=find_kth(root,a);
}
auto [a,b] = split(root,n/2);
while(b!=nullptr){
int val = find_kth(b,0);
if(val > a->ma){
a = merge(a,b);
break;
}
int pos1 = find_first(a,val);
int pos2 = find_first(b,val);
auto [c,d] = split(b,pos2);
auto [e,f] = split(a,pos1);
a = merge(merge(e,c),f);
b = d;
cont++;
}
root = a;
}
for(int i=0;i<q;i++){
cout<<ans[i]<<"\n";
}
return 0;
}
Compilation message
Main.cpp: In function 'node* merge(node*, node*)':
Main.cpp:30:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
30 | if (!a) return b; a->fix();
| ^~
Main.cpp:30:23: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
30 | if (!a) return b; a->fix();
| ^
Main.cpp:31:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
31 | if (!b) return a; b->fix();
| ^~
Main.cpp:31:23: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
31 | if (!b) return a; b->fix();
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
304 ms |
30744 KB |
Output is correct |
2 |
Correct |
362 ms |
29324 KB |
Output is correct |
3 |
Correct |
342 ms |
28716 KB |
Output is correct |
4 |
Correct |
306 ms |
27476 KB |
Output is correct |
5 |
Correct |
336 ms |
31056 KB |
Output is correct |
6 |
Correct |
334 ms |
30356 KB |
Output is correct |
7 |
Correct |
304 ms |
31604 KB |
Output is correct |
8 |
Correct |
303 ms |
29128 KB |
Output is correct |
9 |
Correct |
286 ms |
27992 KB |
Output is correct |
10 |
Correct |
298 ms |
27844 KB |
Output is correct |
11 |
Correct |
291 ms |
28232 KB |
Output is correct |
12 |
Correct |
295 ms |
25728 KB |
Output is correct |
13 |
Correct |
308 ms |
27076 KB |
Output is correct |
14 |
Correct |
382 ms |
29644 KB |
Output is correct |
15 |
Correct |
295 ms |
27552 KB |
Output is correct |
16 |
Correct |
3 ms |
5032 KB |
Output is correct |
17 |
Correct |
280 ms |
25852 KB |
Output is correct |
18 |
Correct |
330 ms |
25824 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
738 ms |
47712 KB |
Output is correct |
2 |
Correct |
607 ms |
46720 KB |
Output is correct |
3 |
Correct |
547 ms |
39704 KB |
Output is correct |
4 |
Correct |
474 ms |
39844 KB |
Output is correct |
5 |
Correct |
495 ms |
40676 KB |
Output is correct |
6 |
Correct |
450 ms |
39244 KB |
Output is correct |
7 |
Correct |
592 ms |
46596 KB |
Output is correct |
8 |
Correct |
522 ms |
44852 KB |
Output is correct |
9 |
Correct |
552 ms |
40472 KB |
Output is correct |
10 |
Correct |
474 ms |
43760 KB |
Output is correct |
11 |
Correct |
374 ms |
38908 KB |
Output is correct |
12 |
Correct |
416 ms |
38440 KB |
Output is correct |
13 |
Correct |
460 ms |
43124 KB |
Output is correct |
14 |
Correct |
397 ms |
39564 KB |
Output is correct |
15 |
Correct |
530 ms |
44776 KB |
Output is correct |
16 |
Correct |
154 ms |
16276 KB |
Output is correct |
17 |
Correct |
763 ms |
41320 KB |
Output is correct |
18 |
Correct |
407 ms |
36548 KB |
Output is correct |
19 |
Correct |
149 ms |
21344 KB |
Output is correct |
20 |
Correct |
210 ms |
22708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
234 ms |
14928 KB |
Output is correct |
2 |
Correct |
157 ms |
14284 KB |
Output is correct |
3 |
Correct |
167 ms |
14204 KB |
Output is correct |
4 |
Correct |
143 ms |
13712 KB |
Output is correct |
5 |
Correct |
212 ms |
14432 KB |
Output is correct |
6 |
Correct |
129 ms |
13676 KB |
Output is correct |
7 |
Correct |
181 ms |
14304 KB |
Output is correct |
8 |
Correct |
144 ms |
13728 KB |
Output is correct |
9 |
Correct |
172 ms |
14156 KB |
Output is correct |
10 |
Correct |
108 ms |
13432 KB |
Output is correct |
11 |
Correct |
122 ms |
13704 KB |
Output is correct |
12 |
Correct |
115 ms |
13528 KB |
Output is correct |
13 |
Correct |
142 ms |
13360 KB |
Output is correct |
14 |
Correct |
122 ms |
13756 KB |
Output is correct |
15 |
Correct |
122 ms |
13404 KB |
Output is correct |
16 |
Correct |
73 ms |
10616 KB |
Output is correct |
17 |
Correct |
165 ms |
12972 KB |
Output is correct |
18 |
Correct |
112 ms |
12932 KB |
Output is correct |
19 |
Correct |
3 ms |
5032 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
304 ms |
30744 KB |
Output is correct |
2 |
Correct |
362 ms |
29324 KB |
Output is correct |
3 |
Correct |
342 ms |
28716 KB |
Output is correct |
4 |
Correct |
306 ms |
27476 KB |
Output is correct |
5 |
Correct |
336 ms |
31056 KB |
Output is correct |
6 |
Correct |
334 ms |
30356 KB |
Output is correct |
7 |
Correct |
304 ms |
31604 KB |
Output is correct |
8 |
Correct |
303 ms |
29128 KB |
Output is correct |
9 |
Correct |
286 ms |
27992 KB |
Output is correct |
10 |
Correct |
298 ms |
27844 KB |
Output is correct |
11 |
Correct |
291 ms |
28232 KB |
Output is correct |
12 |
Correct |
295 ms |
25728 KB |
Output is correct |
13 |
Correct |
308 ms |
27076 KB |
Output is correct |
14 |
Correct |
382 ms |
29644 KB |
Output is correct |
15 |
Correct |
295 ms |
27552 KB |
Output is correct |
16 |
Correct |
3 ms |
5032 KB |
Output is correct |
17 |
Correct |
280 ms |
25852 KB |
Output is correct |
18 |
Correct |
330 ms |
25824 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
738 ms |
47712 KB |
Output is correct |
22 |
Correct |
607 ms |
46720 KB |
Output is correct |
23 |
Correct |
547 ms |
39704 KB |
Output is correct |
24 |
Correct |
474 ms |
39844 KB |
Output is correct |
25 |
Correct |
495 ms |
40676 KB |
Output is correct |
26 |
Correct |
450 ms |
39244 KB |
Output is correct |
27 |
Correct |
592 ms |
46596 KB |
Output is correct |
28 |
Correct |
522 ms |
44852 KB |
Output is correct |
29 |
Correct |
552 ms |
40472 KB |
Output is correct |
30 |
Correct |
474 ms |
43760 KB |
Output is correct |
31 |
Correct |
374 ms |
38908 KB |
Output is correct |
32 |
Correct |
416 ms |
38440 KB |
Output is correct |
33 |
Correct |
460 ms |
43124 KB |
Output is correct |
34 |
Correct |
397 ms |
39564 KB |
Output is correct |
35 |
Correct |
530 ms |
44776 KB |
Output is correct |
36 |
Correct |
154 ms |
16276 KB |
Output is correct |
37 |
Correct |
763 ms |
41320 KB |
Output is correct |
38 |
Correct |
407 ms |
36548 KB |
Output is correct |
39 |
Correct |
149 ms |
21344 KB |
Output is correct |
40 |
Correct |
210 ms |
22708 KB |
Output is correct |
41 |
Correct |
234 ms |
14928 KB |
Output is correct |
42 |
Correct |
157 ms |
14284 KB |
Output is correct |
43 |
Correct |
167 ms |
14204 KB |
Output is correct |
44 |
Correct |
143 ms |
13712 KB |
Output is correct |
45 |
Correct |
212 ms |
14432 KB |
Output is correct |
46 |
Correct |
129 ms |
13676 KB |
Output is correct |
47 |
Correct |
181 ms |
14304 KB |
Output is correct |
48 |
Correct |
144 ms |
13728 KB |
Output is correct |
49 |
Correct |
172 ms |
14156 KB |
Output is correct |
50 |
Correct |
108 ms |
13432 KB |
Output is correct |
51 |
Correct |
122 ms |
13704 KB |
Output is correct |
52 |
Correct |
115 ms |
13528 KB |
Output is correct |
53 |
Correct |
142 ms |
13360 KB |
Output is correct |
54 |
Correct |
122 ms |
13756 KB |
Output is correct |
55 |
Correct |
122 ms |
13404 KB |
Output is correct |
56 |
Correct |
73 ms |
10616 KB |
Output is correct |
57 |
Correct |
165 ms |
12972 KB |
Output is correct |
58 |
Correct |
112 ms |
12932 KB |
Output is correct |
59 |
Correct |
3 ms |
5032 KB |
Output is correct |
60 |
Correct |
3 ms |
4948 KB |
Output is correct |
61 |
Correct |
1616 ms |
53516 KB |
Output is correct |
62 |
Correct |
1086 ms |
51068 KB |
Output is correct |
63 |
Correct |
1111 ms |
49840 KB |
Output is correct |
64 |
Correct |
1085 ms |
49276 KB |
Output is correct |
65 |
Correct |
1056 ms |
50988 KB |
Output is correct |
66 |
Correct |
1148 ms |
49156 KB |
Output is correct |
67 |
Correct |
1006 ms |
48844 KB |
Output is correct |
68 |
Correct |
929 ms |
46988 KB |
Output is correct |
69 |
Correct |
974 ms |
49356 KB |
Output is correct |
70 |
Correct |
829 ms |
45828 KB |
Output is correct |
71 |
Correct |
802 ms |
46684 KB |
Output is correct |
72 |
Correct |
967 ms |
45836 KB |
Output is correct |
73 |
Correct |
962 ms |
46528 KB |
Output is correct |
74 |
Correct |
1132 ms |
48868 KB |
Output is correct |
75 |
Correct |
903 ms |
46932 KB |
Output is correct |
76 |
Correct |
112 ms |
16276 KB |
Output is correct |
77 |
Correct |
683 ms |
41664 KB |
Output is correct |
78 |
Correct |
682 ms |
41444 KB |
Output is correct |
79 |
Correct |
3 ms |
4948 KB |
Output is correct |
80 |
Correct |
3 ms |
4948 KB |
Output is correct |