This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
typedef long long ll;
typedef std::pair<int,int> pii;
using namespace std;
vector<int> shf(vector<int> vec)
{
int n = vec.size();
vector<int> ans;
int p0 = 0, p1 = n/2;
Loop (_,0,n) {
if (p1 == n || (p0 < n/2 && vec[p0] < vec[p1]))
ans.push_back(vec[p0++]);
else
ans.push_back(vec[p1++]);
}
return ans;
}
const int N = 1'000'010;
int ans[N];
vector<pii> Q[N];
int main()
{
cin.tie(0) -> sync_with_stdio(false);
int n, q;
cin >> n >> q;
vector<int> vec(n);
//iota(vec.begin(), vec.end(), 0);
//auto pleh = vec;
//int cntt = 0;
//for (;;) {
// int cnt = 0;
// vec = pleh;
// for (;;) {
// auto tmp = shf(vec);
// if (tmp == vec)
// break;
// vec = tmp;
// ++cnt;
// }
// cntt = max(cntt, cnt);
// if (!next_permutation(pleh.begin(), pleh.end()))
// break;
//}
//cout << cntt << '\n';
//return 0;
//mt19937_64 rd(chrono::high_resolution_clock::now().time_since_epoch().count());
//iota(vec.begin(), vec.end(), 0);
//shuffle(vec.begin(), vec.end(), rd);
for (int &x : vec)
cin >> x;
//vecs.push_back(vec);
Loop (i,0,q) {
int t, p;
cin >> t >> p;
--p;
t = min(t, n);
Q[t].push_back({p, i});
}
int p0 = 0, p1 = n;
vector<int> vec2(n);
Loop (i,0,n) {
for (auto [p, j] : Q[i])
ans[j] = vec[p];
merge(vec.data()+p0, vec.data()+n/2, vec.data()+n/2, vec.data()+p1, vec2.data()+p0);//vec = shf(vec);
vec.swap(vec2);
//while (p0 < n/2 && vec[p0] == vec2[p0])
// ++p0;
//while (n/2 < p1 && vec[p1-1] == vec2[p1-1])
// --p1;
//vec = shf(vec);
}
for (auto [p, j] : Q[n])
ans[j] = vec[p];
Loop (i,0,q)
cout << ans[i] << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |