/*
http://hi.baidu.com/wyl8899/item/e00796a9cb2df73d020a4d68
*/
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
const int MAX_N=100005;
const int MAX_M=5005;
const int MAX=MAX_M*600;
int n,m;
int len;
int a[MAX_N];
int b[MAX_N];
int tot;
int L[MAX];
int R[MAX];
int Left[MAX];
int Right[MAX];
int sum[MAX];
int root[MAX];
int pos[MAX_N];
int build(int l,int r){
int id = tot++;
L[id]=R[id]=-1;
Left[id]=l;
Right[id]=r;
sum[id]=0;
if(l==r)return id;
int mid=(l+r)>>1;
L[id]=build(l,mid);
R[id]=build(mid+1,r);
return id;
}
void initTree(){
tot=0;
root[0]=build(0,len-1);
}
int insertTree(int rt,int p){
//printf("rt==%d p==%d\n",rt,p);
int id = tot++;
sum[id]=sum[rt]+1;
L[id]=L[rt];
R[id]=R[rt];
Left[id]=Left[rt];
Right[id]=Right[rt];
if(Left[id]==p && Right[id]==p)return id;
int mid=(Left[id]+Right[id])>>1;
if(p<=mid) L[id]=insertTree(L[rt],p);
else R[id]=insertTree(R[rt],p);
return id;
}
int queryTree(int y,int x,int z){
if(L[x]==-1 && R[x]==-1){
return Left[x];
}
if(sum[L[y]]-sum[L[x]]>=z) return queryTree(L[y],L[x],z);
else return queryTree(R[y],R[x],z-(sum[L[y]]-sum[L[x]]));
}
int main(){
int x,y,z;
int i,j,k;
while(scanf("%d%d",&n,&m)!=EOF){
for(i=0;i<n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b,b+n);
len=unique(b,b+n)-b;
for(i=0;i<n;i++){
pos[i]=lower_bound(b,b+n,a[i])-b;
// printf("pos==%d len==%d\n",pos[i],len);
}
initTree();
for(i=0;i<n;i++) {
root[i+1]=insertTree(root[i],pos[i]);
//printf("tmp");
}
for(i=0;i<m;i++){
scanf("%d%d%d",&x,&y,&z);
int id=queryTree(root[y],root[x-1],z);
printf("%d\n",b[id]);
}
}
return 0;
}
[POJ 2104]K-th Number (不带修改的区间k大,函数式线段树)
最新推荐文章于 2023-10-06 16:47:07 发布