1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include <bits/stdc++.h> #define pb push_back const int N = 2e5 + 100; int n, num, m; int a[N]; std::vector<int> xx; void lsh() { xx.reserve(n + 1); xx.pb(-1); for (int i = 1; i <= n; ++i) xx.pb(a[i]); std::sort(xx.begin(), xx.end()); xx.erase(std::unique(xx.begin(), xx.end()), xx.end()); for (int i = 1; i <= n; ++i) a[i] = std::lower_bound(xx.begin(), xx.end(), a[i]) - xx.begin(); num = xx.size(); } namespace CT { const int NN = 5e6 + 100; struct Node{ int lc, rc, dat; } tr[NN]; int rt[N], tot = 0; #define lc(x) tr[(x)].lc #define rc(x) tr[(x)].rc #define dat(x) tr[(x)].dat #define mid ((l + r) >> 1) void bd(int &u, int l = 1, int r = num) { u = ++tot; tr[u] = {0, 0, 0}; if (l < r) bd(lc(u), l, mid), bd(rc(u), mid + 1, r); } void cg(int pos, int d, int &u, int la, int l = 1, int r = num) { u = ++tot; tr[u] = tr[la]; dat(u) += d; if (l == r) return ; (pos <= mid) ? cg(pos, d, lc(u), lc(la), l, mid) : cg(pos, d, rc(u), rc(la), mid + 1, r); } int ask(int ul, int ur, int k, int l = 1, int r = num) { if (l == r) return l; int t = dat(lc(ur)) - dat(lc(ul)); return (t >= k) ? ask(lc(ul), lc(ur), k, l, mid) : ask(rc(ul), rc(ur), k - t, mid + 1, r); } #undef lc #undef rc #undef dat #undef mid } using CT::rt; int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); lsh(), CT::bd(rt[0]); for (int i = 1; i <= n; ++i) CT::cg(a[i], 1, rt[i], rt[i - 1]); for (int l, r, k; m--; ) { scanf("%d %d %d", &l, &r, &k); printf("%d\n", xx[CT::ask(rt[l - 1], rt[r], k)]); } return 0; }
|