【链接】
poj2104
【题目大意】
给你一个序列,每次询问区间[L,R]中第k大的
【解题报告】
主席树模板题,详见主席树总结。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100005,maxm=2000005;
int n,m,tot,Size,a[maxn],b[maxn];
struct PT
{
PT* son[2];
int l,r,s;
}tem[maxm],*Null=tem,*len=Null,*ro[maxn];
void Pushup(PT* k) {k->s=k->son[0]->s+k->son[1]->s;}
PT* New(int L,int R,int p)
{
++len; len->l=L; len->r=R; len->s=p; len->son[0]=len->son[1]=Null; return len;
}
PT* Build(int L,int R)
{
PT* now=New(L,R,0);
if (L==R) return now;
int mid=(R-L>>1)+L;
now->son[0]=Build(L,mid); now->son[1]=Build(mid+1,R);
Pushup(now); return now;
}
PT* Insert(PT* k,int where)
{
int L=k->l,R=k->r;
PT* now=New(L,R,k->s);
now->son[0]=k->son[0]; now->son[1]=k->son[1];
if (L==R) {now->s++; return now;}
int mid=(R-L>>1)+L;
if (where<=mid) now->son[0]=Insert(now->son[0],where);
else now->son[1]=Insert(now->son[1],where);
Pushup(now); return now;
}
int Query(PT* L,PT* R,int k)
{
if (L->l==L->r) return b[L->l];
int p=R->son[0]->s-L->son[0]->s;
if (p>=k) return Query(L->son[0],R->son[0],k);
else return Query(L->son[1],R->son[1],k-p);
}
inline int Read()
{
int res=0,f=1;
char ch=getchar(),cc=ch;
while (ch<'0'||ch>'9') cc=ch,ch=getchar();
if (cc=='-') f=-1;
while (ch>='0'&&ch<='9') res=res*10+ch-48,ch=getchar();
return res*f;
}
int main()
{
freopen("2104.in","r",stdin);
freopen("2104.out","w",stdout);
n=Read(); m=Read(); tot=0;
for (int i=1; i<=n; i++) a[i]=b[i]=Read();
sort(b+1,b+1+n); Size=unique(b+1,b+1+n)-b-1;
ro[0]=Build(1,Size);
for (int i=1; i<=n; i++) ro[i]=Insert(ro[i-1],lower_bound(b+1,b+1+Size,a[i])-b);
for (int i=1,x,y,k; i<=m; i++) x=Read(),y=Read(),k=Read(),printf("%d\n",Query(ro[x-1],ro[y],k));
return 0;
}