#include<bits/stdc++.h>
using namespace std;
int N, Q;
int cur;
vector<int> A, nxtg, subans;
vector<pair<int, int>> q;
set<array<int, 3>> S;// original segment {L, R};
void shuffle_go() {
if(cur == N / 2) {
return;
}
auto x = end(S); --x;
while(x != begin(S) && cur - ((*x)[2] - (*x)[1] + 1) >= N / 2) {
cur -= ((*x)[2] - (*x)[1] + 1);
for(int i = (*x)[2]; i >= (*x)[1]; --i) {
subans.push_back(A[i]);
}
x = S.erase(x); --x;
}
if(cur == N / 2) {
return;
}
array<int, 3> mv = *x;
S.erase(x);
int w = mv[2] - mv[1] + 1;
int trsh = cur - N / 2;
S.insert({mv[0], mv[1], mv[2] - trsh});
S.insert({A[mv[2] - trsh + 1], mv[2] - trsh + 1, mv[2]});
}
int32_t main() {
cin >> N >> Q;
cur = N;
A.resize(N); for(int& u : A) cin >> u;
q.resize(Q); for(auto& u : q) { cin >> u.first >> u.second; u.first = min(u.first, N / 2); }
{
nxtg.resize(N, N);
vector<int> st;
for(int i = N - 1; i >= 0; --i) {
while(!st.empty() && A[st.back()] < A[i]) st.pop_back();
if(!st.empty()) nxtg[i] = st.back();
st.push_back(i);
}
}
if(q[0].first == 0) {
for(auto& u : q) {
cout << A[u.second - 1] << '\n';
}
return 0;
}
//first change manually
for(int i = 0; i < N / 2; ++i) {
int j = min(nxtg[i] - 1, N / 2 - 1);
S.insert({A[i], i, j});
i = j;
}
for(int i = N / 2; i < N; ++i) {
int j = nxtg[i] - 1;
S.insert({A[i], i, j});
i = j;
}
for(int i = 1; i < q[0].first; ++i) {
shuffle_go();
}
vector<int> wtf;
for(auto& u : S) {
for(int i = u[1]; i <= u[2]; ++i) {
wtf.push_back(A[i]);
}
}
reverse(begin(subans), end(subans));
for(auto& u : subans) {
// cout << u << '\n';
wtf.push_back(u);
}
for(auto& u : q) {
// cout << u << ' ';
cout << wtf[u.second - 1] << '\n';
}
return 0;
}
Compilation message
Main.cpp: In function 'void shuffle_go()':
Main.cpp:27:6: warning: unused variable 'w' [-Wunused-variable]
27 | int w = mv[2] - mv[1] + 1;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
363 ms |
11980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
523 ms |
17460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
70 ms |
3128 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
363 ms |
11980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |