HDU 2665 Kth number

题目链接 : HDU2665

Kth number

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


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
 

 

划分树模板题    不知道以后会不会用Splay再写一次

 

 

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
using namespace std;


int tree[20][100005],left[20][100005];
int num[100005];
bool cmp(int a,int b)
{
    return tree[1][a]<tree[1][b];
}
void build_tree(int l,int r,int dep)
{
    if(l==r)return;
    int mid=(l+r)>>1;
    int lpos=l,rpos=mid+1;
    for(int i=l;i<=r;i++)
    {
        if(tree[dep][i]>mid) tree[dep+1][rpos++]=tree[dep][i];
        else tree[dep+1][lpos++]=tree[dep][i];
        left[dep][i]=lpos-l;
    }
    build_tree(l,mid,dep+1);
    build_tree(mid+1,r,dep+1);
}
int query(int lef,int rig,int l,int r,int dep,int k)
{
    if(l==r) return tree[dep][l];
    int mid=(lef+rig)>>1;
    int cnt=left[dep][r]-((l!=lef)?left[dep][l-1]:0);
    if(cnt<k)
    {
        int newl=left[dep][rig]+l-((l!=lef)?left[dep][l-1]:0);
        int newr=newl+(r-l-cnt);
        return query(mid+1,rig,newl,newr,dep+1,k-cnt);
    }
    else
    {
        int newl=lef+((l!=lef)?left[dep][l-1]:0);
        int newr=newl+cnt-1;
        return query(lef,mid,newl,newr,dep+1,k);
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%d",&tree[1][i]),num[i]=i;
        sort(num+1,num+n+1,cmp);
        for(int i=1;i<=n;i++)
            tree[0][num[i]]=i;
        for(int i=1;i<=n;i++)
            num[i]=tree[1][num[i]];
        build_tree(1,n,0);
        int l,r,k;
        while(m--)
        {
            scanf("%d%d%d",&l,&r,&k);
            printf("%d\n",num[query(1,n,l,r,0,k)]);
        }

    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值