频道栏目
首页 > 程序开发 > 软件开发 > C++ > 正文
poj 2104 K-th Number(划分树裸题)
2013-10-28 09:26:31      个评论      
收藏   我要投稿

 

K-th Number

Time Limit: 20000MS Memory Limit: 65536K

Total Submissions: 33453 Accepted: 10551

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

题意:

给你含n个数的数组。然后叫你求i,j。之间的第k大数。

思路:

划分树裸题。又重新写了次。加深了印象。也发现了一些需注意的地方。

详细见代码:

 

#include<algorithm>  
#include<iostream>  
#include<string.h>  
#include<sstream>  
#include<stdio.h>  
#include<math.h>  
#include<vector>  
#include<string>  
#include<queue>  
#include<set>  
#include<map>  
using namespace std;  
const int INF=0x3f3f3f3f;  
const int maxn=100010;  
int seg[20][maxn],lnum[20][maxn],sa[maxn];  
int n,m;  
void btree(int L,int R,int d)  
{  
    int i,ls,rs,lm,mid;  
    if(L==R)  
        return ;  
    mid=(L+R)>>1;  
    ls=L,rs=mid+1;  
    lm=mid-L+1;  
    for(i=L;i<=R;i++)  
        if(seg[d][i]<sa[mid])  
            lm--;  
    for(i=L;i<=R;i++)  
    {  
        lnum[d][i]=(i==L)?0:lnum[d][i-1];  
        if(seg[d][i]==sa[mid])  
        {  
            if(lm>0)  
            {  
                lm--;  
                lnum[d][i]++;  
                seg[d+1][ls++]=seg[d][i];  
            }  
            else  
                seg[d+1][rs++]=seg[d][i];  
        }  
        else if(seg[d][i]<sa[mid])  
        {  
            lnum[d][i]++;  
            seg[d+1][ls++]=seg[d][i];  
        }  
        else  
            seg[d+1][rs++]=seg[d][i];  
    }  
    btree(L,mid,d+1);  
    btree(mid+1,R,d+1);  
}  
int qu(int L,int R,int l,int r,int d,int k)  
{  
    int ss,s,bb,b,mid;  
    if(L==R)  
        return seg[d][L];  
    ss=(l==L)?0:lnum[d][l-1];//[1,l-1]进入左树的个数  
    s=lnum[d][r]-ss;//[l,r]进入左树的个数  
    mid=(L+R)>>1;  
    if(s>=k)//在左树中  
        return qu(L,mid,L+ss,L+ss+s-1,d+1,k);//注意边界。可以确定L+ss-1不为所求所以从L+ss  
    else  
    {  
        bb=l-L-ss;//[1,l-1]进入右树的个数.(l-1)-L+1-ss  
        b=r-l+1-s;//[l,r]进入右树的个数  
        return qu(mid+1,R,mid+bb+1,mid+bb+b,d+1,k-s);  
    }  
}  
void init()  
{  
    int i;  
    for(i=1;i<=n;i++)  
    {  
        scanf("%d",&seg[0][i]);  
        sa[i]=seg[0][i];  
    }  
    sort(sa+1,sa+n+1);//注意排序起点啊。。  
    btree(1,n,0);//mid+1+bb+b-1  
}  
int main()  
{  
    int i,j,k;  
  
    while(~scanf("%d%d",&n,&m))  
    {  
        init();  
        while(m--)  
        {  
            scanf("%d%d%d",&i,&j,&k);  
            printf("%d\n",qu(1,n,i,j,0,k));  
        }  
    }  
    return 0;  
}  

 

 

点击复制链接 与好友分享!回本站首页
相关TAG标签
上一篇: Introdution to 3D Game Programming With DirectX11
下一篇:leetcode Insert Interval
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站