HDU4251-The Famous ICPC Team Again(划分树)

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

?裸的 划分树。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 100000+10;
int n,m;
struct node{
    int lson,rson;
    int mid(){
        return (lson+rson)>>1;
    }
}tree[maxn*4];
int num[maxn];
int seg[25][maxn];
int  lftnum[25][maxn];
void build(int L,int R,int rt,int  dep){
    tree[rt].lson = L;
    tree[rt].rson = R;
    if(L==R) return;
    int mid = tree[rt].mid(),key = num[mid];
    int lcnt  = mid-L+1;
    for(int i = L; i <= R; i++){
        if(seg[dep][i] < key){
            lcnt--;
        }
    }
    int sta = L, ed = mid+1;
    for(int i = L;i <= R; i++){
        if(i==L){
            lftnum[dep][i] = 0;
        }else{
            lftnum[dep][i]  = lftnum[dep][i-1];
        }
        if(seg[dep][i] < key){
            lftnum[dep][i]++;
            seg[dep+1][sta++] = seg[dep][i];
        }
        else if(seg[dep][i] > key){
            seg[dep+1][ed++] = seg[dep][i];
        }
        else{
            if(lcnt>0){
                lftnum[dep][i]++;
                lcnt--;
                seg[dep+1][sta++] = seg[dep][i];
            }else{
                seg[dep+1][ed++] = seg[dep][i];
            }
        }
    }
    build(L,mid,rt<<1,dep+1);
    build(mid+1,R,rt<<1|1,dep+1);
}
int query(int L,int R,int rt,int dep,int k){
    if(tree[rt].lson ==tree[rt].rson) return seg[dep][tree[rt].lson];
    int ucnt,ncnt;
    int mid = tree[rt].mid();
    if(tree[rt].lson == L){
        ncnt = 0;
        ucnt = lftnum[dep][R];
    }else{
        ncnt = lftnum[dep][L-1];
        ucnt = lftnum[dep][R] - lftnum[dep][L-1];
    }
    if(ucnt >= k){
        L = tree[rt].lson + ncnt;
        R = tree[rt].lson + ncnt + ucnt-1;
        return query(L,R,rt<<1,dep+1,k);
    }else{
        int a = L - tree[rt].lson - ncnt;
        int b = R - L - ucnt + 1;
        L = mid+a+1;
        R = mid+a+b;
        return query(L,R,rt<<1|1,dep+1,k-ucnt);
    }
}

int main(){

    int T = 1;
    while(cin >> n){
        memset(seg,0,sizeof seg);
        memset(lftnum,0,sizeof lftnum);
        for(int i = 1; i <= n; i++){
            scanf("%d",&num[i]);
            seg[0][i] = num[i];
        }
        sort(num+1,num+n+1);
        build(1,n,1,0);
        scanf("%d",&m);
        printf("Case %d:\n",T++);
        while(m--){
            int a,b;
            scanf("%d%d",&a,&b);
            printf("%d\n",query(a,b,1,0,(b-a)/2+1));
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/ldxsuanfa/p/10494999.html

时间: 2024-11-08 19:25:16

HDU4251-The Famous ICPC Team Again(划分树)的相关文章

hud--4251The Famous ICPC Team Again+划分树入门题

题目链接:点击进入 这次求得是给定区间的中值,所以还是直接可以套划分树的模板. 代码如下: #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; const int maxn=100000+100; int tree[20][maxn]; int sorted[maxn]; int toleft[20][maxn]; voi

HDOJ 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): 859    Accepted Submission(s): 415 Problem Description When Mr. B, Mr. G and Mr. M were preparing for the

A Famous ICPC Team

A  Famous ICPC Team                                      Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Mr. B, Mr. G, Mr. M and their coach Professor S are planning their way to Warsaw for the ACM-ICPC World Finals. Each of th

【HDOJ】4251 The Famous ICPC Team Again

划分树模板题目,主席树也可解.划分树. 1 /* 4251 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector> 10 #include <deq

HDU 4247 A Famous ICPC Team

Problem Description Mr. B, Mr. G, Mr. M and their coach Professor S are planning their way to Warsaw for the ACM-ICPC World Finals. Each of the four has a square-shaped suitcase with side length Ai (1<=i<=4) respectively. They want to pack their sui

划分树模板+模板题--hdu4251

题目链接:点击进入 划分树解决的是快速求区间中第k大值的问题,算法的主要思想是基于线段树和快排的划分方法,可以实现在logn时间内求出任意区间的第k大值.下面这份代码是基于hud4251的一份模板. 代码如下: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=100000+1000; in

hdu4417 Super Mario 树状数组离线/划分树

http://acm.hdu.edu.cn/showproblem.php?pid=4417 Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2720    Accepted Submission(s): 1322 Problem Description Mario is world-famous plumber

hdu 4417 Super Mario(离线树状数组|划分树)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2584    Accepted Submission(s): 1252 Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping a

HDU-4417-Super Mario(划分树+二分)

Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover. We regard the road to the boss's castle as a l