#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <string>
#include <queue>
#include <unordered_set>
#include <deque>
#include <numeric>
#include <cmath>
#include <unordered_map>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC optimization("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
typedef long long ll;
ll p = 234571, Mod = 2e9 + 11;
ll hash_array(const vector<int>& a) {
ll h = 0;
for (int x : a) {
h = (h * p + x) % Mod;
}
return h;
}
void riffle(vector<int>& a) {
int n = a.size();
vector<int> a1, a2;
for (int i = 0; i < n / 2; ++i) {
a1.push_back(a[i]);
a2.push_back(a[i + n / 2]);
}
int it1 = 0, it2 = 0;
while (it1 < n / 2 && it2 < n / 2) {
if (a1[it1] < a2[it2]) {
a[it1 + it2] = a1[it1];
it1++;
} else {
a[it1 + it2] = a2[it2];
it2++;
}
}
while (it1 < n / 2) {
a[it1 + it2] = a1[it1];
it1++;
}
while (it2 < n / 2) {
a[it1 + it2] = a2[it2];
it2++;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> p(n);
for (int i = 0; i < n; ++i) {
cin >> p[i];
}
vector<vector<int>> save;
save.push_back(p);
unordered_map<ll, int> t;
t[hash_array(p)] = 0;
int cur_t = 1;
int preper = -1, per = -1;
while (true) {
riffle(p);
ll h = hash_array(p);
if (t.count(h)) {
preper = t[h] - 1;
per = cur_t - t[h];
break;
}
save.push_back(p);
t[h] = cur_t;
cur_t++;
}
for (int _ = 0; _ < q; ++_) {
int t, pos;
cin >> t >> pos;
pos--;
if (t <= preper) {
cout << save[t][pos] << '\n';
} else {
t -= preper;
t %= per;
cout << save[preper + 1 + t][pos] << '\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... |