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
| #include <cstdio> #include <algorithm> #include <vector> #include <iostream> #include <map> #include <queue> #include <cstring> #include <string> #include <cstdlib> #include <cmath> #include <set> using namespace std; const int inf = (1 << 30); typedef long long ll; const int maxn = 2e5 + 5; int rt[maxn], cnt; struct node { int ls, rs, sum; } tr[maxn * 40]; void inser(int ver, int &now, int L, int R, int pos) { now = ++cnt; tr[now] = tr[ver]; tr[now].sum++; if (L == R) return; int mid = L + R >> 1; if (mid >= pos) inser(tr[ver].ls, tr[now].ls, L, mid, pos); else inser(tr[ver].rs, tr[now].rs, mid + 1, R, pos); } int ask(int ver, int now, int L, int R, int pos) { if (L == R) return L; int mid = L + R >> 1; int sum = tr[tr[now].ls].sum - tr[tr[ver].ls].sum; if (sum >= pos) return ask(tr[ver].ls, tr[now].ls, L, mid, pos); else return ask(tr[ver].rs, tr[now].rs, mid + 1, R, pos - sum); } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, x, q; cin >> n >> q; for (int i = 1; i <= n; i++) cin >> x, inser(rt[i - 1], rt[i], -inf, inf, x); while (q--) { int ql, qr, ik; cin >> ql >> qr >> ik; cout << ask(rt[ql - 1], rt[qr], -inf, inf, ik) << endl; } }
|