HDU 2665

Kth number

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


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
 

Recommend
zty   |   We have carefully selected several similar problems for you:   2667  2663  2661  1542  2871 

 


区间第k小,可持久化线段树

#include<stdio.h>
#include<algorithm>
#define N 100010
int T[N];
int num[N];
int san[N];
int ls[N*20];  
int rs[N*20];
int sum[N*20];
int tot,rt;
int n, m;
void Build(int l,int r,int &rt)
{
    rt=++tot;
    sum[rt]=0;
    if(l==r) return ;
    int m=(l+r)>>1;
    Build(l,m,ls[rt]);
    Build(m+1,r,rs[rt]);
}

void Update(int last,int p,int l,int r,int &rt)
{
    rt=++tot;
    ls[rt]=ls[last];
    rs[rt]=rs[last];
    sum[rt]=sum[last]+1;
    if(l==r) return ;
    int m=(l+r)>>1;
    if(p<=m) Update(ls[last],p,l,m,ls[rt]);
    else     Update(rs[last],p,m+1,r,rs[rt]);
}

int Query(int ss,int tt,int l,int r,int k)
{
    if(l==r) return l;
    int m=(l+r)>>1;
    int cnt=sum[ls[tt]]-sum[ls[ss]];
    if(k<=cnt)
        return Query(ls[ss],ls[tt],l,m,k);
    else
        return Query(rs[ss],rs[tt],m+1,r,k-cnt);
}

void gogogo()
{
    int l,r,k;
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&num[i]);
        san[i]=num[i];
    }
    tot=0;
    std::sort(san+1,san+n+1);
    int cnt=std::unique(san+1,san+n+1)-san-1;
    Build(1,cnt,T[0]);
    for(int i=1; i<=n; i++)   num[i]=std::lower_bound(san+1,san+cnt+1,num[i])-san;
    for(int i=1; i<=n; i++) 	Update(T[i-1],num[i],1,cnt,T[i]);
    while(m--)
    {
        scanf("%d%d%d",&l,&r,&k);
        int id=Query(T[l-1],T[r],1,cnt,k);  //下标从1开始
       // printf("%d\n",id);
        printf("%d\n",san[id]); //假设你要求区间[l,r]的第k大,那就是第r-l+1-k大。
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        gogogo();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值