题目
https://www.luogu.org/problemnew/show/P3834
解题思路
模板题目, 没有解释(我不会 )。
https://www.luogu.org/problemnew/solution/P3834
代码
#include<cstdio>
#include<algorithm>
#include<cstring>
#define mid ((l+r)>>1)
#define rep(i,x,y) for(register int i=x;i<=y;i++)
using namespace std;
const int N=200010;
int n,q,m,cnt=0,a[N],b[N],t[N],sum[N<<5],L[N<<5],R[N<<5];
inline int build(int l,int r){
int rt=++cnt; sum[rt]=0;
if (l<r) L[rt]=build(l,mid),R[rt]=build(mid+1,r);
return rt;
}
inline int update(int pre,int l,int r,int x){
int rt=++cnt;
L[rt]=L[pre],R[rt]=R[pre],sum[rt]=sum[pre]+1;
if (l<r) {if (x<=mid) L[rt]=update(L[pre],l,mid,x); else R[rt]=update(R[pre],mid+1,r,x);}
return rt;
}
inline int query(int u,int v,int l,int r,int k){
if (l>=r) return l;
int g=sum[L[v]]-sum[L[u]];
return (g>=k)?query(L[u],L[v],l,mid,k):query(R[u],R[v],mid+1,r,k-g);
}
int main(){
scanf("%d%d",&n,&q);
rep(i,1,n) {scanf("%d",&a[i]); b[i]=a[i];}
sort(b+1,b+n+1); m=unique(b+1,b+n+1)-b-1;
t[0]=build(1,m);
rep(i,1,n){
int k=lower_bound(b+1,b+m+1,a[i])-b;
t[i]=update(t[i-1],1,m,k);
}
int x,y,z,g;
while(q--){
scanf("%d%d%d",&x,&y,&z);
g=query(t[x-1],t[y],1,m,z);
printf("%d\n",b[g]);
}
}