POJ 2104 K-th Number

 主席树模板~~~~

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#define maxn 100005
using namespace std;
int root[maxn],ls[maxn*20],rs[maxn*20],sum[maxn*20];
int tot=0,n,m,a,b,c,num[maxn],hash[maxn];
void build(int left,int right,int fath,int ftype)
{
int rt=++tot;
if (ftype==1) ls[fath]=rt;
else rs[fath]=rt;
sum[rt]=0;
if (left==right) return;
int mid=(left+right)>>1;
build(left,mid,rt,1);
build(mid+1,right,rt,2);
}
void update(int left,int right,int p,int last,int rt,int fath,int ftype)
{
rt=++tot;
if (ftype==1) ls[fath]=rt;
else rs[fath]=rt;
ls[rt]=ls[last];
rs[rt]=rs[last];
sum[rt]=sum[last]+1;
if (left==right) return;
int mid=(left+right)>>1;
if (p<=mid) update(left,mid,p,ls[last],ls[rt],rt,1);
else update(mid+1,right,p,rs[last],rs[rt],rt,2);
}
int query(int left,int right,int c,int last,int rt)
{
if (left==right) return left;
int cnt=sum[ls[rt]]-sum[ls[last]];
int mid=(left+right)>>1;
if (c<=cnt) return query(left,mid,c,ls[last],ls[rt]);
else return query(mid+1,right,c-cnt,rs[last],rs[rt]);
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
hash[i]=num[i];
}
sort(hash+1,hash+n+1);
int cnt=unique(hash+1,hash+n+1)-hash-1;
root[0]=1;
build(1,cnt,0,0);
for (int i=1;i<=n;i++)
num[i]=lower_bound(hash+1,hash+cnt+1,num[i])-hash;
for (int i=1;i<=n;i++)
{
root[i]=tot+1;
update(1,cnt,num[i],root[i-1],root[i],0,0);
}
for (int i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
int ans=query(1,cnt,c,root[a-1],root[b]);
printf("%d\n",hash[ans]);
}
return 0;
}

你可能感兴趣的:(POJ 2104 K-th Number)