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
| struct Seg_Tree { int cnt, n; int rt[N], sum[N<<5], ls[N<<5], rs[N<<5]; inline void push_up(int suc) { sum[suc] = sum[ls[suc]] + sum[rs[suc]]; } void build(int suc, int l, int r) { sum[suc] = 0; if (l >= r) return; int mid = (l + r) >> 1; build(ls[suc] = ++cnt, l, mid), build(rs[suc] = ++cnt, mid+1, r); push_up(suc); } void update(int suc, int pre, int l, int r, int x) { ls[suc] = ls[pre], rs[suc] = rs[pre], sum[suc] = sum[pre] + 1; if (l >= r) return; int mid = (l + r) >> 1; if (x <= mid) update(ls[suc] = ++cnt, ls[pre], l, mid, x); else update(rs[suc] = ++cnt, rs[pre], mid+1, r, x); } int query(int u, int v, int l, int r, int k) { if (l >= r) return l; int t = sum[ls[v]] - sum[ls[u]], mid = (l + r) >> 1; if (k <= t) return query(ls[u], ls[v], l, mid, k); else return query(rs[u], rs[v], mid+1, r, k - t); } } tr;
inline void main() { read(n), read(m); for (int i = 1; i <= n; ++i) read(a[i]), b[i] = a[i]; sort(b+1, b+n+1); tr.n = unique(b+1, b+n+1) - b - 1; tr.build(tr.rt[0] = ++tr.cnt, 1, tr.n); for (int i = 1; i <= n; ++i) { int x = lower_bound(b+1, b+tr.n+1, a[i]) - b; tr.update(tr.rt[i] = ++tr.cnt, tr.rt[i-1], 1, tr.n, x); } int l, r, k; while (m--) { read(l), read(r), read(k); printf("%d\n", b[tr.query(tr.rt[l-1], tr.rt[r], 1, tr.n, k)]); } }
|