Hdu 4251 The Famous ICPC Team Again

The Famous ICPC Team Again

Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 555    Accepted Submission(s): 257


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
 

Source
Fudan Local Programming Contest 2012
 

划分树


#include<stdio.h>
#include<algorithm>
using namespace std;

#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define MAXN 110000

int sorted[MAXN];
int val[20][MAXN];
int toL[20][MAXN];
int n;

void build(int l,int r,int rt,int d)
{
    if(l==r)
    {
        return ;
    }
    int i,count,mid=(l+r)>>1;
    int tmp=sorted[mid];
    count=mid-l+1;
    for(i=l;i<=r;i++)
    {
        if(val[d][i]<tmp)
            count--;
    }
    int lpos=l,rpos=mid+1;
    toL[d][0]=0;
    for(i=l;i<=r;i++)
    {
        if(val[d][i]<tmp)
        {
            val[d+1][lpos++]=val[d][i];
            toL[d][i]=toL[d][i-1]+1;
        }
        else if(val[d][i]>tmp)
        {
            val[d+1][rpos++]=val[d][i];
            toL[d][i]=toL[d][i-1];
        }
        else if(count>0)
        {
            val[d+1][lpos++]=val[d][i];
            toL[d][i]=toL[d][i-1]+1;
            count--;
        }
        else
        {
            val[d+1][rpos++]=val[d][i];
            toL[d][i]=toL[d][i-1];
        }

    }
    build(lson,d+1);
    build(rson,d+1);
}

int query(int L,int R,int k,int l,int r,int rt,int d)
{
    if(l==r)
        return val[d][L];
    int s1;
    int s2;
    if(L!=l)
    {
        s1=toL[d][R]-toL[d][L-1];
        s2=toL[d][L-1]-toL[d][l-1];
    }
    else
    {
        s1=toL[d][R]-toL[d][L-1];
        s2=0;
    }
    int new_L,new_R;
    int mid=(l+r)>>1;
    if(k<=s1)
    {
        new_L=l+s2;
        new_R=l+s1+s2-1;
        return query(new_L,new_R,k,lson,d+1);
    }
    else
    {
        k=k-s1;
        s1=(R-L+1)-s1;
        s2=L-l-s2;
        new_L=(mid+1)+s2;
        new_R=(mid+1)+s1+s2-1;
        return query(new_L,new_R,k,rson,d+1);
    }

}
int main(void)
{
//    freopen("d:\\in.txt","r",stdin);
    int t=1;
    while(scanf("%d",&n)==1)
    {
        int i,j;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&val[0][i]);
            sorted[i]=val[0][i];
        }
        sort(sorted+1,sorted+1+n);
        build(1,n,1,0);
        int m;
        scanf("%d",&m);
        int a,b,k;
        printf("Case %d:\n",t++);
        while(m--)
        {
            scanf("%d%d",&a,&b);
            k=((b-a+2)>>1);
            printf("%d\n",query(a,b,k,1,n,1,0));
        }
    }
    return 0;
}



你可能感兴趣的:(Hdu 4251 The Famous ICPC Team Again)