POJ 2104 K-th Number【划分树】

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

恩,题意是,一列数,求从第 l 到第 r 之间的第 k 小的数,就是将第 l 到第 r 位数都拿出来从小到大排序之后的第 k 位数。


#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxn 100500
using namespace std;
#define mid ((l+r)>>1)
int a[maxn];//记录原数列
int s[maxn];//记录排序后数列
int t[20][maxn];//记录分的每一层的数值
int num[20][maxn];//记录从该区间的l到当前位置之间有多少个元素进入其左子树
int n,m;

void build(int c,int l,int r)//层数 区间左端点 区间右端点
{
    int lm=mid-l+1,lp=l,rp=mid+1;//lm为左子树中可放数的个数 lp为左子树的区间左端点 rp为右子树的区间左端点
    for(int i=l;i<=mid;++i)
        lm-=s[i]<s[mid];//判断左子树中有多少个值与s[mid]相等 包括自身(即比它小的数必须放到左子树中,最后的lm值为左子树中可放与s[mid]相等的值的个数
    for(int i=l;i<=r;++i)
    {
        if(i==l)
            num[c][i]=0;//初始化为0
        else
            num[c][i]=num[c][i-1];//在前一个值上累计
        if(t[c][i]==s[mid])
        {
            if(lm)//记录的是有多少个与s[mid]相等的数可以放入左子树中
            {
                lm--;
                num[c][i]++;//从l到i放入左子树的个数加1
                t[c+1][lp++]=t[c][i];//记录左子树中放的是哪些值
            }
            else
                t[c+1][rp++]=t[c][i];//与s[mid]相等但左子树已没有空间放,要放到右子树中
        }
        else if(t[c][i]<s[mid])//放入左子树
        {
            num[c][i]++;
            t[c+1][lp++]=t[c][i];
        }
        else //放入右子树
            t[c+1][rp++]=t[c][i];
    }
    if(l<r)
        build(c+1,l,mid),build(c+1,mid+1,r);
}
int query(int c,int l,int r,int ql,int qr,int k)
{
    if(l==r)
        return t[c][l];
    int s,ss;//s为ql左边放入左子树的个数,ss为从区间左端到qr放入左子树的个数
    if(l==ql)
        s=0,ss=num[c][qr];
    else
        s=num[c][ql-1],ss=num[c][qr]-num[c][ql-1];
    if(k<=ss)//证明要查找的值在左子树中
        return query(c+1,l,mid,l+s,l+s+ss-1,k);//l+s为原本区间的左端点加上ql左端进入左子树的个数,即从l+s开始为从ql开始放入左子树的数 l+s+ss-1为原本区间左端点加上qr左端进入左子树的个数,即从l+s+ss-1往后是从qr往后放入左子树的数
    else//ss为ql到qr之间进入左子树的个数,所以这里要减去ss
        return query(c+1,mid+1,r,mid+1+ql-l-s,mid+1+qr-l-s-ss,k-ss);//ql-l-s为从该区间左端点到ql左端进入右子树的个数,再加上mid+1即为右子树中从ql开始放入右子树的数 qr-l-s-ss为该区间左端点到qr左端放入右子树的个数,再加上mid+1即为右子树中ql到qr之间的数放入右子树中的最右端
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&a[i]);
            s[i]=t[0][i]=a[i];
        }
        sort(s+1,s+1+n);
        build(0,1,n);
        while(m--)
        {
            int l,r,k;
            scanf("%d%d%d",&l,&r,&k);
            printf("%d\n",query(0,1,n,l,r,k));
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值