*hdu 2665 (可持久化线段树)

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
题意:给出n,m,接着n个数,然后m次询问,给出区间l,r和k,求区间第K大的数;
思路:结点存储的是该权值范围内出现元素的总次数。在线段树上找k大数时就像平衡树询问k大数一样根据结点上的信息往左或者往右走。现在可以利用函数式线段树维护权值出现数量,将数列中每个结点依次插入线段树,第r次插入后的线段树与第l-1次插入的线段树之“差”(对应结点的值相减,因为按权值建树结构是一样的)得到的线段树里进行上述的查找k大数操作即可。总之,对于点操作,新增加一个点或修改一个点,只要新建一条从这个点到root[i]的路径即可,这样就形成第i个历史版本的线段树;
用到函数lower_bound()和unique();
函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置;
STL中unique的函数的功能,是去除相邻的重复元素(只保留一个),
还有一个容易忽视的特性是它并不真正把重复的元素删除,是把重复的放到数组的最后面。头文件是include< algorithm > ;
第一次接触可持续化线段树,代码思路都是参考大神们做的,自己在纸上建树有利于理解,其中有用到地址,需要理解;
代码:

#include<cstdio>
#include<algorithm>
#define maxn 100105
using namespace std;
int ls[maxn*20],rs[maxn*20];  
int tot,sum[maxn*20],root[maxn];//tot是总的节点数,sum是记录每个数在某个节点出现的次数,root是每个线段树的根的值;
void build(int l,int r,int &st)  //st表示地址
{
    st=++tot;
    sum[st]=0;
    if(l==r)
        return;
    int temp=(l+r)/2;
    build(l,temp,ls[st]); 
    build(temp+1,r,rs[st]);
}
void update(int last,int k,int l,int r,int &st)
{
    st=++tot;
    ls[st]=ls[last];
    rs[st]=rs[last];
    sum[st]=sum[last]+1;
    if(l==r)
        return;
    int temp=(l+r)/2;
    if(k<=temp)
        update(ls[last],k,l,temp,ls[st]);
    else
        update(rs[last],k,temp+1,r,rs[st]);
}
int query(int ss,int tt,int k,int l,int r)
{
    if(l==r)
        return l;
    int ans=sum[ls[tt]]-sum[ls[ss]];
    int temp=(l+r)/2;
    if(k<=ans)
        return query(ls[ss],ls[tt],k,l,temp);
    else
        return query(rs[ss],rs[tt],k-ans,temp+1,r);
}
int a[maxn],b[maxn];
int main()
{
    int T;
    int n,m;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            b[i]=a[i];
        }
        tot=0;
        sort(b+1,b+n+1);
        int t=unique(b+1,b+n+1)-b-1;
        build(1,t,root[0]);
        for(int i=1;i<=n;i++)
            a[i]=lower_bound(b+1,b+t+1,a[i])-b;
        for(int i=1;i<=n;i++)
        {
            update(root[i-1],a[i],1,t,root[i]);
        }
        for(int i=1;i<=m;i++)
        {
            int l,r,k;
            scanf("%d%d%d",&l,&r,&k);
            int ans=query(root[l-1],root[r],k,1,t);
            printf("%d\n",b[ans]);
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值