HDU2665 Kth number(主席树)

Kth number

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

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

解析:

经典的主席树模板题求第k小的数

主席树:
是按值插入的可持久化线段树(具有前缀和性质,新的树是在原来的树的基础上更新某个点得到的)
因为是按值插入,序列内的值可能很大,所以需要进行离散化。
例如10 6 7 8,离散化成4 1 2 3
在按值插入,每一个节点的值表示的区间[L,R]表示序列内在[L,R]内的数的个数。
例如,最终根节点的值应该表示的是序列内数的值在[1,4]内的个数,应该为4
其左孩子节点表示的是[1,2],表示序列内数的值在[1,2]内的个数,应该为2
主席树就是记录每一次插入之后线段树。上述例子就有5个状态——初始没有任何值插入,插入第一个数(值为4)后的线段树,插入第二个数(值为1)后的线段树.......(ps:插入的位置是线段树的叶子节点,例如4就是插入到从左往右数第4个叶子节点(该节点表示的区间是[4,4])),1也是同理第一个叶子节点)
而这样存储的话,空间复杂度会极大。
并且发现在这些线段树中有很大一部分是重复的。那么我们所要做的就是使他们公共重复部分,记录不同的部分即可。

这里有两个不错的大佬博客

点击打开链接

点击打开链接

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 1e5+5;
//const int MAXM = (1e5+5)*15;

typedef struct node
{
    int x;
    int id;
}node;

int a[MAXN];
int ran[MAXN];
int rt[MAXN*20],ls[MAXN*20],rs[MAXN*20],tot,sz;
int sum[MAXN*20];

bool cmp(node a,node b)
{
    return a.x<b.x;
}

void build(int& root,int l,int r)
{
    root=++tot;
    sum[root]=0;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(ls[root],l,mid);
    build(rs[root],mid+1,r);

}

void update(int& root,int l,int r,int last,int x)
{
    root=++tot;
    ls[root]=ls[last];
    rs[root]=rs[last];
    sum[root]=sum[last]+1;
    if(l==r) return ;
    int mid=(l+r)>>1;
    if(mid>=x) update(ls[root],l,mid,ls[last],x);
    else update(rs[root],mid+1,r,rs[last],x);


}

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

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]),ran[i]=a[i];
        tot=0;
        sort(ran+1,ran+1+n);
        sz=unique(ran+1,ran+1+n)-(ran+1);
        build(rt[0],1,sz);
        for(int i=1;i<=n;i++) a[i]=lower_bound(ran+1,ran+1+sz,a[i])-ran;
        for(int i=1;i<=n;i++) update(rt[i],1,sz,rt[i-1],a[i]);
        for(int i=0;i<m;i++)
        {
            int l,r,k;
            scanf("%d%d%d",&l,&r,&k);
            int ind=query(rt[l-1],rt[r],1,sz,k);
            printf("%d\n",ran[ind]);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37025443/article/details/81005260
今日推荐