#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 100010
int n,m;
int S,T,K;
int seq[maxn],sortseq[25][maxn];
int depth;
struct Tree{
int l,r;
}tree[maxn*3];
void setTree(int p,int l,int r,int depth)
{
tree[p].l=l;tree[p].r=r;
if(l==r){
sortseq[depth][l]=seq[l];
return ;
}
int mid=(l+r)>>1;
setTree(p*2,l,mid,depth+1);
setTree(p*2+1,mid+1,r,depth+1);
int i,j,k;
k=i=l;j=mid+1;
while(i<=mid &&j<=r){
if(sortseq[depth+1][i]<sortseq[depth+1][j])
sortseq[depth][k++]=sortseq[depth+1][i++];
else sortseq[depth][k++]=sortseq[depth+1][j++];
}
while(i<=mid)
sortseq[depth][k++]=sortseq[depth+1][i++];
while(j<=r)
sortseq[depth][k++]=sortseq[depth+1][j++];
}
int query(int p,int val,int depth)
{//printf("come into query(%d , %d , %d) and zuoerzi %d , youerzi %d\n",p,val,depth,tree[p].l,tree[p].r);
if(S<=tree[p].l && tree[p].r<=T)
//对有序数列二分查找,返回地址
// {cout<<"comehrer"<
// cout<<"return is "<
return lower_bound(&sortseq[depth][tree[p].l], &sortseq[depth][tree[p].r]+1,val)-&sortseq[depth][tree[p].l];
//}
int res=0;
if(S<=tree[p*2].r)
res+=query(p*2,val,depth+1);
if(T>=tree[p*2+1].l)
res+=query(p*2+1,val,depth+1);
// cout<<"res fanhui"<
return res;
}
int main()
{
int i,l,r,mid,key,kk;
while(scanf("%d%d",&n,&m)==2)
{
for(i=1;i<=n;i++)
scanf("%d",&seq[i]);
setTree(1,1,n,1);
for(int i=1;i<=5;++i){for(int j=0;j<=10;++j)cout<<sortseq[i][j]<<' ';cout<<endl;}
while(m--){
scanf("%d%d%d",&S,&T,&K);
K--;
l=1;r=n;
while(l<=r){
mid=(l+r)>>1;
kk=query(1,sortseq[1][mid],1);
if(kk<=K){
key=mid;
l=mid+1;
}
else
r=mid-1;
}
printf("%d\n",sortseq[1][key]);
}
}
return 0;
}
|