附上主席树代码, 以示我完全理解了主席树:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <ctime>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const double eps = 1e-6;
const double PI = acos(-1);
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
// & 0x7FFFFFFF
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 100000 * 50;
int T,n,m,tot;
struct node {
int l, r, sum;
}tree[maxn];
int build(int l, int r) {
int root = ++tot;
if(l == r) return root;
int mid = (l+r)>>1;
tree[root].sum = 0;
tree[root].l = build(l, mid);
tree[root].r = build(mid+1, r);
return root;
}
int update(int l, int r, int root, int pos) {
int newroot = ++tot;
tree[newroot] = tree[root];
tree[newroot].sum++;
if(l == r) return newroot;
int mid = (l+r)>>1;
if(pos <= mid) tree[newroot].l = update(l, mid, tree[root].l, pos);
else tree[newroot].r = update(mid+1, r, tree[root].r, pos);
return newroot;
}
int query(int l, int r, int x, int y, int pos) {
if(l == r) return l;
int mid = (l + r) >> 1;
int sum = tree[tree[y].l].sum - tree[tree[x].l].sum;
if(sum >= pos) return query(l, mid, tree[x].l, tree[y].l, pos);
else return query(mid+1, r, tree[x].r, tree[y].r, pos - sum);
}
vector<int> v;
int getid(int x) { return lower_bound(v.begin(), v.end(), x) - v.begin() + 1; }
int a[maxn], rt[maxn];
int main() {
while(~scanf("%d%d",&n,&m)) {
v.clear();
for(int i = 1; i <= n; i++) scanf("%d",&a[i]), v.push_back(a[i]);
sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());
rt[0] = build(1, n);
for(int i = 1; i <= n; i++) {
rt[i] = update(1, n, rt[i-1], getid(a[i]));
}
while(m--) {
int x, y, k; scanf("%d%d%d",&x,&y,&k);
printf("%d\n", v[query(1, n, rt[x-1], rt[y], k)-1]);
}
}
return 0;
}