题意:给一个数列然后有多次询问,问l到r中第K大的数
思路:模版题,模版来自点击打开链接 PS:数独不如划分树快,但是用途比划分树要广
#include <vector>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fll;
const int maxn=100010;
int T[maxn*30],lson[maxn*30],rson[maxn*30],c[maxn*30],a[maxn],t[maxn];
int n,m,q,tot;
void init_hash(){
for(int i=1;i<=n;i++) t[i]=a[i];
sort(t+1,t+1+n);
m=unique(t+1,t+1+n)-t-1;
}
int buildtree(int le,int ri){
int root=tot++;
c[root]=0;
if(le!=ri){
int mid=(le+ri)>>1;
lson[root]=buildtree(le,mid);
rson[root]=buildtree(mid+1,ri);
}
return root;
}
int update(int root,int pos,int val){
int newroot=tot++,tmp=newroot;
c[newroot]=c[root]+val;
int l=1,r=m;
while(l<r){
int mid=(l+r)>>1;
if(pos<=mid){
lson[newroot]=tot++;rson[newroot]=rson[root];
newroot=lson[newroot];root=lson[root];
r=mid;
}else{
rson[newroot]=tot++;lson[newroot]=lson[root];
newroot=rson[newroot];root=rson[root];
l=mid+1;
}
c[newroot]=c[root]+val;
}
return tmp;
}
int query(int le,int ri,int k){
int l=1,r=m;
while(l<r){
int mid=(l+r)>>1;
if(c[lson[le]]-c[lson[ri]]>=k){
r=mid;
le=lson[le];ri=lson[ri];
}else{
l=mid+1;
k-=c[lson[le]]-c[lson[ri]];
le=rson[le];ri=rson[ri];
}
}
return l;
}
int main(){
int u,v,k;
while(scanf("%d%d",&n,&q)!=-1){
tot=0;
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
init_hash();
T[n+1]=buildtree(1,m);
for(int i=n;i>=1;i--){
int pos=lower_bound(t+1,t+1+m,a[i])-t;
T[i]=update(T[i+1],pos,1);
}
while(q--){
scanf("%d%d%d",&u,&v,&k);
printf("%d\n",t[query(T[u],T[v+1],k)]);
}
}
return 0;
}