이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// #pragma GCC optimize("O3,Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int i = 0; (i) < (n); (i)++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const double EPS = 0.00001;
const int INF = 1e9+500;
const int N = 3e5+5;
const int ALPH = 26;
const int LGN = 25;
constexpr int MOD = 1e9+7;
int n,m,q;
struct SegT {
vector<int> data;
int sz;
void reset(int s) {
sz = s;
data.assign(4*(sz + 3), 0);
}
void update(int tl, int tr, int v, int ind, int val) {
if(tl == tr) {
data[v] += val;
return;
}
int tm = (tl + tr) >> 1;
if(ind <= tm) {
update(tl, tm, v << 1, ind, val);
}
else {
update(tm + 1, tr, v << 1 ^ 1, ind, val);
}
data[v] = data[v << 1] + data[v << 1 ^ 1];
}
void update(int ind, int val) {
update(0, sz, 1, ind, val);
}
int query(int tl, int tr, int v, int val) {
if(tl == tr) {
return tl;
}
int tm = (tl + tr) >> 1;
if(val <= data[v << 1]) {
return query(tl, tm, v << 1, val);
}
else {
return query(tm + 1, tr, v << 1 ^ 1, val - data[v << 1]);
}
}
int query(int val) {
return query(0, sz, 1, val);
}
};
vector<int> a, nxt;
vector<array<int,3> > comp;
set<array<int,3> > st;
vector<int> fin;
vector<array<int,3> > qu;
int tot = 0;
void calcn() {
a[n + 1] = INF;
vector<array<int,2> > tmp;
for(int i = 1; i<=n + 1; i++) {
while(tmp.size() && tmp.back()[0] < a[i]) {
nxt[tmp.back()[1]] = i;
tmp.pop_back();
}
tmp.pb({a[i], i});
}
}
int itr;
int t = 0;
void get_fin(array<int,3> bl) {
// cout << bl[0] << " " << bl[1] << " " << bl[2] << endl;
for(int i = bl[2]; i >= bl[1]; i--) {
fin[itr--] = a[i];
}
}
void prt() {
for(auto c : st) {
cout << c[0] << " " << c[1] << " " << c[2] << "\n";
}
}
void get_comp() {
if(qu[0][0] == 0) return; // TODO
for(int l = 1; l <= n / 2;) {
int r = min(n / 2, nxt[l] - 1);
st.insert({a[l], l, r});
comp.pb({a[l], l, r});
tot += r - l + 1;
l = nxt[l];
}
for(int l = n / 2 + 1; l <= n;) {
int r = nxt[l] - 1;
st.insert({a[l], l, r});
comp.pb({a[l], l, r});
tot += r - l + 1;
l = nxt[l];
}
t++;
bool f1 = 1;
while(f1) {
// prt();
// cout << "round" << t << endl;
// cout << "tot:" << tot << "\n";
if(st.empty()) break;
if(t == qu[0][0]) break;
auto last = st.end(); last--;
while(tot - ((*last)[2] - (*last)[1] + 1) >= n / 2) {
tot -= ((*last)[2] - (*last)[1] + 1);
get_fin(*last); // TODO
st.erase(last);
assert(st.size() != 0);
last = st.end(); last--;
}
auto el = *last;
if(tot == n / 2) break;
int len = el[2] - el[1] + 1;
tot -= ((*last)[2] - (*last)[1] + 1);
st.erase(last);
int ext = tot + len - n / 2;
st.insert({el[0], el[1], el[2] - ext});
comp.pb({el[0], el[1], el[2] - ext});
tot += el[2] - ext - el[1] + 1;
for(int l = el[2] - ext + 1; l <= el[2];) {
int r = min(el[2], nxt[l] - 1);
st.insert({a[l], l, r});
tot += r - l + 1;
comp.pb({a[l], l, r});
l = nxt[l];
}
t++;
}
while(st.size()) { // TODO
auto x = prev(st.end());
get_fin(*x);
st.erase(x);
}
sort(all(comp));
}
inline void solve() {
cin>>n>>q;
a.resize(n + 2);
nxt.resize(n + 1);
fin.assign(n + 1, 0);
qu.resize(q);
itr = n;
for(int i = 1; i<=n; i++) {
cin >> a[i];
}
REP(i, q) {
cin >> qu[i][0] >> qu[i][1];
qu[i][2] = i;
}
// sort(all(qu));
calcn();
get_comp();
for(int i = 0; i < q; i++) {
if(qu[i][0] == 0) cout << a[qu[i][1]] << "\n";
else {
cout << fin[qu[i][1]] << "\n";
}
}
}
signed main() {
fastio();
int test = 1;
//cin>>test;
while(test--) {
solve();
}
}
# | 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... |