이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// Arayi
#include <bits/stdc++.h>
#define fr first
#define sc second
#define MP make_pair
#define ad push_back
#define PB push_back
#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(0);                       \
    cout.tie(0);
#define lli long long int
#define y1 arayikhalatyan
#define j1 jigglypuff
#define ld long double
#define itn int
#define pir pair<int, int>
#define all(x) (x).begin(), (x).end()
#define str string
#define enl endl
#define en endl
#define cd complex<long double>
#define vcd vector<cd>
#define vii vector<int>
#define vlli vector<lli>
using namespace std;
lli gcd(lli a, lli b) { return (b == 0LL ? a : gcd(b, a % b)); }
ld dist(ld x, ld y1, ld x2, ld y2)
{
    return sqrt((x - x2) * (x - x2) + (y1 - y2) * (y1 - y2));
}
lli S(lli a)
{
    return (a * (a + 1LL)) / 2;
}
mt19937 rnd(363542);
char vow[] = {'a', 'e', 'i', 'o', 'u'};
int dx[] = {0, -1, 0, 1, -1, -1, 1, 1, 0};
int dy[] = {-1, 0, 1, 0, -1, 1, -1, 1, 0};
char dc[] = {'R', 'D', 'L', 'U'};
const int N = 1e6 + 20;
const lli mod = 1e9 + 7;
const ld pi = acos(-1);
const ld e = 1e-13;
const int T = 200;
lli bp(lli a, lli b = mod - 2LL)
{
    lli ret = 1;
    while (b)
    {
        if (b & 1)
            ret *= a, ret %= mod;
        a *= a;
        a %= mod;
        b >>= 1;
    }
    return ret;
}
ostream &operator<<(ostream &c, pir a)
{
    c << a.fr << " " << a.sc;
    return c;
}
template <class T>
void maxi(T &a, T b)
{
    a = max(a, b);
}
template <class T>
void mini(T &a, T b)
{
    a = min(a, b);
}
int n, q;
int a[N], ind[N], hj[N], pat[N];
pair<pir, int> b[N];
int t[4 * N];
void upd(int q, int v, int l = 0, int r = n, int nd = 1)
{
    if (q > r || q < l)
        return;
    if (l == r)
    {
        t[nd] += v;
        return;
    }
    int md = (l + r) / 2;
    upd(q, v, l, md, nd * 2);
    upd(q, v, md + 1, r, nd * 2 + 1);
    t[nd] = t[nd * 2] + t[nd * 2 + 1];
}
int qry(int l, int r, int nl = 0, int nr = n, int nd = 1)
{
    if (l > nr || r < nl)
        return 0;
    if (nl == l && r == nr)
        return t[nd];
    int md = (nl + nr) / 2;
    return qry(l, min(md, r), nl, md, nd * 2) + qry(max(md + 1, l), r, md + 1, nr, nd * 2 + 1);
}
int qr(int v, int nl = 0, int nr = n, int nd = 1)
{
    if (nl == nr)
        return nl;
    int md = (nl + nr) / 2;
    if (t[nd * 2] > v)
        return qr(v, nl, md, nd * 2);
    else
        return qr(v - t[nd * 2], md + 1, nr, nd * 2 + 1);
}
int main()
{
    fastio;
    cin >> n >> q;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        ind[a[i]] = i;
    }
    for (int i = 0; i < q; i++)
    {
        cin >> b[i].fr.fr >> b[i].fr.sc;
        b[i].fr.sc--;
        b[i].sc = i;
    }
    sort(b, b + q);
    stack<pair<int, int>> st;
    st.push(MP(mod, n));
    for (int i = n - 1; i >= 0; i--)
    {
        while (a[i] > st.top().fr)
            st.pop();
        hj[i] = st.top().sc;
        st.push(MP(a[i], i));
    }
    int i1 = 0;
    while (i1 < n)
    {
        if (i1 < n / 2 && hj[i1] >= n / 2)
        {
            upd(a[i1], n / 2 - i1);
            // cout << a[i1] << " " << n / 2 - i1 << endl;
            i1 = n / 2;
        }
        else
        {
            upd(a[i1], hj[i1] - i1);
            // cout << a[i1] << " " << hj[i1] - i1 << endl;
            i1 = hj[i1];
        }
    }
    i1 = 0;
    int t1 = 0;
    while (i1 < q && b[i1].fr.fr == t1)
    {
        pat[b[i1].sc] = a[b[i1].fr.sc];
        i1++;
    }
    t1++;
    while (i1 < q)
    {
        while (i1 < q && b[i1].fr.fr == t1)
        {
            int ans = qr(b[i1].fr.sc);
            // cout << b[i1].fr.sc << " " << ans << endl;
            int sm = b[i1].fr.sc - qry(0, ans - 1);
            pat[b[i1].sc] = a[ind[ans] + sm];
            i1++;
        }
        int ans = qr(n / 2);
        if (qry(0, ans - 1) == n / 2)
        {
            while (i1 < q)
            {
                int ans = qr(b[i1].fr.sc);
                // cout << b[i1].fr.sc << " " << ans << endl;
                int sm = b[i1].fr.sc - qry(0, ans - 1);
                pat[b[i1].sc] = a[ind[ans] + sm];
                i1++;
            }
        }
        else
        {
            int sm = n / 2 - qry(0, ans - 1);
            int qn = qry(ans, ans);
            upd(ans, sm - qn);
            int i1 = ind[ans] + sm;
            while (i1 < ind[ans] + qn)
            {
                upd(a[i1], min(hj[i1], ind[ans] + qn) - i1);
                i1 = hj[i1];
            }
        }
        t1++;
    }
    for (int i = 0; i < q; i++)
        cout << pat[i] << "\n";
    return 0;
}
/*
    __
  *(><)*
    \/ /
    ||/
  --||
    ||
    /\
   /  \
  /    \
*/
| # | 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... |