#include<bits/stdc++.h>
using namespace std;
mt19937 rng(0);//rng(time(nullptr));
using T = int;
struct node {
T val, ma;
int size;
size_t prior;
node *left, *right;
node *par=nullptr; bool isleft=0; //se serve get_pos
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) {
auto tmp = merge(a->right, b);
a->right = tmp;
if(tmp)tmp->par = a, tmp->isleft = 0; //se serve get_pos
return a->fix();
} else {
auto tmp = merge(a, b->left);
b->left = tmp;
if(tmp)tmp->par = b, tmp->isleft = 1; //se serve get_pos
return b->fix();
}
}
//taglio subito dopo k elementi (1-based)
//es. k=1, divido il primo elemento dagli altri
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);
if(a) a->par = nullptr, a->isleft=0; //se serve get_pos
p->left = b;
if(b) b->par = p, b->isleft = 1; //se serve get_pos
return {a, p->fix()};
} else {
auto [a, b] = split(p->right, k-sl-1);
if(b) b->par = nullptr, b->isleft=0; //se serve get_pos
p->right = a;
if(a) a->par = p, a->isleft = 0; //se serve get_pos
return {p->fix(), b};
}//invariante: sui due nodi di ritorno
} //è sempre stata chiamata ->fix()
int find_kth(node* p,int pos){
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:31:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
31 | if (!a) return b; a->fix();
| ^~
Main.cpp:31:23: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
31 | if (!a) return b; a->fix();
| ^
Main.cpp:32:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
32 | if (!b) return a; b->fix();
| ^~
Main.cpp:32:23: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
32 | if (!b) return a; b->fix();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
278 ms |
27212 KB |
Output is correct |
2 |
Correct |
286 ms |
26124 KB |
Output is correct |
3 |
Correct |
254 ms |
25660 KB |
Output is correct |
4 |
Correct |
262 ms |
25580 KB |
Output is correct |
5 |
Correct |
271 ms |
28068 KB |
Output is correct |
6 |
Correct |
244 ms |
28136 KB |
Output is correct |
7 |
Correct |
311 ms |
28612 KB |
Output is correct |
8 |
Correct |
262 ms |
27012 KB |
Output is correct |
9 |
Correct |
253 ms |
25952 KB |
Output is correct |
10 |
Correct |
252 ms |
25676 KB |
Output is correct |
11 |
Correct |
248 ms |
26040 KB |
Output is correct |
12 |
Correct |
251 ms |
24272 KB |
Output is correct |
13 |
Correct |
249 ms |
25068 KB |
Output is correct |
14 |
Correct |
261 ms |
27232 KB |
Output is correct |
15 |
Correct |
281 ms |
25484 KB |
Output is correct |
16 |
Correct |
3 ms |
5036 KB |
Output is correct |
17 |
Correct |
245 ms |
24404 KB |
Output is correct |
18 |
Correct |
251 ms |
24356 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
647 ms |
40780 KB |
Output is correct |
2 |
Correct |
468 ms |
40736 KB |
Output is correct |
3 |
Correct |
457 ms |
36232 KB |
Output is correct |
4 |
Correct |
443 ms |
36488 KB |
Output is correct |
5 |
Correct |
458 ms |
37024 KB |
Output is correct |
6 |
Correct |
482 ms |
35940 KB |
Output is correct |
7 |
Correct |
458 ms |
40600 KB |
Output is correct |
8 |
Correct |
501 ms |
39380 KB |
Output is correct |
9 |
Correct |
415 ms |
36952 KB |
Output is correct |
10 |
Correct |
419 ms |
39176 KB |
Output is correct |
11 |
Correct |
390 ms |
36788 KB |
Output is correct |
12 |
Correct |
377 ms |
35828 KB |
Output is correct |
13 |
Correct |
399 ms |
39356 KB |
Output is correct |
14 |
Correct |
372 ms |
36648 KB |
Output is correct |
15 |
Correct |
435 ms |
39972 KB |
Output is correct |
16 |
Correct |
115 ms |
19340 KB |
Output is correct |
17 |
Correct |
458 ms |
39372 KB |
Output is correct |
18 |
Correct |
326 ms |
35992 KB |
Output is correct |
19 |
Correct |
189 ms |
24024 KB |
Output is correct |
20 |
Correct |
186 ms |
24500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
227 ms |
16420 KB |
Output is correct |
2 |
Correct |
195 ms |
15468 KB |
Output is correct |
3 |
Correct |
169 ms |
15608 KB |
Output is correct |
4 |
Correct |
139 ms |
14968 KB |
Output is correct |
5 |
Correct |
209 ms |
15320 KB |
Output is correct |
6 |
Correct |
137 ms |
15108 KB |
Output is correct |
7 |
Correct |
162 ms |
15156 KB |
Output is correct |
8 |
Correct |
177 ms |
14848 KB |
Output is correct |
9 |
Correct |
174 ms |
15612 KB |
Output is correct |
10 |
Correct |
124 ms |
14820 KB |
Output is correct |
11 |
Correct |
132 ms |
14832 KB |
Output is correct |
12 |
Correct |
147 ms |
14848 KB |
Output is correct |
13 |
Correct |
116 ms |
14748 KB |
Output is correct |
14 |
Correct |
118 ms |
15160 KB |
Output is correct |
15 |
Correct |
156 ms |
14592 KB |
Output is correct |
16 |
Correct |
52 ms |
11916 KB |
Output is correct |
17 |
Correct |
107 ms |
14052 KB |
Output is correct |
18 |
Correct |
111 ms |
14144 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
278 ms |
27212 KB |
Output is correct |
2 |
Correct |
286 ms |
26124 KB |
Output is correct |
3 |
Correct |
254 ms |
25660 KB |
Output is correct |
4 |
Correct |
262 ms |
25580 KB |
Output is correct |
5 |
Correct |
271 ms |
28068 KB |
Output is correct |
6 |
Correct |
244 ms |
28136 KB |
Output is correct |
7 |
Correct |
311 ms |
28612 KB |
Output is correct |
8 |
Correct |
262 ms |
27012 KB |
Output is correct |
9 |
Correct |
253 ms |
25952 KB |
Output is correct |
10 |
Correct |
252 ms |
25676 KB |
Output is correct |
11 |
Correct |
248 ms |
26040 KB |
Output is correct |
12 |
Correct |
251 ms |
24272 KB |
Output is correct |
13 |
Correct |
249 ms |
25068 KB |
Output is correct |
14 |
Correct |
261 ms |
27232 KB |
Output is correct |
15 |
Correct |
281 ms |
25484 KB |
Output is correct |
16 |
Correct |
3 ms |
5036 KB |
Output is correct |
17 |
Correct |
245 ms |
24404 KB |
Output is correct |
18 |
Correct |
251 ms |
24356 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
647 ms |
40780 KB |
Output is correct |
22 |
Correct |
468 ms |
40736 KB |
Output is correct |
23 |
Correct |
457 ms |
36232 KB |
Output is correct |
24 |
Correct |
443 ms |
36488 KB |
Output is correct |
25 |
Correct |
458 ms |
37024 KB |
Output is correct |
26 |
Correct |
482 ms |
35940 KB |
Output is correct |
27 |
Correct |
458 ms |
40600 KB |
Output is correct |
28 |
Correct |
501 ms |
39380 KB |
Output is correct |
29 |
Correct |
415 ms |
36952 KB |
Output is correct |
30 |
Correct |
419 ms |
39176 KB |
Output is correct |
31 |
Correct |
390 ms |
36788 KB |
Output is correct |
32 |
Correct |
377 ms |
35828 KB |
Output is correct |
33 |
Correct |
399 ms |
39356 KB |
Output is correct |
34 |
Correct |
372 ms |
36648 KB |
Output is correct |
35 |
Correct |
435 ms |
39972 KB |
Output is correct |
36 |
Correct |
115 ms |
19340 KB |
Output is correct |
37 |
Correct |
458 ms |
39372 KB |
Output is correct |
38 |
Correct |
326 ms |
35992 KB |
Output is correct |
39 |
Correct |
189 ms |
24024 KB |
Output is correct |
40 |
Correct |
186 ms |
24500 KB |
Output is correct |
41 |
Correct |
227 ms |
16420 KB |
Output is correct |
42 |
Correct |
195 ms |
15468 KB |
Output is correct |
43 |
Correct |
169 ms |
15608 KB |
Output is correct |
44 |
Correct |
139 ms |
14968 KB |
Output is correct |
45 |
Correct |
209 ms |
15320 KB |
Output is correct |
46 |
Correct |
137 ms |
15108 KB |
Output is correct |
47 |
Correct |
162 ms |
15156 KB |
Output is correct |
48 |
Correct |
177 ms |
14848 KB |
Output is correct |
49 |
Correct |
174 ms |
15612 KB |
Output is correct |
50 |
Correct |
124 ms |
14820 KB |
Output is correct |
51 |
Correct |
132 ms |
14832 KB |
Output is correct |
52 |
Correct |
147 ms |
14848 KB |
Output is correct |
53 |
Correct |
116 ms |
14748 KB |
Output is correct |
54 |
Correct |
118 ms |
15160 KB |
Output is correct |
55 |
Correct |
156 ms |
14592 KB |
Output is correct |
56 |
Correct |
52 ms |
11916 KB |
Output is correct |
57 |
Correct |
107 ms |
14052 KB |
Output is correct |
58 |
Correct |
111 ms |
14144 KB |
Output is correct |
59 |
Correct |
3 ms |
4948 KB |
Output is correct |
60 |
Correct |
3 ms |
4948 KB |
Output is correct |
61 |
Correct |
1422 ms |
46760 KB |
Output is correct |
62 |
Correct |
1276 ms |
45032 KB |
Output is correct |
63 |
Correct |
1182 ms |
44004 KB |
Output is correct |
64 |
Correct |
1089 ms |
43324 KB |
Output is correct |
65 |
Correct |
1187 ms |
44332 KB |
Output is correct |
66 |
Correct |
1200 ms |
42916 KB |
Output is correct |
67 |
Correct |
1082 ms |
42292 KB |
Output is correct |
68 |
Correct |
1063 ms |
40800 KB |
Output is correct |
69 |
Correct |
1036 ms |
42964 KB |
Output is correct |
70 |
Correct |
938 ms |
39940 KB |
Output is correct |
71 |
Correct |
917 ms |
41852 KB |
Output is correct |
72 |
Correct |
896 ms |
40012 KB |
Output is correct |
73 |
Correct |
931 ms |
41008 KB |
Output is correct |
74 |
Correct |
918 ms |
42312 KB |
Output is correct |
75 |
Correct |
916 ms |
40544 KB |
Output is correct |
76 |
Correct |
119 ms |
18944 KB |
Output is correct |
77 |
Correct |
884 ms |
37732 KB |
Output is correct |
78 |
Correct |
841 ms |
37584 KB |
Output is correct |
79 |
Correct |
2 ms |
4948 KB |
Output is correct |
80 |
Correct |
3 ms |
4948 KB |
Output is correct |