[POJ2104]K-th Number-划分树

K-th Number

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


虽说划分树号称是“时代的眼泪”……
但是在某何姓学长在校内冬令营集训上当着百余人的面(有无数的外省大佬)讲了划分树可以做树套树外层这一性质后,突然有种不好的预感……
赶紧先学一下.jpg


思路:
划分树板子题~
所谓划分树,就是类似如下的东西:

    15248376
    . .. .
   1243 5876
   ..   .  .
  12 43 56 87
  .   . .   .
1 2 3 4 5 6 7 8

每一段连续的数字代表了一个节点及其内部的数字。
如果树的每一层下面有点存在,则代表这个点在下一层中会在当前节点的左子树,否则去右子树。

对于每一层,将较小的一半的数顺序不变划入左子树,其余顺序不变划入右子树。
这样递归到叶子便可得到一棵划分树~

对于查询,考虑记录每一层每个点之前有多少个点被划入左子树。
每次递归下去,使用上面的信息推算出符合条件的下一层中询问区间的起点和终点。
由于有序,可以永远保证询问区间在每层递归都是连续的一段。
若查询区间中有大于k个被划到了左子树,说明答案在左子树中。
否则,将k减去查询区间中划到左子树中的点数,递归右儿子。
最后走到叶子即可得到答案~

具体见代码吧~
纯脑补写的有点奇怪请见谅~

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

using namespace std;

inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0' || '9'<ch){if(ch=='-')f=-1;ch=getchar();}
    while('0'<=ch && ch<='9')x=x*10+(ch^48),ch=getchar();
    return x*f;
}

const int N=100009;
const int K=21;

int n,m;
int t[K][N],v[K][N];
int ori[N];

inline void build(int dep,int l,int r)
{
    if(l==r)return;
    int mid=l+r>>1,lcnt=l-1,rcnt=mid,midcnt=mid-l+1;

    for(int i=l;i<=r;i++)
        if(t[dep][i]<ori[mid])
            midcnt--;

    for(int i=l;i<=r;i++)
        if(t[dep][i]<ori[mid] || (t[dep][i]==ori[mid] && midcnt))
        {
            t[dep+1][++lcnt]=t[dep][i];
            v[dep][i]=v[dep][i-1]+1;
            if(t[dep][i]==ori[mid])midcnt--;
        }
        else
        {
            t[dep+1][++rcnt]=t[dep][i];
            v[dep][i]=v[dep][i-1];
        }

    build(dep+1,l,mid);
    build(dep+1,mid+1,r);
}

inline int query(int dep,int l,int r,int dl,int dr,int k)
{
    if(l==r)return t[dep][l];
    int mid=l+r>>1;

    int delta=v[dep][dr]-v[dep][dl-1];
    if(delta>=k)
        return query(dep+1,l,mid,v[dep][dl-1]-v[dep][l-1]+l,v[dep][dr]-v[dep][l-1]+l-1,k);
    else
    {
        int pre=dl-l-(v[dep][dl-1]-v[dep][l-1]);
        return query(dep+1,mid+1,r,mid+1+pre,mid+1+pre+dr-dl+1-delta-1,k-delta);
    }
}

int main()
{
    n=read();m=read();
    for(int i=1;i<=n;i++)
        t[0][i]=ori[i]=read();
    sort(ori+1,ori+n+1);
    build(0,1,n);

    while(m--)
    {
        int l=read(),r=read(),k=read();
        printf("%d\n",query(0,1,n,l,r,k));
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值