#include<bits/stdc++.h>
#define de(x) cout << #x << " = " << x << endl;
#define all(A) A.begin(), A.end()
using namespace std;
const int MAXN = 2e5;
const int MAXQ = 1e6;
ostream& operator<<(ostream& out, array<int, 3> v){
out << "(" << v[0] << " " << v[1] << " " << v[2] << ")";
return out;
}
struct Treap{
struct Node{
array<int, 3> val;
int my_size, acc_size;
int prior;
Node *lc, *rc;
Node() {}
Node(array<int, 3> val) : val(val), prior(rand()), lc(nullptr), rc(nullptr), my_size(val[2] - val[1] + 1), acc_size(val[2] - val[1] + 1) {}
void update(){
acc_size = my_size + (lc == nullptr ? 0 : lc -> acc_size) + (rc == nullptr ? 0 : rc -> acc_size);
}
};
Node* root;
pair<Node*, Node*> split(Node* node, array<int, 3> key){
if(node == nullptr) return {nullptr, nullptr};
if(node -> val <= key){
auto [lt, rt] = split(node -> rc, key);
node -> rc = lt;
node -> update();
return {node, rt};
}
else{
auto [lt, rt] = split(node -> lc, key);
node -> lc = rt;
node -> update();
return {lt, node};
}
}
Node* merge(Node* lt, Node* rt){
if(lt == nullptr) return rt;
if(rt == nullptr) return lt;
if(lt -> prior < rt -> prior){
rt -> lc = merge(lt, rt -> lc);
rt -> update();
return rt;
}
else{
lt -> rc = merge(lt -> rc, rt);
lt -> update();
return lt;
}
}
void insert(array<int, 3> key){
auto [lt, rt] = split(root, key);
auto mt = new Node(key);
root = merge(merge(lt, mt), rt);
}
void erase(array<int, 3> low_key, array<int, 3> high_key){
auto [lt, mrt] = split(root, low_key);
auto [mt, rt] = split(mrt, high_key);
root = merge(lt, rt);
}
void util_print(Node* node){
if(node == nullptr) return;
util_print(node -> lc);
cout << node -> val << " ";
util_print(node -> rc);
}
void print(){ util_print(root); cout << endl; }
array<int, 3> util_search(Node* node, int &to_left, int key){
if(to_left + (node -> lc == nullptr ? 0 : node -> lc -> acc_size) < key && to_left + (node -> lc == nullptr ? 0 : node -> lc -> acc_size) + (node -> my_size) >= key){
to_left += (node -> lc == nullptr ? 0 : node -> lc -> acc_size);
return node -> val;
}
else if(to_left + (node -> lc == nullptr ? 0 : node -> lc -> acc_size) < key){
to_left += (node -> acc_size) - (node -> rc -> acc_size);
return util_search(node -> rc, to_left, key);
}
else{
return util_search(node -> lc, to_left, key);
}
}
pair<int, array<int, 3>> search(int key){
int shift = 0;
auto ret = util_search(root, shift, key);
return {shift, ret};
}
};
int n, q, a[MAXN + 5], ans[MAXQ + 5], nxt[MAXN + 5];
vector<array<int, 3>> queries;
Treap tr;
bool riffle_shuffle(){
auto [shift, block] = tr.search(n / 2);
// cout << "middle block is " << block << " and previous blocks shift with " << shift << endl;
if(shift + block[2] - block[1] + 1 == n / 2) return false;
auto [v, l, r] = block;
tr.erase({v, 0, 0}, {v + 1, 0, 0});
tr.insert({v, l, n / 2 - shift + block[1] - 1});
int m = n / 2 - shift + block[1];
// de(m);
while(m > 0 && m <= r){
int my_r = min(r, nxt[m] - 1);
tr.insert({a[m], m, my_r});
// cout << "new block " << m << " " << my_r << endl;
m = my_r + 1;
}
// tr.print();
return true;
}
int main(){
cin >> n >> q;
for(int i = 1; i <= n; i ++){
cin >> a[i];
}
for(int i = 1; i <= n; ){
int j = i;
while(j <= n && a[j] <= a[i]){
++ j;
}
tr.insert({a[i], i, j - 1});
i = j;
}
// tr.print();
stack<int> st;
for(int i = n; i >= 1; i --){
while(!st.empty() && a[st.top()] < a[i]){
st.pop();
}
nxt[i] = (st.empty() ? n+1 : st.top());
st.push(i);
}
for(int i = 0; i < q; i ++){
int x, t; cin >> t >> x;
queries.push_back({t, x, i});
}
sort(all(queries));
int cur_time = 0;
for(auto [t, x, idx] : queries){
while(cur_time < t){
if(riffle_shuffle()) ++ cur_time;
else cur_time = t;
}
// cout << "(" << t << ", " << x << ", " << idx << ") -> "; tr.print();
auto [shift, block] = tr.search(x);
ans[idx] = a[x - shift + block[1] - 1];
}
for(int i = 0; i < q; i ++){
cout << ans[i] << endl;
}
}
/*
6 3
1 5 6 2 3 4
1 2
0 4
1 5
*/
Compilation message
Main.cpp: In constructor 'Treap::Node::Node(std::array<int, 3>)':
Main.cpp:19:14: warning: 'Treap::Node::rc' will be initialized after [-Wreorder]
19 | Node *lc, *rc;
| ^~
Main.cpp:17:7: warning: 'int Treap::Node::my_size' [-Wreorder]
17 | int my_size, acc_size;
| ^~~~~~~
Main.cpp:21:3: warning: when initialized here [-Wreorder]
21 | Node(array<int, 3> val) : val(val), prior(rand()), lc(nullptr), rc(nullptr), my_size(val[2] - val[1] + 1), acc_size(val[2] - val[1] + 1) {}
| ^~~~
Main.cpp: In member function 'std::pair<Treap::Node*, Treap::Node*> Treap::split(Treap::Node*, std::array<int, 3>)':
Main.cpp:30:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
30 | auto [lt, rt] = split(node -> rc, key);
| ^
Main.cpp:36:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
36 | auto [lt, rt] = split(node -> lc, key);
| ^
Main.cpp: In member function 'void Treap::insert(std::array<int, 3>)':
Main.cpp:57:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
57 | auto [lt, rt] = split(root, key);
| ^
Main.cpp: In member function 'void Treap::erase(std::array<int, 3>, std::array<int, 3>)':
Main.cpp:62:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
62 | auto [lt, mrt] = split(root, low_key);
| ^
Main.cpp:63:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
63 | auto [mt, rt] = split(mrt, high_key);
| ^
Main.cpp: In function 'bool riffle_shuffle()':
Main.cpp:98:7: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
98 | auto [shift, block] = tr.search(n / 2);
| ^
Main.cpp:101:7: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
101 | auto [v, l, r] = block;
| ^
Main.cpp: In function 'int main()':
Main.cpp:145:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
145 | for(auto [t, x, idx] : queries){
| ^
Main.cpp:151:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
151 | auto [shift, block] = tr.search(x);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1290 ms |
20760 KB |
Output is correct |
2 |
Correct |
1214 ms |
20652 KB |
Output is correct |
3 |
Correct |
1215 ms |
20648 KB |
Output is correct |
4 |
Correct |
1146 ms |
20672 KB |
Output is correct |
5 |
Correct |
1171 ms |
20660 KB |
Output is correct |
6 |
Correct |
1174 ms |
20052 KB |
Output is correct |
7 |
Correct |
1206 ms |
20720 KB |
Output is correct |
8 |
Correct |
1104 ms |
19844 KB |
Output is correct |
9 |
Correct |
1112 ms |
19636 KB |
Output is correct |
10 |
Correct |
1140 ms |
20652 KB |
Output is correct |
11 |
Correct |
1196 ms |
20428 KB |
Output is correct |
12 |
Correct |
1152 ms |
19636 KB |
Output is correct |
13 |
Correct |
1191 ms |
20144 KB |
Output is correct |
14 |
Correct |
1204 ms |
20668 KB |
Output is correct |
15 |
Correct |
1185 ms |
20660 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1269 ms |
19980 KB |
Output is correct |
18 |
Correct |
1207 ms |
20140 KB |
Output is correct |
19 |
Correct |
0 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1474 ms |
38640 KB |
Output is correct |
2 |
Correct |
1341 ms |
48844 KB |
Output is correct |
3 |
Correct |
1102 ms |
41644 KB |
Output is correct |
4 |
Correct |
1148 ms |
35508 KB |
Output is correct |
5 |
Correct |
1141 ms |
37300 KB |
Output is correct |
6 |
Correct |
1070 ms |
33660 KB |
Output is correct |
7 |
Correct |
1316 ms |
39604 KB |
Output is correct |
8 |
Correct |
1291 ms |
37804 KB |
Output is correct |
9 |
Correct |
1097 ms |
34992 KB |
Output is correct |
10 |
Correct |
1210 ms |
35252 KB |
Output is correct |
11 |
Correct |
1006 ms |
30900 KB |
Output is correct |
12 |
Correct |
1054 ms |
31144 KB |
Output is correct |
13 |
Correct |
1220 ms |
34508 KB |
Output is correct |
14 |
Correct |
1031 ms |
30392 KB |
Output is correct |
15 |
Correct |
1239 ms |
36528 KB |
Output is correct |
16 |
Correct |
45 ms |
4688 KB |
Output is correct |
17 |
Correct |
1265 ms |
42408 KB |
Output is correct |
18 |
Correct |
1000 ms |
27808 KB |
Output is correct |
19 |
Correct |
266 ms |
9924 KB |
Output is correct |
20 |
Correct |
290 ms |
11556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
231 ms |
13944 KB |
Output is correct |
2 |
Correct |
183 ms |
12484 KB |
Output is correct |
3 |
Correct |
187 ms |
13248 KB |
Output is correct |
4 |
Correct |
154 ms |
7116 KB |
Output is correct |
5 |
Correct |
174 ms |
10656 KB |
Output is correct |
6 |
Correct |
150 ms |
7652 KB |
Output is correct |
7 |
Correct |
186 ms |
9416 KB |
Output is correct |
8 |
Correct |
166 ms |
8132 KB |
Output is correct |
9 |
Correct |
183 ms |
9236 KB |
Output is correct |
10 |
Correct |
151 ms |
6276 KB |
Output is correct |
11 |
Correct |
151 ms |
6344 KB |
Output is correct |
12 |
Correct |
138 ms |
6248 KB |
Output is correct |
13 |
Correct |
150 ms |
6368 KB |
Output is correct |
14 |
Correct |
154 ms |
6340 KB |
Output is correct |
15 |
Correct |
142 ms |
6340 KB |
Output is correct |
16 |
Correct |
20 ms |
3692 KB |
Output is correct |
17 |
Correct |
154 ms |
10848 KB |
Output is correct |
18 |
Correct |
149 ms |
5976 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1290 ms |
20760 KB |
Output is correct |
2 |
Correct |
1214 ms |
20652 KB |
Output is correct |
3 |
Correct |
1215 ms |
20648 KB |
Output is correct |
4 |
Correct |
1146 ms |
20672 KB |
Output is correct |
5 |
Correct |
1171 ms |
20660 KB |
Output is correct |
6 |
Correct |
1174 ms |
20052 KB |
Output is correct |
7 |
Correct |
1206 ms |
20720 KB |
Output is correct |
8 |
Correct |
1104 ms |
19844 KB |
Output is correct |
9 |
Correct |
1112 ms |
19636 KB |
Output is correct |
10 |
Correct |
1140 ms |
20652 KB |
Output is correct |
11 |
Correct |
1196 ms |
20428 KB |
Output is correct |
12 |
Correct |
1152 ms |
19636 KB |
Output is correct |
13 |
Correct |
1191 ms |
20144 KB |
Output is correct |
14 |
Correct |
1204 ms |
20668 KB |
Output is correct |
15 |
Correct |
1185 ms |
20660 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1269 ms |
19980 KB |
Output is correct |
18 |
Correct |
1207 ms |
20140 KB |
Output is correct |
19 |
Correct |
0 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1474 ms |
38640 KB |
Output is correct |
22 |
Correct |
1341 ms |
48844 KB |
Output is correct |
23 |
Correct |
1102 ms |
41644 KB |
Output is correct |
24 |
Correct |
1148 ms |
35508 KB |
Output is correct |
25 |
Correct |
1141 ms |
37300 KB |
Output is correct |
26 |
Correct |
1070 ms |
33660 KB |
Output is correct |
27 |
Correct |
1316 ms |
39604 KB |
Output is correct |
28 |
Correct |
1291 ms |
37804 KB |
Output is correct |
29 |
Correct |
1097 ms |
34992 KB |
Output is correct |
30 |
Correct |
1210 ms |
35252 KB |
Output is correct |
31 |
Correct |
1006 ms |
30900 KB |
Output is correct |
32 |
Correct |
1054 ms |
31144 KB |
Output is correct |
33 |
Correct |
1220 ms |
34508 KB |
Output is correct |
34 |
Correct |
1031 ms |
30392 KB |
Output is correct |
35 |
Correct |
1239 ms |
36528 KB |
Output is correct |
36 |
Correct |
45 ms |
4688 KB |
Output is correct |
37 |
Correct |
1265 ms |
42408 KB |
Output is correct |
38 |
Correct |
1000 ms |
27808 KB |
Output is correct |
39 |
Correct |
266 ms |
9924 KB |
Output is correct |
40 |
Correct |
290 ms |
11556 KB |
Output is correct |
41 |
Correct |
231 ms |
13944 KB |
Output is correct |
42 |
Correct |
183 ms |
12484 KB |
Output is correct |
43 |
Correct |
187 ms |
13248 KB |
Output is correct |
44 |
Correct |
154 ms |
7116 KB |
Output is correct |
45 |
Correct |
174 ms |
10656 KB |
Output is correct |
46 |
Correct |
150 ms |
7652 KB |
Output is correct |
47 |
Correct |
186 ms |
9416 KB |
Output is correct |
48 |
Correct |
166 ms |
8132 KB |
Output is correct |
49 |
Correct |
183 ms |
9236 KB |
Output is correct |
50 |
Correct |
151 ms |
6276 KB |
Output is correct |
51 |
Correct |
151 ms |
6344 KB |
Output is correct |
52 |
Correct |
138 ms |
6248 KB |
Output is correct |
53 |
Correct |
150 ms |
6368 KB |
Output is correct |
54 |
Correct |
154 ms |
6340 KB |
Output is correct |
55 |
Correct |
142 ms |
6340 KB |
Output is correct |
56 |
Correct |
20 ms |
3692 KB |
Output is correct |
57 |
Correct |
154 ms |
10848 KB |
Output is correct |
58 |
Correct |
149 ms |
5976 KB |
Output is correct |
59 |
Correct |
1 ms |
2396 KB |
Output is correct |
60 |
Correct |
0 ms |
2396 KB |
Output is correct |
61 |
Correct |
1659 ms |
56664 KB |
Output is correct |
62 |
Correct |
1626 ms |
49580 KB |
Output is correct |
63 |
Correct |
1581 ms |
50092 KB |
Output is correct |
64 |
Correct |
1357 ms |
40844 KB |
Output is correct |
65 |
Correct |
1400 ms |
42932 KB |
Output is correct |
66 |
Correct |
1366 ms |
41392 KB |
Output is correct |
67 |
Correct |
1361 ms |
38832 KB |
Output is correct |
68 |
Correct |
1361 ms |
38068 KB |
Output is correct |
69 |
Correct |
1331 ms |
38608 KB |
Output is correct |
70 |
Correct |
1316 ms |
35816 KB |
Output is correct |
71 |
Correct |
1276 ms |
34228 KB |
Output is correct |
72 |
Correct |
1326 ms |
35440 KB |
Output is correct |
73 |
Correct |
1337 ms |
34964 KB |
Output is correct |
74 |
Correct |
1365 ms |
36272 KB |
Output is correct |
75 |
Correct |
1330 ms |
36032 KB |
Output is correct |
76 |
Correct |
40 ms |
4444 KB |
Output is correct |
77 |
Correct |
1362 ms |
42412 KB |
Output is correct |
78 |
Correct |
1281 ms |
32436 KB |
Output is correct |
79 |
Correct |
1 ms |
2396 KB |
Output is correct |
80 |
Correct |
1 ms |
2396 KB |
Output is correct |