#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
mt19937 rng(199);
int gen(int m){
return ((int)rng() % m + m) % m;
}
struct node{
int cnt;
int maxi;
int key;
int prior;
node *left;
node *right;
node(int val) : cnt(1), maxi(val), key(val), prior(gen((int)1e9)), left(NULL), right(NULL) {};
};
typedef node* pnode;
int get_cnt(pnode x){
if(!x) return 0;
return x->cnt;
}
int get_max(pnode x){
if(!x) return 0;
return x->maxi;
}
void update(pnode &t){
if(!t) return;
t->cnt = get_cnt(t->left) + 1 + get_cnt(t->right);
t->maxi = max({get_max(t->left), t->key, get_max(t->right)});
}
void split_by_size(pnode T, int key, pnode &lef, pnode &rig){
if(!T){
lef=rig=NULL;
return;
}
int id = get_cnt(T->left) + 1;
if(id <= key){
split_by_size(T->right, key-id, T->right, rig);
lef = T;
}
else{
split_by_size(T->left, key, lef, T->left);
rig = T;
}
update(T);
}
void split_by_max(pnode T, int key, pnode &lef, pnode &rig){
if(!T){
lef=rig=NULL;
return;
}
int id = max(get_max(T->left), T->key);
if(id <= key){
split_by_max(T->right, key, T->right, rig);
lef = T;
}
else{
split_by_max(T->left, key, lef, T->left);
rig = T;
}
update(T);
}
void merge(pnode &T, pnode L, pnode R){ // assume keys in L are strictly smaller than R
if(!L && !R){
T = NULL;
return;
}
if(!L){
T = R;
return;
}
if(!R){
T = L;
return;
}
if((L -> prior) > (R -> prior)){
merge(L -> right, L -> right, R);
T = L;
}
else{
merge(R -> left, L, R -> left);
T = R;
}
update(T);
}
int get_id(pnode T, int idx){
if(!T) return -1;
int id = get_cnt(T->left) + 1;
if(id == idx){
return T->key;
}
else if(id < idx){
return get_id(T->right, idx-id);
}
else{
return get_id(T->left, idx);
}
}
const int N = (int)2e5 + 10;
const int M = (int)1e6 + 10;
vector<pii> qq[N];
int res[M];
int main(){
fastIO;
//freopen("in.txt", "r", stdin);
int n, q;
cin >> n >> q;
pnode treap = NULL;
int p;
for(int i = 0 ; i < n; i ++ ){
cin >> p;
merge(treap, treap, new node(p));
}
int t, ii;
for(int iq = 1; iq <= q; iq ++ ){
cin >> t >> ii;
t=min(t,n);
qq[t].push_back(mp(iq, ii));
}
int take;
int oper = 0;
for(int it = 0; it <= n; it ++ ){
if(it > 0){
pnode sa, sb;
split_by_size(treap, n / 2, sa, sb);
treap = NULL;
while(sb != NULL && sa != NULL){
take = get_id(sb, 1);
pnode c, d;
if(take < sa->maxi){
split_by_max(sa, take, c, sa);
int big = (sb ? get_id(sa, 1) : (int)1e9);
split_by_max(sb, big, d, sb);
merge(treap, treap, c);
merge(treap, treap, d);
//return 0;
}
else{
break;
}
}
merge(treap, treap, sa);
merge(treap, treap, sb);
}
for(auto x : qq[it]){
res[x.fi] = get_id(treap, x.se);
}
}
//cout << oper << " !\n";
for(int iq = 1; iq <= q; iq ++ ){
cout << res[iq] << "\n";
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:142:9: warning: unused variable 'oper' [-Wunused-variable]
142 | int oper = 0;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
222 ms |
23088 KB |
Output is correct |
2 |
Correct |
232 ms |
21772 KB |
Output is correct |
3 |
Correct |
217 ms |
21432 KB |
Output is correct |
4 |
Correct |
204 ms |
20440 KB |
Output is correct |
5 |
Correct |
218 ms |
23652 KB |
Output is correct |
6 |
Correct |
248 ms |
24104 KB |
Output is correct |
7 |
Correct |
261 ms |
24736 KB |
Output is correct |
8 |
Correct |
208 ms |
22720 KB |
Output is correct |
9 |
Correct |
199 ms |
22100 KB |
Output is correct |
10 |
Correct |
204 ms |
21452 KB |
Output is correct |
11 |
Correct |
203 ms |
21852 KB |
Output is correct |
12 |
Correct |
213 ms |
20136 KB |
Output is correct |
13 |
Correct |
208 ms |
20872 KB |
Output is correct |
14 |
Correct |
210 ms |
23228 KB |
Output is correct |
15 |
Correct |
209 ms |
21552 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
203 ms |
20292 KB |
Output is correct |
18 |
Correct |
213 ms |
20200 KB |
Output is correct |
19 |
Correct |
2 ms |
5032 KB |
Output is correct |
20 |
Correct |
3 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
421 ms |
32708 KB |
Output is correct |
2 |
Correct |
376 ms |
46108 KB |
Output is correct |
3 |
Correct |
300 ms |
38992 KB |
Output is correct |
4 |
Correct |
334 ms |
39304 KB |
Output is correct |
5 |
Correct |
342 ms |
40008 KB |
Output is correct |
6 |
Correct |
311 ms |
38712 KB |
Output is correct |
7 |
Correct |
371 ms |
45960 KB |
Output is correct |
8 |
Correct |
387 ms |
44220 KB |
Output is correct |
9 |
Correct |
324 ms |
39844 KB |
Output is correct |
10 |
Correct |
324 ms |
43128 KB |
Output is correct |
11 |
Correct |
280 ms |
38056 KB |
Output is correct |
12 |
Correct |
288 ms |
37844 KB |
Output is correct |
13 |
Correct |
316 ms |
42512 KB |
Output is correct |
14 |
Correct |
278 ms |
38936 KB |
Output is correct |
15 |
Correct |
350 ms |
44112 KB |
Output is correct |
16 |
Correct |
86 ms |
15568 KB |
Output is correct |
17 |
Correct |
293 ms |
40892 KB |
Output is correct |
18 |
Correct |
245 ms |
35748 KB |
Output is correct |
19 |
Correct |
129 ms |
20796 KB |
Output is correct |
20 |
Correct |
147 ms |
21992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
144 ms |
12728 KB |
Output is correct |
2 |
Correct |
104 ms |
13924 KB |
Output is correct |
3 |
Correct |
127 ms |
13792 KB |
Output is correct |
4 |
Correct |
87 ms |
13448 KB |
Output is correct |
5 |
Correct |
124 ms |
14076 KB |
Output is correct |
6 |
Correct |
94 ms |
13412 KB |
Output is correct |
7 |
Correct |
143 ms |
14028 KB |
Output is correct |
8 |
Correct |
104 ms |
13388 KB |
Output is correct |
9 |
Correct |
127 ms |
13780 KB |
Output is correct |
10 |
Correct |
100 ms |
13128 KB |
Output is correct |
11 |
Correct |
102 ms |
13484 KB |
Output is correct |
12 |
Correct |
81 ms |
13136 KB |
Output is correct |
13 |
Correct |
88 ms |
13096 KB |
Output is correct |
14 |
Correct |
82 ms |
13456 KB |
Output is correct |
15 |
Correct |
79 ms |
13148 KB |
Output is correct |
16 |
Correct |
35 ms |
10248 KB |
Output is correct |
17 |
Correct |
73 ms |
12656 KB |
Output is correct |
18 |
Correct |
80 ms |
12644 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
5036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
222 ms |
23088 KB |
Output is correct |
2 |
Correct |
232 ms |
21772 KB |
Output is correct |
3 |
Correct |
217 ms |
21432 KB |
Output is correct |
4 |
Correct |
204 ms |
20440 KB |
Output is correct |
5 |
Correct |
218 ms |
23652 KB |
Output is correct |
6 |
Correct |
248 ms |
24104 KB |
Output is correct |
7 |
Correct |
261 ms |
24736 KB |
Output is correct |
8 |
Correct |
208 ms |
22720 KB |
Output is correct |
9 |
Correct |
199 ms |
22100 KB |
Output is correct |
10 |
Correct |
204 ms |
21452 KB |
Output is correct |
11 |
Correct |
203 ms |
21852 KB |
Output is correct |
12 |
Correct |
213 ms |
20136 KB |
Output is correct |
13 |
Correct |
208 ms |
20872 KB |
Output is correct |
14 |
Correct |
210 ms |
23228 KB |
Output is correct |
15 |
Correct |
209 ms |
21552 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
203 ms |
20292 KB |
Output is correct |
18 |
Correct |
213 ms |
20200 KB |
Output is correct |
19 |
Correct |
2 ms |
5032 KB |
Output is correct |
20 |
Correct |
3 ms |
4992 KB |
Output is correct |
21 |
Correct |
421 ms |
32708 KB |
Output is correct |
22 |
Correct |
376 ms |
46108 KB |
Output is correct |
23 |
Correct |
300 ms |
38992 KB |
Output is correct |
24 |
Correct |
334 ms |
39304 KB |
Output is correct |
25 |
Correct |
342 ms |
40008 KB |
Output is correct |
26 |
Correct |
311 ms |
38712 KB |
Output is correct |
27 |
Correct |
371 ms |
45960 KB |
Output is correct |
28 |
Correct |
387 ms |
44220 KB |
Output is correct |
29 |
Correct |
324 ms |
39844 KB |
Output is correct |
30 |
Correct |
324 ms |
43128 KB |
Output is correct |
31 |
Correct |
280 ms |
38056 KB |
Output is correct |
32 |
Correct |
288 ms |
37844 KB |
Output is correct |
33 |
Correct |
316 ms |
42512 KB |
Output is correct |
34 |
Correct |
278 ms |
38936 KB |
Output is correct |
35 |
Correct |
350 ms |
44112 KB |
Output is correct |
36 |
Correct |
86 ms |
15568 KB |
Output is correct |
37 |
Correct |
293 ms |
40892 KB |
Output is correct |
38 |
Correct |
245 ms |
35748 KB |
Output is correct |
39 |
Correct |
129 ms |
20796 KB |
Output is correct |
40 |
Correct |
147 ms |
21992 KB |
Output is correct |
41 |
Correct |
144 ms |
12728 KB |
Output is correct |
42 |
Correct |
104 ms |
13924 KB |
Output is correct |
43 |
Correct |
127 ms |
13792 KB |
Output is correct |
44 |
Correct |
87 ms |
13448 KB |
Output is correct |
45 |
Correct |
124 ms |
14076 KB |
Output is correct |
46 |
Correct |
94 ms |
13412 KB |
Output is correct |
47 |
Correct |
143 ms |
14028 KB |
Output is correct |
48 |
Correct |
104 ms |
13388 KB |
Output is correct |
49 |
Correct |
127 ms |
13780 KB |
Output is correct |
50 |
Correct |
100 ms |
13128 KB |
Output is correct |
51 |
Correct |
102 ms |
13484 KB |
Output is correct |
52 |
Correct |
81 ms |
13136 KB |
Output is correct |
53 |
Correct |
88 ms |
13096 KB |
Output is correct |
54 |
Correct |
82 ms |
13456 KB |
Output is correct |
55 |
Correct |
79 ms |
13148 KB |
Output is correct |
56 |
Correct |
35 ms |
10248 KB |
Output is correct |
57 |
Correct |
73 ms |
12656 KB |
Output is correct |
58 |
Correct |
80 ms |
12644 KB |
Output is correct |
59 |
Correct |
3 ms |
4948 KB |
Output is correct |
60 |
Correct |
2 ms |
5036 KB |
Output is correct |
61 |
Correct |
981 ms |
53612 KB |
Output is correct |
62 |
Correct |
715 ms |
51560 KB |
Output is correct |
63 |
Correct |
704 ms |
50216 KB |
Output is correct |
64 |
Correct |
753 ms |
49960 KB |
Output is correct |
65 |
Correct |
787 ms |
51792 KB |
Output is correct |
66 |
Correct |
637 ms |
49716 KB |
Output is correct |
67 |
Correct |
640 ms |
49432 KB |
Output is correct |
68 |
Correct |
658 ms |
47652 KB |
Output is correct |
69 |
Correct |
793 ms |
50028 KB |
Output is correct |
70 |
Correct |
637 ms |
45996 KB |
Output is correct |
71 |
Correct |
574 ms |
47616 KB |
Output is correct |
72 |
Correct |
721 ms |
46460 KB |
Output is correct |
73 |
Correct |
634 ms |
46832 KB |
Output is correct |
74 |
Correct |
653 ms |
49012 KB |
Output is correct |
75 |
Correct |
693 ms |
47700 KB |
Output is correct |
76 |
Correct |
75 ms |
15588 KB |
Output is correct |
77 |
Correct |
555 ms |
40956 KB |
Output is correct |
78 |
Correct |
539 ms |
40808 KB |
Output is correct |
79 |
Correct |
3 ms |
4948 KB |
Output is correct |
80 |
Correct |
3 ms |
5040 KB |
Output is correct |