P3834 【模板】可持久化线段树 2

P3834主席树模板 , 求区间第k小 。
【P3834 【模板】可持久化线段树 2】 1 #include <bits/stdc++.h> 2 using namespace std; 3 #define lc tr[i].ch[0] 4 #define rc tr[i].ch[1] 5 #define Lc tr[j].ch[0] 6 #define Rc tr[j].ch[1] 7 #define mid ((l + r) >> 1) 8 const int N = 2e5 + 10; 9 int n, m, a[N], b[N];10 int cnt, rt[N];11 struct node {12int num, ch[2];13 }tr[N * 20];14 void update(int &i, int j, int l, int r, int k) {15i = ++cnt;//编号16tr[i] = tr[j];//复制之前版本17++ tr[i].num;18if (l == r) return ;19if (k <= mid) update(lc, Lc, l, mid, k);20else update(rc, Rc, mid + 1, r, k);21 }22 int query(int i, int j, int l, int r, int k) {23if (l == r) return l;//返回下标24int s = tr[Lc].num - tr[lc].num;25if (k <= s) return query(lc, Lc, l, mid, k);26else return query(rc, Rc, mid + 1, r, k - s);27 }28 int main() {29scanf("%d %d", &n, &m);30for (int i = 1; i <= n; i ++) {31scanf("%d", &a[i]);32b[i] = a[i];33}34sort(b + 1, b + n + 1);35int tot = unique(b + 1, b + n + 1) - b - 1;36cnt = 0, rt[0] = 0;37for (int i = 1; i <= n; i ++)38update(rt[i], rt[i - 1], 1, tot, lower_bound(b + 1, b + tot + 1, a[i]) - b);39int x, y, k;40while (m --) {41scanf("%d %d %d", &x, &y, &k);42printf("%d\n", b[query(rt[x - 1], rt[y], 1, tot, k)]);43}44return 0;45 }

    推荐阅读