现在的位置: 首页 > 综合 > 正文

poj 2104 K-th Number(划分树)

2014年09月29日 ⁄ 综合 ⁄ 共 3006字 ⁄ 字号 评论关闭
K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 33553   Accepted: 10600
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小的数
题解:划分树可以解之,本人第一次码,参照博客  http://blog.csdn.net/acceptedxukai/article/details/6937084
#include<stdio.h>
#include<stdlib.h>
#define N 100008
struct node{
    int l,r;
    int mid(){ return (l+r)>>1; }
}tree[N*4+520];
int n,sorted[N];
int val[20][N],toleft[20][N];
int cmp(const void *a,const void *b)
{
    return *(int *)a<*(int *)b?-1:1;
}
void build(int l,int r,int pos,int deep)
{
    tree[pos].l=l;
    tree[pos].r=r;
    if(l==r) return;
    int mid=tree[pos].mid();
    int midval=sorted[mid];
    int leftsame=mid-l+1;
    for(int i=l;i<=r;i++)
    {
        if(val[deep][i]<midval)
            leftsame--;
    }
    int lpos=l,rpos=mid+1;
    for(int i=l;i<=r;i++)
    {
        if(i==l) toleft[deep][i]=0;
        else toleft[deep][i]=toleft[deep][i-1];
        if(val[deep][i]<midval)
        {
            toleft[deep][i]++;
            val[deep+1][lpos++]=val[deep][i];
        }
        else if(val[deep][i]>midval)
        {
            val[deep+1][rpos++]=val[deep][i];
        }
        else
        {
            if(leftsame>0)
            {
                leftsame--;
                toleft[deep][i]++;
                val[deep+1][lpos++]=val[deep][i];
            }
            else
            {
                val[deep+1][rpos++]=val[deep][i];
            }
        }
    }
    build(l,mid,pos<<1,deep+1);
    build(mid+1,r,pos<<1|1,deep+1);
}
int query(int l,int r,int k,int pos,int deep)
{
    if(l==r) return val[deep][l];
    int num1,num2;
    if(l==tree[pos].l)
    {
        num1=toleft[deep][r];
        num2=0;
    }
    else
    {
        num2=toleft[deep][l-1];
        num1=toleft[deep][r]-num2;
    }
    if(num1>=k)
    {
        int newl=tree[pos].l+num2;
        int newr=newl+num1-1;
        return query(newl,newr,k,pos<<1,deep+1);
    }
    else
    {
        int mid=tree[pos].mid();
        int num3=r-l+1-num1;
        int num4=l-1-tree[pos].l+1-num2;
        int newl=mid+num4+1;
        int newr=mid+num4+num3;
        return query(newl,newr,k-num1,pos<<1|1,deep+1);
    }

    return 0;
}
int main()
{
    int m,l,r,x;

    while(scanf("%d%d",&n,&m)>0)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&val[0][i]);
            sorted[i]=val[0][i];
        }
        qsort(sorted+1,n,sizeof(int),cmp);
        build(1,n,1,0);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&l,&r,&x);
            printf("%d\n",query(l,r,x,1,0));
        }
    }

    return 0;
}

抱歉!评论已关闭.