Submission #1153950

#TimeUsernameProblemLanguageResultExecution timeMemory
1153950itslqIndex (COCI21_index)C++20
60 / 110
2598 ms51052 KiB
#include <bits/stdc++.h>
using namespace std;

const int MAX = 2e5 + 10;
vector<int> P(MAX);

struct node {
    int l, r, m;
    vector<int> v;
    node *lc, *rc;

    node(int _l, int _r): l(_l), r(_r), m((_l + _r) >> 1) {
        if (l == r) {
            v.push_back(P[l]);
        } else {
            lc = new node(l, m);
            rc = new node(m + 1, r);

            v.resize(lc -> v.size() + rc -> v.size());
            auto lv = lc -> v.begin(), rv = rc -> v.begin(), it = v.begin();
            
            while (lv != lc -> v.end() && rv != rc -> v.end()) *(it++) = *((*lv < *rv ? lv : rv)++);
            while (lv != lc -> v.end()) *(it++) = *(lv++);
            while (rv != rc -> v.end()) *(it++) = *(rv++);
        }
    }

    int query(int _l, int _r, int _v) {
        if (l == _l && r == _r) return (int) (v.end() - lower_bound(v.begin(), v.end(), _v));
        if (_r <= m) return lc -> query(_l, _r, _v);
        if (_l > m) return rc -> query(_l, _r, _v);
        return lc -> query(_l, m, _v) + rc -> query(m + 1, _r, _v);
    }

} *root;

int main() {
    int N, Q, L, R, l, r, m, ans;
    cin >> N >> Q;
    for (int i = 1; i <= N; i++) cin >> P[i];

    root = new node(1, N);

    while (Q--) {
        cin >> L >> R;

        l = 1, r = R - L + 1;
        while (l <= r) {
            m = (l + r) >> 1;
            if (root -> query(L, R, m) >= m) {
                ans = m;
                l = ++m;
            } else {
                r = --m;
            }
        }

        cout << ans << '\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...