poj 2104 K-th Number(划分树)

K-th Number
Time Limit: 20000MS Memory Limit: 65536K
Total Submissions: 28725 Accepted: 8602
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 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.

Source

Northeastern Europe 2004, Northern Subregion

题目:http://poj.org/problem?id=2104

题意:给你一个数列,每次询问一个区间内第k大的数。。。

分析:这题有多种搞法,不过我没去多想,主要是为了学划分树才做这题的,所有,如果会划分树的话,这个直接就是个模板题了。。。

PS:在网上找关于划分树的资料,结果没找到合适的,最后找了个代码研究了下,才明白,划分树是个简单的东西。。。

划分树有两个步骤,建树和查询:

建树比较简单,就是对于每个区间,把小于等于它的中位数的数移到左边构成左子树,其余的构成右子树,(注意数的前后位置不能调换,查询时用到)

接下来就是如何查询了,假设要查询区间 [ L,R ]的第k大,当前查找到划分树的 [ l,r ] 子树,那么首先要计算两个值,一个w1为 区间 [ l, L-1 ]中排在[ l,r ] 的左子树中的个数,

一个w2为 区间 [ L, R ]中排在[ l,r ] 的左子树中的个数,那么如果k大于 w2的话,第k大的数肯定在右子树,否则在左子树。

如果在左子树的话,那么区间 [ L,R ]相应的变成 [ l +w1, l+w1+w2-1 ],而k不变。。。(因为数的摆放顺序相对不变,所以区间移到左子树时,前面肯定有w1个数了,而区间只关心移过去的w2个数)

如果在右子树,相应的,区间变成 [ m+w1+1,m +w1+w2 ]具体与左子树相同


代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int mm=111111;
int a[mm],s[22][mm],g[22][mm];
int i,j,k,n,m;
void build(int l,int r,int d)
{
    if(l==r)return;
    int i,t1,t2,same=0,m=(l+r)>>1;
    for(i=l;i<=r;++i)
        g[d][i]=0,same+=(s[d][i]<a[m]);
    same=m-l+1-same;
    for(t1=i=l,t2=m+1;i<=r;++i)
        if(s[d][i]==a[m]&&same)--same,s[d+1][t1++]=s[d][i],g[d][i]=1;
        else if(s[d][i]<a[m])
            s[d+1][t1++]=s[d][i],g[d][i]=1;
        else s[d+1][t2++]=s[d][i];
    for(i=l;i<r;++i)
        g[d][i+1]+=g[d][i];
    build(l,m,d+1);
    build(m+1,r,d+1);
}
int query(int L,int R,int k,int l,int r,int d)
{
    if(l==r)return s[d][l];
    int m=(l+r)>>1,w1=0,w2=g[d][R];
    if(L>l)w2-=(w1=g[d][L-1]);
    if(w2>=k)return query(l+w1,l+w1+w2-1,k,l,m,d+1);
    k-=w2;
    w1=L-l-w1;
    w2=R-L+1-w2;
    return query(m+w1+1,m+w1+w2,k,m+1,r,d+1);
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(i=1;i<=n;++i)
        {
            scanf("%d",&a[i]);
            s[0][i]=a[i];
        }
        sort(a+1,a+n+1);
        build(1,n,0);
        while(m--)
        {
            scanf("%d%d%d",&i,&j,&k);
            printf("%d\n",query(i,j,k,1,n,0));
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值