Problem Description
Give you a sequence and ask you the kth big number of a inteval.
Input
The first line is the number of the test cases.
For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere.
The second line contains n integers, describe the sequence.
Each of following m lines contains three integers s, t, k.
[s, t] indicates the interval and k indicates the kth big number in interval [s, t]
Output
For each test case, output m lines. Each line contains the kth big number.
Sample Input
1 10 1 1 4 2 3 5 6 7 8 9 0 1 3 2
Sample Output
2
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
const int maxn=1e5+10;
int sz[maxn],h[maxn];
int T[maxn],L[maxn*20],R[maxn*20],sum[maxn*20];
int tot;
void build(int& rt,int l,int r){
rt=++tot;
//printf("rt:%d l:%d r:%d\n",rt,l,r);
sum[rt]=0;
if(l==r)return;
int mid=(l+r)>>1;
build(L[rt],l,mid);
build(R[rt],mid+1,r);
}
void update(int& rt,int l,int r,int pre,int x){
rt=++tot;
L[rt]=L[pre];
R[rt]=R[pre];
sum[rt]=sum[pre]+1;
if(l==r) return;
int mid=(l+r)>>1;
if(x<=mid)update(L[rt],l,mid,L[pre],x);
else update(R[rt],mid+1,r,R[pre],x);
}
int query(int s,int e,int l,int r,int k){
if(l==r)return l;
int mid=(l+r)>>1;
int res=sum[L[e]]-sum[L[s]];
if(k<=res)return query(L[s],L[e],l,mid,k);
else return query(R[s],R[e],mid+1,r,k-res);
}
int main(){
int Kase;
scanf("%d",&Kase);
while(Kase--){
int n,q;
scanf("%d %d",&n,&q);
rep(i,1,n+1) scanf("%d",&sz[i]),h[i]=sz[i];
sort(h+1,h+n+1);
int num=unique(h+1,h+n+1)-h-1;
tot=0;
build(T[0],1,num);
rep(i,1,n+1) update(T[i],1,num,T[i-1],lower_bound(h+1,h+num+1,sz[i])-h);
while(q--){
int ql,qr,k;
scanf("%d %d %d",&ql,&qr,&k);
int ans=query(T[ql-1],T[qr],1,num,k);
printf("%d\n",h[ans]);
}
}
return 0;
}