1. 程式人生 > >HDU:4251 The Famous ICPC Team Again(劃分樹)

HDU:4251 The Famous ICPC Team Again(劃分樹)

題意:給一組數字,多次查詢求中位數。

思路:劃分樹。

#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;
const int maxn=100005;
struct DivideTree
{
    int sorted[maxn],dat[20][maxn];
    int toleft[20][maxn];
    void build(int c,int L,int R)
    {
        int mid=(L+R)/2,lsame=mid+1-L,lp=L,rp=mid+1;
        for(int i=L; i<mid; ++i)
            if(sorted[i]<sorted[mid]) lsame--;
        for(int i=L; i<=R; ++i)
        {
            if(i==L)toleft[c][i]=0;
            else toleft[c][i]=toleft[c][i-1];
            if(dat[c][i]<sorted[mid])
            {
                dat[c+1][lp++]=dat[c][i];
                toleft[c][i]++;
            }
            else if(dat[c][i]>sorted[mid])
                dat[c+1][rp++]=dat[c][i];
            else
            {
                if(lsame)
                {
                    lsame--;
                    toleft[c][i]++;
                    dat[c+1][lp++]=sorted[mid];
                }
                else
                    dat[c+1][rp++]=sorted[mid];
            }


        }
        if(L==R) return ;
        build(c+1,L,mid);
        build(c+1,mid+1,R);
    }
    int query(int c,int L,int R,int ql,int qr,int k)
    {
        if(L==R) return dat[c][L];
        int mid=(L+R)/2;
        int la,lb,ra,rb;
        if(L==ql) la=0;
        else la=toleft[c][ql-1];
        lb=toleft[c][qr];
        ra=ql-L-la;
        rb=qr+1-L-lb;
        int s=lb-la;
        if(k<=s) return query(c+1,L,mid,L+la,L+lb-1,k);
        else return query(c+1,mid+1,R,mid+1+ra,mid+rb,k-s);
    }
};
DivideTree tree;
int main()
{
    int n,kase=0;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1; i<=n; ++i)
        {
            scanf("%d",&tree.dat[0][i]);
            tree.sorted[i]=tree.dat[0][i];
        }
        sort(tree.sorted+1,tree.sorted+n+1);
        tree.build(0,1,n);
        int m;
        scanf("%d",&m);
        printf("Case %d:\n",++kase);
        while(m--)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            printf("%d\n",tree.query(0,1,n,a,b,(b-a)/2+1));
        }
    }
    return 0;
}


相關推薦

HDU:4251 The Famous ICPC Team Again(劃分)

題意:給一組數字,多次查詢求中位數。 思路:劃分樹。 #include <iostream> #include <string> #include <cstring> #include <cctype> #include &l

hdu 5381 The sum of gcd(線段+gcd)

const HR LV oid pac vector AR statistic modify 題目鏈接:hdu 5381 The sum of gcd 將查詢離線

HDU - 5542 The Battle of Chibi(LIS+狀數組優化)

possible ould eof namespace chan panel arm int sam The Battle of Chibi Cao Cao made up a big army and was going to invade the whole Sout

hdu 5381 The sum of gcd(線段等差數列區間修改+單點查詢)

題意: 給出一個數組a,叫你每次詢問如下等式的值。 f(l,r)=∑ri=l∑rj=igcd(ai,ai+1....aj) 解析: 思考了很久終於理解了學長的思路 給你一個序列,這個序列的子序列gcd的個數不會超過logN個(N為

HDU 4251 劃分

/* 劃分樹:解題型別單一,給出10^5個點,給你 q 個查詢,每次查詢出[l,r] 中第 k 大的值。 劃分樹:                                 主系列  4 3 2 5 8 7 6 1 9                        

HDU 3591 The trouble of Xiaoqian(多重背包+全然背包)

給他 cas 維數 color cost 代碼 01背包 size code HDU 3591 The trouble of Xiaoqian(多重背包+全然背包) http://acm.hdu.edu.cn/showproblem.php?pid=3591 題意:

hdu 5381 The sum of gcd 2015多校聯合訓練賽#8莫隊算法

names 來看 efi nbsp span ems multipl script there The sum of gcd Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K

hdu 2665 Kth number(劃分

first con build 這一 cst second class stream tom Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth

hdu 1679 The Unique MST (克魯斯卡爾)

plm () pst cati 卡爾 nts 相等 cat get The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24152 Accepted:

hdu 4717 The Moving Points(三分)

fine names -- sqrt blank .cn class 題目 col 題目鏈接:hdu 4717 The Moving Points 題意: 在二維平面上有n個點,每個點給出移動的方向和速度。 問在某個時刻,這些點中最大距離最小是多少,輸出時刻和距離。 題解:

hdu 1867 A + B for you again

roc stdio.h 相同 tween them substr bsp sdf des A + B for you again Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (

hdu 4901 The Romantic Hero 計數dp,位計算

height accep fin -a can stop ott from include The Romantic Hero Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072

POJ 3653 &amp; ZOJ 2935 &amp; HDU 2722 Here We Go(relians) Again(最短路dijstra)

tracking spec else condition lds mina switch comm scan 題目鏈接: PKU:http://poj.org/problem?id=3653 ZJU:problemId=1934" target="_blan

hdu 3861 The King’s Problem trajan縮點+二分圖匹配

acm ant 二分 scanf title atan size pair city The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768

HDU 4891 The Great Pan (字符串處理)

scan ref return eof 意思 ng- length count con 題目鏈接:HDU 4891 The Great Pan 求一串字符有多少種不同的意思,當中關心‘{’,‘}’之間的‘|’。和‘$‘,‘$‘之間的空格,連續N個空&

HDU 4260(The End of The World-Hanoi塔從中間狀態移動)

ctype read finish car ann bbb over lines 遞歸 The End of The World Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/327

HDU 4489 The King’s Ups and Downs

stdin algo pll class tar mem blank mes nbsp http://acm.hdu.edu.cn/showproblem.php?pid=4489 題意:有n個身高不同的人,計算高低或低高交錯排列的方法數。 思路:可以按照身高順序

HDU - 4422-The Little Girl who Picks Mushrooms

pac names pic 兩個 can break hdu tle http 題目鏈接:https://vjudge.net/problem/HDU-4422 題目大意: 自行百度 題目分析: 當n<=3的時候,易得可以得到的最多的蘑菇是1024。 當n>3時

hdu 5443 The Water Problem(長春網絡賽——暴力)

targe int hdu align follow limit eight math ould 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=5443 The Water Problem Time L

HDU 2105: The Center of Gravity

using int date amp pan space bit sync double The Center of Gravity[email protected]/* */ Sycamore, ZJNU; [email protected]/*