这就是以后我的可持久化线段树的模板啦~
//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 100010
int n,m,cnt,b[N],cpy[N],now,root[N];
struct Tree{int l,r,cnt;}tree[N*20];
void push_up(int pos){tree[pos].cnt=tree[tree[pos].l].cnt+tree[tree[pos].r].cnt;}
int build(int l,int r){
int pos=now++;
if(l==r)return pos;
int mid=(l+r)>>1;
tree[pos].l=build(l,mid);
tree[pos].r=build(mid+1,r);
push_up(pos);
return pos;
}
int insert(int o,int l,int r,int arr){
int pos=now++;
tree[pos]=tree[o];
if(l==arr&&r==arr){tree[pos].cnt++;return pos;}
int mid=(l+r)>>1;
if(arr<=mid)tree[pos].l=insert(tree[o].l,l,mid,arr);
else tree[pos].r=insert(tree[o].r,mid+1,r,arr);
push_up(pos);
return pos;
}
int query(int l,int r,int o,int v,int kth){
if(l==r)return l;
int mid=(l+r)>>1,res=tree[tree[v].l].cnt-tree[tree[o].l].cnt;
if(kth<=res)return query(l,mid,tree[o].l,tree[v].l,kth);
else return query(mid+1,r,tree[o].r,tree[v].r,kth-res);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&b[i]),cpy[i]=b[i];
sort(cpy+1,cpy+1+n);cnt=unique(cpy+1,cpy+1+n)-cpy-1;
root[0]=build(1,cnt);
for(int i=1;i<=n;i++)
root[i]=insert(root[i-1],1,cnt,lower_bound(cpy+1,cpy+1+cnt,b[i])-cpy);
while(m--){
int xx,yy,zz;scanf("%d%d%d",&xx,&yy,&zz);
printf("%d
",cpy[query(1,cnt,root[xx-1],root[yy],zz)]);
}
}