#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 |
271 ms |
28052 KB |
Output is correct |
2 |
Correct |
263 ms |
27680 KB |
Output is correct |
3 |
Correct |
267 ms |
26956 KB |
Output is correct |
4 |
Correct |
244 ms |
27380 KB |
Output is correct |
5 |
Correct |
281 ms |
29900 KB |
Output is correct |
6 |
Correct |
244 ms |
29648 KB |
Output is correct |
7 |
Correct |
261 ms |
29516 KB |
Output is correct |
8 |
Correct |
246 ms |
27896 KB |
Output is correct |
9 |
Correct |
244 ms |
27112 KB |
Output is correct |
10 |
Correct |
253 ms |
27084 KB |
Output is correct |
11 |
Correct |
248 ms |
26828 KB |
Output is correct |
12 |
Correct |
234 ms |
24692 KB |
Output is correct |
13 |
Correct |
254 ms |
25580 KB |
Output is correct |
14 |
Correct |
249 ms |
29452 KB |
Output is correct |
15 |
Correct |
257 ms |
27560 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
239 ms |
25624 KB |
Output is correct |
18 |
Correct |
240 ms |
25404 KB |
Output is correct |
19 |
Correct |
2 ms |
5076 KB |
Output is correct |
20 |
Correct |
2 ms |
5036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
597 ms |
47356 KB |
Output is correct |
2 |
Correct |
459 ms |
46244 KB |
Output is correct |
3 |
Correct |
423 ms |
39556 KB |
Output is correct |
4 |
Correct |
419 ms |
37740 KB |
Output is correct |
5 |
Correct |
433 ms |
38452 KB |
Output is correct |
6 |
Correct |
454 ms |
38784 KB |
Output is correct |
7 |
Correct |
447 ms |
42416 KB |
Output is correct |
8 |
Correct |
456 ms |
43076 KB |
Output is correct |
9 |
Correct |
390 ms |
38076 KB |
Output is correct |
10 |
Correct |
412 ms |
41648 KB |
Output is correct |
11 |
Correct |
348 ms |
36400 KB |
Output is correct |
12 |
Correct |
369 ms |
35820 KB |
Output is correct |
13 |
Correct |
401 ms |
41376 KB |
Output is correct |
14 |
Correct |
368 ms |
39284 KB |
Output is correct |
15 |
Correct |
412 ms |
44256 KB |
Output is correct |
16 |
Correct |
106 ms |
16140 KB |
Output is correct |
17 |
Correct |
416 ms |
39472 KB |
Output is correct |
18 |
Correct |
313 ms |
34336 KB |
Output is correct |
19 |
Correct |
190 ms |
20888 KB |
Output is correct |
20 |
Correct |
184 ms |
21572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
205 ms |
14872 KB |
Output is correct |
2 |
Correct |
164 ms |
13908 KB |
Output is correct |
3 |
Correct |
163 ms |
14100 KB |
Output is correct |
4 |
Correct |
126 ms |
13388 KB |
Output is correct |
5 |
Correct |
195 ms |
13820 KB |
Output is correct |
6 |
Correct |
126 ms |
13544 KB |
Output is correct |
7 |
Correct |
157 ms |
13572 KB |
Output is correct |
8 |
Correct |
144 ms |
13260 KB |
Output is correct |
9 |
Correct |
154 ms |
14024 KB |
Output is correct |
10 |
Correct |
117 ms |
13332 KB |
Output is correct |
11 |
Correct |
101 ms |
13132 KB |
Output is correct |
12 |
Correct |
130 ms |
13388 KB |
Output is correct |
13 |
Correct |
104 ms |
13244 KB |
Output is correct |
14 |
Correct |
111 ms |
13560 KB |
Output is correct |
15 |
Correct |
110 ms |
13004 KB |
Output is correct |
16 |
Correct |
51 ms |
10444 KB |
Output is correct |
17 |
Correct |
103 ms |
12488 KB |
Output is correct |
18 |
Correct |
108 ms |
12756 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 |
271 ms |
28052 KB |
Output is correct |
2 |
Correct |
263 ms |
27680 KB |
Output is correct |
3 |
Correct |
267 ms |
26956 KB |
Output is correct |
4 |
Correct |
244 ms |
27380 KB |
Output is correct |
5 |
Correct |
281 ms |
29900 KB |
Output is correct |
6 |
Correct |
244 ms |
29648 KB |
Output is correct |
7 |
Correct |
261 ms |
29516 KB |
Output is correct |
8 |
Correct |
246 ms |
27896 KB |
Output is correct |
9 |
Correct |
244 ms |
27112 KB |
Output is correct |
10 |
Correct |
253 ms |
27084 KB |
Output is correct |
11 |
Correct |
248 ms |
26828 KB |
Output is correct |
12 |
Correct |
234 ms |
24692 KB |
Output is correct |
13 |
Correct |
254 ms |
25580 KB |
Output is correct |
14 |
Correct |
249 ms |
29452 KB |
Output is correct |
15 |
Correct |
257 ms |
27560 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
239 ms |
25624 KB |
Output is correct |
18 |
Correct |
240 ms |
25404 KB |
Output is correct |
19 |
Correct |
2 ms |
5076 KB |
Output is correct |
20 |
Correct |
2 ms |
5036 KB |
Output is correct |
21 |
Correct |
597 ms |
47356 KB |
Output is correct |
22 |
Correct |
459 ms |
46244 KB |
Output is correct |
23 |
Correct |
423 ms |
39556 KB |
Output is correct |
24 |
Correct |
419 ms |
37740 KB |
Output is correct |
25 |
Correct |
433 ms |
38452 KB |
Output is correct |
26 |
Correct |
454 ms |
38784 KB |
Output is correct |
27 |
Correct |
447 ms |
42416 KB |
Output is correct |
28 |
Correct |
456 ms |
43076 KB |
Output is correct |
29 |
Correct |
390 ms |
38076 KB |
Output is correct |
30 |
Correct |
412 ms |
41648 KB |
Output is correct |
31 |
Correct |
348 ms |
36400 KB |
Output is correct |
32 |
Correct |
369 ms |
35820 KB |
Output is correct |
33 |
Correct |
401 ms |
41376 KB |
Output is correct |
34 |
Correct |
368 ms |
39284 KB |
Output is correct |
35 |
Correct |
412 ms |
44256 KB |
Output is correct |
36 |
Correct |
106 ms |
16140 KB |
Output is correct |
37 |
Correct |
416 ms |
39472 KB |
Output is correct |
38 |
Correct |
313 ms |
34336 KB |
Output is correct |
39 |
Correct |
190 ms |
20888 KB |
Output is correct |
40 |
Correct |
184 ms |
21572 KB |
Output is correct |
41 |
Correct |
205 ms |
14872 KB |
Output is correct |
42 |
Correct |
164 ms |
13908 KB |
Output is correct |
43 |
Correct |
163 ms |
14100 KB |
Output is correct |
44 |
Correct |
126 ms |
13388 KB |
Output is correct |
45 |
Correct |
195 ms |
13820 KB |
Output is correct |
46 |
Correct |
126 ms |
13544 KB |
Output is correct |
47 |
Correct |
157 ms |
13572 KB |
Output is correct |
48 |
Correct |
144 ms |
13260 KB |
Output is correct |
49 |
Correct |
154 ms |
14024 KB |
Output is correct |
50 |
Correct |
117 ms |
13332 KB |
Output is correct |
51 |
Correct |
101 ms |
13132 KB |
Output is correct |
52 |
Correct |
130 ms |
13388 KB |
Output is correct |
53 |
Correct |
104 ms |
13244 KB |
Output is correct |
54 |
Correct |
111 ms |
13560 KB |
Output is correct |
55 |
Correct |
110 ms |
13004 KB |
Output is correct |
56 |
Correct |
51 ms |
10444 KB |
Output is correct |
57 |
Correct |
103 ms |
12488 KB |
Output is correct |
58 |
Correct |
108 ms |
12756 KB |
Output is correct |
59 |
Correct |
3 ms |
4948 KB |
Output is correct |
60 |
Correct |
3 ms |
4948 KB |
Output is correct |
61 |
Correct |
1347 ms |
50652 KB |
Output is correct |
62 |
Correct |
1033 ms |
49076 KB |
Output is correct |
63 |
Correct |
1086 ms |
46636 KB |
Output is correct |
64 |
Correct |
987 ms |
46452 KB |
Output is correct |
65 |
Correct |
1093 ms |
49100 KB |
Output is correct |
66 |
Correct |
1066 ms |
46296 KB |
Output is correct |
67 |
Correct |
930 ms |
46028 KB |
Output is correct |
68 |
Correct |
914 ms |
45076 KB |
Output is correct |
69 |
Correct |
953 ms |
46420 KB |
Output is correct |
70 |
Correct |
801 ms |
44084 KB |
Output is correct |
71 |
Correct |
759 ms |
45108 KB |
Output is correct |
72 |
Correct |
839 ms |
42700 KB |
Output is correct |
73 |
Correct |
773 ms |
43680 KB |
Output is correct |
74 |
Correct |
854 ms |
46480 KB |
Output is correct |
75 |
Correct |
821 ms |
43392 KB |
Output is correct |
76 |
Correct |
105 ms |
15784 KB |
Output is correct |
77 |
Correct |
750 ms |
38804 KB |
Output is correct |
78 |
Correct |
668 ms |
39720 KB |
Output is correct |
79 |
Correct |
3 ms |
4948 KB |
Output is correct |
80 |
Correct |
3 ms |
5028 KB |
Output is correct |