HDU2665-Kth number

来源:互联网 发布:饥荒数据修改 编辑:程序博客网 时间:2024/06/07 04:36

Kth number

                                                                      Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                                                                    Total Submission(s): 9899    Accepted Submission(s): 3070


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
 

Source
HDU男生专场公开赛——赶在女生之前先过节(From WHU)

题意:给出n个数,有m个询问,求区间内第k大的数是哪个

解题思路:由于题目未说明n个数的范围,因此可以先做一个预处理,将n个数排序后重新编号,然后建一棵可持久化线段树来维护


#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#include <stack>#include <vector>#include <set>using namespace std;#define LL long longint n,m,k;int a[100009],x[100009];int s[100009],L[100009*20],R[100009*20],sum[100009*20];int tot;void build(int now,int l,int r,int p){    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(mid>=p) L[tot]=tot+1;        else R[tot]=tot+1;        if(p<=mid) build(L[now],l,mid,p);        else build(R[now],mid+1,r,p);    }}int query(int u,int v,int l,int r,int p){    if(l==r) return l;    else    {        int mid=(l+r)>>1;        if(p>sum[L[u]]-sum[L[v]])            return query(R[u],R[v],mid+1,r,p-sum[L[u]]+sum[L[v]]);        else return query(L[u],L[v],l,mid,p);    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d %d",&n,&k);        for(int i=1;i<=n;i++)            scanf("%d",&a[i]),x[i]=a[i];        sort(a+1,a+n+1);        m=unique(a+1,a+n+1)-a;        memset(s,0,sizeof s);        L[0]=R[0]=sum[0]=tot=0;        for(int i=1;i<=n;i++)        {            x[i]=lower_bound(a+1,a+m,x[i])-a;            s[i]=tot+1;            build(s[i-1],1,m,x[i]);        }        int ll,rr,kk;        for(int i=0;i<k;i++)        {            scanf("%d %d %d",&ll,&rr,&kk);            printf("%d\n",a[query(s[rr],s[ll-1],1,m,kk)]);        }    }    return 0;}

0 0
原创粉丝点击