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): 733    Accepted Submission(s): 351
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
 
Recommend
We have carefully selected several similar problems for you:   4255  4247  4252  4246  4248 
 
タイトル:
配列をあげます.2つの端点a,bを聞いてください.[a,b]の中間値を聞いてください.例えば、1,2,5の中間値は2です.
考え方:
木を分ける裸の問題.簡単です.手がつけられないことはありません.木を分ける思想はやはり線分の木の分治の思想に似ています.
ネット上ではあまり詳しく言われていないので、ほとんど直接コードをあげます.私はまだ午後研究してやっと茅塞顿が開いたのです.分かりました.簡単ですが.
水題1 Aはやはりさっぱりしています.ふふ~またデータ構造を勉強しました.
詳細は、コードを参照してください.
#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 n,m,md;
int seg[20][maxn],lnum[20][maxn],sa[maxn];
void init()
{
    memset(lnum,0,sizeof lnum);
    sort(sa,sa+n);
}
void btree(int L,int R,int d)
{
    int i,lm,ls,rs,mid;
    md=max(d,md);
    if(L==R)
        return;
    mid=(L+R)>>1;
    lm=mid-L+1;
    ls=L;
    rs=mid+1;
    for(i=L;i<=R;i++)
        if(seg[d][i]<sa[mid])
            lm--;
    for(i=L;i<=R;i++)
    {
        if(i==L)
            lnum[d][i]=0;
        else
            lnum[d][i]=lnum[d][i-1];
        if(seg[d][i]==sa[mid])
        {
            if(lm>0)
            {
                lnum[d][i]++;
                seg[d+1][ls++]=seg[d][i];
                lm--;
            }
            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)
{
    if(L==R)
        return seg[d][L];
    int s,ss,b,bb,mid;
    if(l==L)
        ss=0;
    else
        ss=lnum[d][l-1];//[1,l-1]       
    s=lnum[d][r]-ss;//[l,r]       
    //printf("d %d ss %d s %d k %d
",d,ss,s,k); 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] b=r-l+1-s;//[l,r] return qu(mid+1,R,mid+bb+1,mid+bb+b,d+1,k-s);//mid+1+bb+b-1 } } int main() { int a,b,i,j,cas=1; while(~scanf("%d",&n)) { for(i=1;i<=n;i++) { scanf("%d",&seg[0][i]); sa[i]=seg[0][i]; } md=0; init(); btree(1,n,0); // for(i=0;i<=md;i++) // { // for(j=1;j<=n;j++) // printf("%d ",seg[i][j]); // printf("
"); // } scanf("%d",&m); printf("Case %d:
",cas++); while(m--) { scanf("%d%d",&a,&b); printf("%d
",qu(1,n,a,b,0,(b-a+2)/2)); } } return 0; }