#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void shuffle(vector<int> &a) {
int n = a.size();
vector<int> b;
int j = n / 2;
for (int i = 0; i < n / 2; i++) {
while (j < n && a[j] < a[i]) {
b.push_back(a[j++]);
}
b.push_back(a[i]);
}
for (int i = j; i < n; i++) {
b.push_back(a[i]);
}
swap(a, b);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
cin >> N >> Q;
vector<int> A(N);
for (int &x : A) {
cin >> x;
}
if (N <= 1000) {
vector<vector<int>> T;
do {
T.push_back(A);
shuffle(A);
} while (A != T.back());
int M = T.size();
while (Q--) {
int t, i;
cin >> t >> i;
t = min(t, M - 1);
i--;
cout << T[t][i] << "\n";
}
return 0;
}
vector<int> q(Q);
int t;
for (auto &x : q) {
cin >> t >> x;
x--;
}
if (t == 0) {
for (int x : q) {
cout << A[x] << "\n";
}
return 0;
}
vector<int> f(N, N), stk;
for (int i = N - 1; i >= 0; i--) {
while (!stk.empty() && A[stk.back()] < A[i]) {
stk.pop_back();
}
if (!stk.empty()) {
f[i] = stk.back();
}
stk.push_back(i);
}
set<tuple<int, int, int>> s, s2;
for (int i = 0; i < N; i = f[i]) {
s.emplace(A[i], i, f[i]);
}
int T = N;
while (t--) {
while (1) {
auto [_, l, r] = *prev(s.end());
if (T - r + l < N / 2) {
break;
}
T -= r - l;
s2.insert(*prev(s.end()));
s.erase(prev(s.end()));
}
if (T == N / 2) {
break;
}
auto [v, l, r] = *prev(s.end());
s.erase(prev(s.end()));
int m = r - T + N / 2;
s.emplace(v, l, m);
for (int i = m; i < r; i = f[i]) {
s.emplace(A[i], i, min(r, f[i]));
}
}
s.insert(s2.begin(), s2.end());
vector<int> B;
for (auto [_, l, r] : s) {
B.insert(B.end(), A.begin() + l, A.begin() + r);
}
for (int x : q) {
cout << B[x] << "\n";
}
return 6/22;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
164 ms |
8908 KB |
Output is correct |
2 |
Correct |
188 ms |
12552 KB |
Output is correct |
3 |
Correct |
153 ms |
13084 KB |
Output is correct |
4 |
Correct |
148 ms |
10208 KB |
Output is correct |
5 |
Correct |
156 ms |
11976 KB |
Output is correct |
6 |
Correct |
147 ms |
10832 KB |
Output is correct |
7 |
Correct |
159 ms |
12120 KB |
Output is correct |
8 |
Correct |
146 ms |
10692 KB |
Output is correct |
9 |
Correct |
180 ms |
10396 KB |
Output is correct |
10 |
Correct |
156 ms |
10676 KB |
Output is correct |
11 |
Correct |
179 ms |
10684 KB |
Output is correct |
12 |
Correct |
143 ms |
9552 KB |
Output is correct |
13 |
Correct |
181 ms |
10224 KB |
Output is correct |
14 |
Correct |
180 ms |
11228 KB |
Output is correct |
15 |
Correct |
155 ms |
10680 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
150 ms |
9728 KB |
Output is correct |
18 |
Correct |
177 ms |
9604 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
273 ms |
31488 KB |
Output is correct |
2 |
Correct |
253 ms |
33936 KB |
Output is correct |
3 |
Correct |
191 ms |
25836 KB |
Output is correct |
4 |
Correct |
202 ms |
15428 KB |
Output is correct |
5 |
Correct |
174 ms |
16728 KB |
Output is correct |
6 |
Correct |
155 ms |
14508 KB |
Output is correct |
7 |
Correct |
200 ms |
16996 KB |
Output is correct |
8 |
Correct |
182 ms |
16756 KB |
Output is correct |
9 |
Correct |
170 ms |
15876 KB |
Output is correct |
10 |
Correct |
186 ms |
15236 KB |
Output is correct |
11 |
Correct |
159 ms |
14304 KB |
Output is correct |
12 |
Correct |
175 ms |
13976 KB |
Output is correct |
13 |
Correct |
175 ms |
15132 KB |
Output is correct |
14 |
Correct |
184 ms |
14056 KB |
Output is correct |
15 |
Correct |
194 ms |
15432 KB |
Output is correct |
16 |
Correct |
17 ms |
4080 KB |
Output is correct |
17 |
Correct |
151 ms |
14284 KB |
Output is correct |
18 |
Correct |
126 ms |
14012 KB |
Output is correct |
19 |
Correct |
59 ms |
5900 KB |
Output is correct |
20 |
Correct |
54 ms |
9380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
11144 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
164 ms |
8908 KB |
Output is correct |
2 |
Correct |
188 ms |
12552 KB |
Output is correct |
3 |
Correct |
153 ms |
13084 KB |
Output is correct |
4 |
Correct |
148 ms |
10208 KB |
Output is correct |
5 |
Correct |
156 ms |
11976 KB |
Output is correct |
6 |
Correct |
147 ms |
10832 KB |
Output is correct |
7 |
Correct |
159 ms |
12120 KB |
Output is correct |
8 |
Correct |
146 ms |
10692 KB |
Output is correct |
9 |
Correct |
180 ms |
10396 KB |
Output is correct |
10 |
Correct |
156 ms |
10676 KB |
Output is correct |
11 |
Correct |
179 ms |
10684 KB |
Output is correct |
12 |
Correct |
143 ms |
9552 KB |
Output is correct |
13 |
Correct |
181 ms |
10224 KB |
Output is correct |
14 |
Correct |
180 ms |
11228 KB |
Output is correct |
15 |
Correct |
155 ms |
10680 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
150 ms |
9728 KB |
Output is correct |
18 |
Correct |
177 ms |
9604 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
273 ms |
31488 KB |
Output is correct |
22 |
Correct |
253 ms |
33936 KB |
Output is correct |
23 |
Correct |
191 ms |
25836 KB |
Output is correct |
24 |
Correct |
202 ms |
15428 KB |
Output is correct |
25 |
Correct |
174 ms |
16728 KB |
Output is correct |
26 |
Correct |
155 ms |
14508 KB |
Output is correct |
27 |
Correct |
200 ms |
16996 KB |
Output is correct |
28 |
Correct |
182 ms |
16756 KB |
Output is correct |
29 |
Correct |
170 ms |
15876 KB |
Output is correct |
30 |
Correct |
186 ms |
15236 KB |
Output is correct |
31 |
Correct |
159 ms |
14304 KB |
Output is correct |
32 |
Correct |
175 ms |
13976 KB |
Output is correct |
33 |
Correct |
175 ms |
15132 KB |
Output is correct |
34 |
Correct |
184 ms |
14056 KB |
Output is correct |
35 |
Correct |
194 ms |
15432 KB |
Output is correct |
36 |
Correct |
17 ms |
4080 KB |
Output is correct |
37 |
Correct |
151 ms |
14284 KB |
Output is correct |
38 |
Correct |
126 ms |
14012 KB |
Output is correct |
39 |
Correct |
59 ms |
5900 KB |
Output is correct |
40 |
Correct |
54 ms |
9380 KB |
Output is correct |
41 |
Incorrect |
43 ms |
11144 KB |
Output isn't correct |
42 |
Halted |
0 ms |
0 KB |
- |