设为首页 加入收藏

TOP

划分树详解 结合例题hdu4251(三)
2012-11-17 09:28:12 来源: 作者: 【 】 浏览:969
Tags:划分 详解   结合 例题 hdu4251

个数据被分到了左边(注意这里用的是闭区间)。 

The Famous ICPC Team Again
Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 558    Accepted Submission(s): 260


Problem Description
When Mr. B, Mr. G and Mr. M were preparing for the 2012 ACM-ICPC World Final Contest, Mr. B had collected a large set of contest problems for their daily training. When they decided to take training, Mr. B would choose one of them from the problem set. All the problems in the problem set had been sorted by their time of publish. Each time Prof. S, their coach, would tell them to choose one problem published within a particular time interval. That is to say, if problems had been sorted in a line, each time they would choose one of them from a specified segment of the line.

Moreover, when collecting the problems, Mr. B had also known an estimation of each problem’s difficultness. When he was asked to choose a problem, if he chose the easiest one, Mr. G would complain that “Hey, what a trivial problem!”; if he chose the hardest one, Mr. M would grumble that it took too much time to finish it. To address this dilemma, Mr. B decided to take the one with the medium difficulty. Therefore, he needed a way to know the median number in the given interval of the sequence.
 

Input
For each test case, the first line contains a single integer n (1 <= n <= 100,000) indicating the total number of problems. The second line contains n integers xi (0 <= xi <= 1,000,000,000), separated by single space, denoting the difficultness of each problem, already sorted by publish time. The next line contains a single integer m (1 <= m <= 100,000), specifying number of queries. Then m lines follow, each line contains a pair of integers, A and B (1 <= A <= B <= n), denoting that Mr. B needed to choose a problem between positions A and B (inclusively, positions are counted from 1). It is guaranteed that the number of items between A and B is odd.
 

Output
For each query, output a single line containing an integer that denotes the difficultness of the problem that Mr. B should choose.
 

Sample Input
5
5 3 2 4 1
3
1 3
2 4
3 5
5
10 6 4 8 2
3
1 3
2 4
3 5
 

Sample Output
Case 1:
3
3
2
Case 2:
6
6
4


题意:  输入n  以及n个数  然后输入m个区间 问每个区间中的中间大的数是哪个
[cpp]
#include<stdio.h>  
#include<algorithm>  
using namespace std;  
#define M 100005  
int tree[20][M],sorted[M];  
int toLeft[20][M];  
void build(int level,int left,int right){  
    if(left==right)return ;  
    int mid=(left+right)>>1;  
    int i;  
    int suppose;//假设在中位数sorted[mid]左边的数都全部小于sorted[mid]  
    suppose=mid-left+1;  
    for(i=left;i<=right;i++){  
        if(tree[level][i]<sorted[mid]){  
            suppose--;  
        }  
    }  
    //如果suppose==1,则说明数组中值为sorted[mid]只有一个数。比如序列:1 3 4 5 6,sorted[mid]=4  
    /*如果suppose>1,则说明数组中左半边值为sorted[mid]的不止一个数,为mid-suppose。比如序列:1 4 4 4 6,sorted[mid]=4 
    */  
    int lpos=left,rpos=mid+1;  
    for(i=left;i<=right;i++){  
        if(i==left){//这里是预处理,相当与初始化  
            toLeft[level][i]=0;  
        }else{  
            toLeft[level][i]=toLeft[level][i-1];  
        }  
        if(tree[level][i]<sorted[mid]){//划分到中位数左边  
            toLeft[level][i]++;  
            tree[level+1][lpos++]=tree[level][i];  
        }else if(tree[level][i]>sorted[mid]){//划分到中位数右边  
            tree[level+1][rpos++]=tree[level][i];  
        }else{//这里,suppose大于0的数划分到中位数的左边  
            if(suppose!=0){//这里的处理太巧妙了!帅气!  
                suppose--;  
                toLeft[level][i]++;  
                tree[level+1][lpos++]=tree[level][i];  
            }else{//表示  
                tree[level+1][rpos++]=tree[level][i];  
            }  
        }  
    }  
    build(level+1,left,mid);  
    build(level+1,mid+1,right);  
}  
//在[left,right]数据中查询[qleft,qright]中第k大的数据  
int query(int level,int left,int right,int qleft,int qright,int k){  
    if( qleft==qright)  
        return tree[level][qleft];  
    int s;//代表[left,qleft)之间有多个个元素被分到左边  
    int ss;//[qleft, qright]内将被划分到左子树的元素数目  
    int mid=(left+right)>>1;  
    if(left==qleft){  
        s=0;  
        ss=toLeft[level][qright];  
    }else{  
        s=toLeft[level][qleft-1];  
        ss=toLeft[level][qright]-s;  
    }  
    int newl,newr;  
    if(k<=ss){//查询左边  
        newl=left+s;  
        newr=left+s+ss-1;  
        return query(level+1,left,mid,newl,newr,k);  
    }else{//查询右边  
        newl=mid-left+1+qleft-s;  
        newr=mid-left+1+qright-s-ss;  
        return query(level+1,mid+1,right,newl, newr,k - ss);  
    }  
}  
int main(){  
    int n,m,cas=0;  
    while(scanf("%d",&n)!=EOF) 
    {  
        cas++; 
        printf("Case %d:\n",cas); 
        int i;  
        for(i=1;i<=n;i++){  
            scanf("%d",&tree[0][i]);  
            sorted[i]=tree[0][i];  
        }  
        sort(sorted+1,sorted+n+1);  
        build(0,1,n);  
        scanf("%d",&m); 
        int ql,qr,k;  
        for(i=0;i<m;i++){  
             
            scanf("%d %d",&ql,&qr);  
            k=(qr-ql)/2+1; 
            printf("%d\n",query(0,1,n,ql,qr,k));  
        }  
    }  
    return 0;  
}  

        

首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++二维数组的动态分配 下一篇用栈实现的自动走迷宫

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: