#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define sort undefined_function // To use stable_sort instead sort
#define bpc __builtin_popcount
#define ull unsigned long long
#define ld double
#define ll long long
#define mp make_pair
#define F first
#define S second
#pragma GCC optimize("O3")
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> v(n + 1);
for (int i = 1; i <= n; i ++)
cin >> v[i];
vector<pair<pair<int, int>, int>>qr(q + 1);
for (int i = 1; i <= q; i ++) {
cin >> qr[i].F.F >> qr[i].F.S;
qr[i].S = i;
}
stable_sort(qr.begin() + 1, qr.end());
vector<int> ans(q + 1, -1);
int i1 = 0, i2 = 0;
int HALF = n / 2;
vector<int> half1(HALF), half2(HALF);
int iter = 1;
for (int i = 0; iter < q; i ++) {
while (iter < q && qr[iter].F.F == i) {
ans[qr[iter].S] = v[qr[iter].F.S];
iter ++;
}
i1 = i2 = 0;
for (int j = 1; j <= HALF; j ++, i1 ++)
half1[i1] = v[j];
for (int j = HALF + 1; j <= n; j ++, i2 ++)
half2[i2] = v[j];
i1 = i2 = 0;
int tmp = 1;
while (i1 < HALF && i2 < HALF) {
if (half1[i1] < half2[i2]) {
v[tmp] = half1[i1];
i1 ++;
} else {
v[tmp] = half2[i2];
i2 ++;
}
tmp ++;
}
while (i1 < HALF) {
v[tmp] = half1[i1];
i1 ++;
tmp ++;
}
while (i2 < HALF) {
v[tmp] = half2[i2];
i2 ++;
tmp ++;
}
if (is_sorted(v.begin() + 1, v.end()))
break;
}
for (int i = 1; i <= n; i ++)
if (ans[qr[i].S] == -1)
ans[qr[i].S] = v[qr[i].F.S];
for (int i = 1; i <= q; i ++) {
cout << ans[i] << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3033 ms |
27984 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3099 ms |
32056 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3032 ms |
4176 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3033 ms |
27984 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |