首页 > 其他分享 >HDU 2665 Kth number

HDU 2665 Kth number

时间:2022-11-09 20:40:33浏览次数:41  
标签:HDU int frist sum number tot 2665 Kth maxn


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<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100005;
int n,m,T,a[maxn],b[maxn],c[maxn];
int L[maxn*20],R[maxn*20],sum[maxn*20],tot,frist[maxn];

bool cmp(const int &x,const int &y)
{
return a[x]<a[y];
}

void insert(int now,int l,int r,int u)
{
sum[++tot]=sum[now]+1;
if (l==r) L[tot]=R[tot]=0;
else
{
int mid=(l+r)>>1;
L[tot]=L[now]; R[tot]=R[now];
if (u<=mid) L[tot]=tot+1; else R[tot]=tot+1;
if (u<=mid) insert(L[now],l,mid,u);
else insert(R[now],mid+1,r,u);
}
}

int query(int u,int v,int l,int r,int k)
{
if (l==r) return a[b[l]];
else
{
int mid=(l+r)>>1;
if (sum[L[u]]-sum[L[v]]<k)
return query(R[u],R[v],mid+1,r,k-(sum[L[u]]-sum[L[v]]));
else
return query(L[u],L[v],l,mid,k);
}
}

int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) scanf("%d",&a[b[i]=i]);
sort(b+1,b+n+1,cmp);
for (int i=1;i<=n;i++) c[b[i]]=i;
memset(frist,0,sizeof(frist));
frist[0]=L[0]=R[0]=sum[0]=tot=0;
for (int i=1;i<=n;i++)
{
frist[i]=tot+1;
insert(frist[i-1],1,n,c[i]);
}
while (m--)
{
int x,y,k;
scanf("%d%d%d",&x,&y,&k);
printf("%d\n",query(frist[y],frist[x-1],1,n,k));
}
}
return 0;
}



标签:HDU,int,frist,sum,number,tot,2665,Kth,maxn
From: https://blog.51cto.com/u_15870896/5838718

相关文章

  • HDU 2715 Herd Sums
    DescriptionThecowsinfarmerJohn'sherdarenumberedandbrandedwithconsecutiveintegersfrom1toN(1<=N<=10,000,000).Whenthecowscometot......
  • HDU 5339 Untitled
    ProblemDescriptiona and n integers b1,…,bn.Afterselectingsomenumbersfrom b1,…,bn inanyorder,say c1,…,cr,wewanttom......
  • HDU 3760 Ideal Path
    ProblemDescriptionNewlabyrinthattractionisopeninNewLostlandamusementpark.Thelabyrinthconsistsofnroomsconnectedbympassages.Eachpass......
  • HDU 4101 Ali and Baba
    ProblemDescriptionThereisarectanglearea(withNrowsandMcolumns)infrontofAliandBaba,eachgridmightbeoneofthefollowing:1.Empty......
  • HDU 4198 Quick out of the Harbour
    ProblemDescriptionCaptainClearbearddecidedtogototheharbourforafewdayssohiscrewcouldinspectandrepairtheship.Now,afewdayslater,......
  • HDU 2589 正方形划分
    ProblemDescription一个边长为L的正方形可以分成L*L个小正方形.有N个石子放在N个小正方形里,能否将它分成N个正方形,使得每个正方形里恰有一个石子且这N个正方......
  • HDU 3085 Nightmare Ⅱ
    ProblemDescriptionLastnight,littleerriyuehadahorriblenightmare.Hedreamedthatheandhisgirlfriendweretrappedinabigmazeseparately.Mo......
  • HDU 3313 Key Vertex
    ProblemDescriptionYouneedwalkingfromvertexStovertexTinagraph.IfyouremoveonevertexwhichstopsyoufromwalkingfromStoT,thatverte......
  • HDU 3316 Mine sweeping
    ProblemDescriptionIthinkmostofyouareusingsystemnamedofxporvistaorwin7.Andthesesystemisconsistofafamousgamewhatisminesweeping......
  • HDU 3355 Hop — Don’t Walk!
    ProblemDescriptionKERMITTHEFROGisaclassicvideogamewithasimplecontrolandobjectivebutrequiresagooddealofthinking.Youcontrolanani......