K-th Number【POJ2104】——主席树

Time Limit: 20000MS Memory Limit: 65536K

Case Time Limit: 2000MS

Description

You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment.
That is, given an array a[1…n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: “What would be the k-th number in a[i…j] segment, if this segment was sorted?”
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2…5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.

Input

The first line of the input file contains n — the size of the array, and m — the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000).
The second line contains n different integer numbers not exceeding 109 by their absolute values — the array for which the answers should be given.
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).

Output

For each question output the answer to it — the k-th number in sorted a[i…j] segment.

Sample Input

7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3
Sample Output

5
6
3
Hint

This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.
Source

Northeastern Europe 2004, Northern Subregion

裸主席树的题。

主席树的主体是线段树,准确的说,是很多棵线段树,存的是一段数字区间出现次数(所以要先离散化可能出现的数字)。举个例子,假设我每次都要求整个序列内的第 k 小,那么对整个序列构造一个线段树,然后在线段树上不断找第 k 小在当前数字区间的左半部分还是右半部分。这个操作和平衡树的 Rank 操作一样,只是这里将离散的数字搞成了连续的数字。
先假设没有修改操作:
对于每个前缀 S1…i,保存这样一个线段树 Ti,组成主席树。这样不是会 MLE 么?最后再讲。
注意,这个线段树对一条线段,保存的是这个数字区间的出现次数,所以是可以互相加减的!还有,由于每棵线段树都要保存同样的数字,所以它们的大小、形态也都是一样的!这实在是两个非常好的性质,是平衡树所不具备的。
对于询问 (i,j),我只要拿出 Tj 和 Ti-1,对每个节点相减就可以了。说的通俗一点,询问 i..j 区间中,一个数字区间的出现次数时,就是这些数字在 Tj 中出现的次数减去在 Ti-1 中出现的次数。
那么有修改操作怎么办呢?
如果将询问看成求一段序列的数字和,那么上面那个相当于求出了前缀和。加入修改操作后,就要用树状数组等来维护前缀和了。于是那个 “很好的性质” 又一次发挥了作用,由于主席树可以互相加减,所以可以用树状数组来套上它。做法和维护前缀和长得基本一样,不说了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <algorithm>

using namespace std;

const int Max = 1e5;

typedef struct node
{
    int l,r,data;
}Tree;

Tree Tr[Max*20];

int top;

int Arr[Max+10],srt[Max+10];

int root[Max+10];

int BS(int *a,int L,int R,int goal)//二分查找
{
    int ans = -1;

    while(L<=R)
    {
        int mid = (L + R)>>1;

        if(a[mid] == goal) return mid;

        if(a[mid]<goal) L = mid+1,ans = mid;

        else R = mid-1;
    }

    return ans;
}

int Creat()
{
    Tr[top].l = Tr[top].r = -1;

    Tr[top].data = 0;

    return top++;
}

void Init(int &a,int L,int R)//初始化线段树
{
    a = Creat();

    if(L == R) return ;

    int mid = (L + R)>>1;

    Init(Tr[a].l,L,mid);

    Init(Tr[a].r,mid+1,R);
}

void Build(int pre,int now,int L,int R,int goal)//建主席树
{
    if(L == R)
    {
        Tr[now].data = Tr[pre].data+1;

        return ;
    }

    int mid = (L + R)>>1;

    if(goal <= mid)
    {
        Tr[now].l = Creat();

        Tr[now].r = Tr[pre].r;

        Build(Tr[pre].l,Tr[now].l,L,mid,goal);
    }
    else 
    {
        Tr[now].l = Tr[pre].l;

        Tr[now].r = Creat();

        Build(Tr[pre].r,Tr[now].r,mid+1,R,goal);
    }

    Tr[now].data = Tr[Tr[now].l].data + Tr[Tr[now].r].data;
}

int Query(int l,int r,int L,int R,int k)//查询区间第k大。
{
    if(L == R) return L;

    int  mid = (L+R) >>1;

    if(k<=Tr[Tr[r].l].data-Tr[Tr[l].l].data)
    {
        return Query(Tr[l].l,Tr[r].l,L,mid,k);
    }
    else
    {
        return Query(Tr[l].r,Tr[r].r,mid+1,R,k -(Tr[Tr[r].l].data-Tr[Tr[l].l].data));
    }
}

int main()
{
    int n,m;

    while(~scanf("%d %d",&n,&m))
    {
        for(int  i = 1; i<= n;i++)
        {
            scanf("%d",&Arr[i]);

            srt[i] = Arr[i];
        }

        sort(srt+1,srt+n+1);

        int N = 1;

        for(int i = 2;i<=n;i++)
        {
            if(srt[N] != srt[i]) srt[++N] = srt[i];
        }

        for(int i = 1;i<=n;i++)
        {
            Arr[i] = BS(srt,1,N,Arr[i]);

        }

        top = 0 ;

        root[0] = -1;

        Init(root[0],1,N);

        for(int i = 1;i<=n;i++)
        {
            root[i] = Creat();

            Build(root[i-1],root[i],1,N,Arr[i]);
        }

        int l,r,k;

        while(m--)
        {
            scanf("%d %d %d",&l,&r,&k);

            int ans = Query(root[l-1],root[r],1,N,k);

            printf("%d\n",srt[ans]);
        }
    }
    return 0;
}

你可能感兴趣的:(K-th Number【POJ2104】——主席树)