A - K-th Number POJ - 2104 (主席树)

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 10  9 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.

这个题目的要求是要求给定区间里面第K大的数,要求第K大最明显的方法是将给定的区间排序,然后找到第K个数就行了,但是这样做很显然会超时,这里我们要采用一种线段树衍生出来的数据结构,主席数,主席树上每一颗节点保存的是一颗树,每颗树节点保存的是某个数值区间出现的数的个数,我们假设要找第k大,在给定的数的范围[1,n]之间,如果[1,n/2]之间有超过k个数所以第k大数一定在这个区间里面,否则在另一半区间里面,这是主席树找第k大的数的依据,因为每颗节点保存的都是[1,n]数值区间内出现的数的个数,所以要求给定区间[x,y]之间有多少个树我们可以用[1,y]的值减去[1,x-1]的值,这样就是区间[x, y]里面在数据范围在[1, n]之间的数的个数,然后我们可以逐步缩小数值范围,然后确定第k大的值,就相当于是在二分求取满足条件的值。

代码如下:

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 10 ;
struct Node
{
    int l, r;
    int sum;
}node[maxn*20];
struct Num
{
    int v, pos;
    bool operator <(const Num &a) const
    {
        return v < a.v;
    }
}a[maxn];
int node_cnt;
int rank[maxn], root[maxn];
int build(int l, int r)
{
    if(l == r)
    {
        node[node_cnt++].sum = 0;
        return node_cnt-1;
    }
    int m = (l+r)/2;
    int x;
    node[node_cnt++].sum = 0;
    x = node_cnt-1;
    node[x].l = build(l, m);
    node[x].r = build(m+1, r);
    return x;
}
int insert(int p, int l, int r, int rt)
{
    node[node_cnt++] = node[rt];
    int x = node_cnt-1;
    node[x].sum++;
    if(l == r)
    {
        return x;
    }
    int m = (l+r)/2;
    if(m >= p)
    {
        node[x].l = insert(p, l, m, node[x].l);
    }
    else
    {
        node[x].r = insert(p, m+1, r, node[x].r);
    }
    return x;
}
int query(int r1, int r2, int l, int r, int k)
{
    if(l == r)
    {
        return l;
    }
    int m = (l+r)/2;
    int temp = node[node[r2].l].sum-node[node[r1].l].sum;
    if(temp >= k)
    {
        return query(node[r1].l, node[r2].l, l, m, k);
    }
    else
    {
        return query(node[r1].r, node[r2].r, m+1, r, k-temp);
    }
}                                                                                                                                           ;

int main()
{
    int m, n;
    while(scanf("%d%d", &n, &m) != EOF)
    {
        node_cnt = 0;
        root[0] = build(1, n);
        for(int i = 1; i <= n; ++ i)
        {
            scanf("%d", &a[i].v);
            a[i].pos = i;
        }
        sort(a+1, a+n+1);
        for(int i = 1; i <= n; ++ i)
        {
            rank[a[i].pos] = i;
        }
        for(int i = 1; i <= n; ++ i)
        {
            root[i] = insert(rank[i], 1, n, root[i-1]);
        }
        int x, y, k, ps;
        for(int i = 0; i < m; ++ i)
        {
            scanf("%d%d%d", &x, &y, &k);
            ps = query(root[x-1], root[y], 1, n, k);
            printf("%d\n", a[ps].v);
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值