This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
#include <stack>
#define pb push_back
#define ff first
#define ss second
using namespace std;
const int MX = 2e5 + 5;
int n, q, l, r, cur;
int p[MX], ans[MX], fix[MX], nxt[MX];
pair<pair<int, int>, int> t[MX];
set<pair<pair<int, int>, int> > s;
void pre() {
while((*s.rbegin()).ff.ss + r <= n / 2) {
for(int i = 0; i < (*s.rbegin()).ff.ss; i++) {
fix[n - r - (*s.rbegin()).ff.ss + i] = p[(*s.rbegin()).ss + i];
}
r += (*s.rbegin()).ff.ss;
s.erase(*s.rbegin());
}
}
void go() {
if(r == n / 2) return;
pair<pair<int, int>, int> x = *s.rbegin();
s.erase(*s.rbegin());
s.insert({{x.ff.ff, x.ff.ss + r - n / 2}, x.ss});
int ind = x.ss + x.ff.ss + r - n / 2;
while(ind < x.ss + x.ff.ss) {
s.insert({{p[ind], nxt[ind] - ind}, ind});
ind = nxt[ind];
}
pre();
}
int main() {
memset(fix, -1, sizeof(fix));
cin >> n >> q;
stack<pair<int, int > > st;
for(int i = 0; i <= n; i++) {
if (i != n) {
cin >> p[i];
}
else {
p[i] = INT_MAX;
}
while(!st.empty() && st.top().ff < p[i]) {
nxt[st.top().ss] = i;
st.pop();
}
st.push({p[i], i});
}
// while(l < n) {
// s.insert({{p[l], nxt[l] - l}, l});
// l = nxt[l];
// }
// //pre();
// for(int i = 0; i < q; i++) {
// cin >> t[i].ff.ff >> t[i].ff.ss;
// t[i].ff.ss--;
// t[i].ss = i;
// if (t[i].ff.ff > n) t[i].ff.ff = n;
// }
// sort(t, t + q);
// for(int i = 0; i <= n; i++) {
// if(t[cur].ff.ff > i) {
// go();
// continue;
// }
// int cnt = 0;
// auto itr = s.begin();
// while(cur < q && t[cur].ff.ff == i) {
// if(fix[t[cur].ff.ss] != -1) {
// ans[t[cur].ss] = fix[t[cur].ff.ss];
// cur++;
// continue;
// }
// while(itr != s.end() && cnt + (*itr).ff.ss <= t[cur].ff.ss) {
// cnt += (*itr).ff.ss;
// itr++;
// }
// ans[t[cur].ss] = p[(*itr).ss + t[cur].ff.ss - cnt];
// cur++;
// }
// go();
// }
for(int i = 0; i < q; i++) {
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... |