HDU2665 Kth number

HDU2665 Kth number

Give you a sequence and ask you the kth big number of a interval.

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 query.
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

一.解题思路及要点分析

  1. 题目大意:给定一个数组求一个区间第k小的数
  2. 关于主席树与权值线段树入门讲解可以看看这个博客https://blog.csdn.net/Stupid_Turtle/article/details/80445998, 讲的非常清楚
  3. 对于主席树的理解可以类比: 求数组a[1]到a[n]的和。如果我给定了l和r,想要知道[l,r]这段区间上的和,就可以直接利用前缀和sum[r]-sum[l-1]就可以轻松得到. 那么主席树的思想也是如此,将tree[r]-tree[l-1] (注意是l-1)得到的一棵权值线段树即为属于[l,r]的一棵权值线段树
  4. 要查询第k小数,先求左区间内元素的个数tmp,如果tmp >= k,直接查询左区间内的第k小,否则查询右区间内的第k-tmp小

二.代码

#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;

int num,n,m,s,t,k,cnt;
int a[maxn],b[maxn];//分别记录当前数在排序去重后数组里的位次和排序去重后的数组
int ls[maxn*20],rs[maxn*20],sum[maxn*20],root[maxn*20];//分别记录左子节点,右子节点,所管区间内元素出现的次数,根节点

void build(int l,int r,int &rt)//建立空树,节点均为0
{
    rt=(++cnt);//将索引向前推进便于后续建树和修改
    sum[rt]=0;
    if(l==r)return;
    int mid=(l+r)>>1;
    build(l,mid,ls[rt]);
    build(mid+1,r,rs[rt]);
}
void update(int pre,int &rt,int l,int r,int val)
{
    rt=(++cnt);
    ls[rt]=ls[pre];
    rs[rt]=rs[pre];
    sum[rt]=sum[pre]+1;
    if(l==r)return;
    int mid=(l+r)>>1;
    if(val<=mid) 
    {
        update(ls[pre],ls[rt],l,mid,val);
    }
    else 
    {
        update(rs[pre],rs[rt],mid+1,r,val);
    }
}
int query(int s,int t,int l,int r,int k)
{
    if(l==r) return l;
    int mid=(l+r)>>1;
    int tmp=sum[ls[t]]-sum[ls[s]];
    //要查询第k小数,先求左区间内元素的个数tmp,tmp >= k,直接查询左区间内的第k小,否则查询右区间内的第k-tmp小 
    if(tmp>=k) return query(ls[s],ls[t],l,mid,k);
    else 
    {
        return query(rs[s],rs[t],mid+1,r,k-tmp);
    }
}
int main()
{
    scanf("%d",&num);
    while(num--)
    {
        cnt=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            b[i]=a[i];
        }
        sort(b+1,b+n+1);
        int len=unique(b+1,b+1+n)-b-1;//去掉重复的元素
        build(1,len,root[0]);
        for(int i=1;i<=n;i++)
        {
            a[i]=lower_bound(b+1,b+1+len,a[i])-b;
            update(root[i-1],root[i],1,len,a[i]);
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&s,&t,&k);
            printf("%d\n",b[query(root[s-1],root[t],1,len,k)]);//注意\n
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值