HDU 4417 Super Mario 划分树+二分

来源:互联网 发布:淘宝企业店铺怎么收费 编辑:程序博客网 时间:2024/06/08 05:21
Super MarioTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4939    Accepted Submission(s): 2262Problem DescriptionMario 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 line (the length is n), on every integer point i there is a brick on height hi. Now the question is how many bricks in [L, R] Mario can hit if the maximal height he can jump is H.InputThe first line follows an integer T, the number of test data.For each test data:The first line contains two integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, m is the number of queries.Next line contains n integers, the height of each brick, the range is [0, 1000000000].Next m lines, each line contains three integers L, R,H.( 0 <= L <= R < n 0 <= H <= 1000000000.)OutputFor each case, output "Case X: " (X is the case number starting from 1) followed by m lines, each line contains an integer. The ith integer is the number of bricks Mario can hit for the ith query.Sample Input110 100 5 2 7 5 4 3 8 7 7 2 8 63 5 01 3 11 9 40 1 03 5 55 5 14 6 31 5 75 7 3Sample OutputCase 1:4003120151Source2012 ACM/ICPC Asia Regional Hangzhou OnlineRecommendliuyiding   |   We have carefully selected several similar problems for you:  5877 5876 5875 5873 5872 
来源: http://acm.hdu.edu.cn/showproblem.php?pid=4417#include <cstdio>#include <iostream>#include <cmath>#include <algorithm>#include <cstring>#include <string>#include <cstdlib>#include <set>#include <queue>#include <vector>#include <map>using namespace std;typedef long long LL;#define MAXN 120000int Sorted[MAXN];int Tree[30][MAXN];int Toleft[30][MAXN];void Build(int Lef,int Rig,int dep){    if(Lef == Rig) return;    int Mid = (Lef+Rig)>>1;    int Same = Mid - Lef + 1;    for(int i=Lef;i<=Rig;i++)        if(Tree[dep][i]<Sorted[Mid]) Same--;    int Lpos = Lef;    int Rpos = Mid+1;    for(int i=Lef;i<=Rig;i++)    {        if(Tree[dep][i]<Sorted[Mid])            Tree[dep+1][Lpos++] = Tree[dep][i];        else if(Tree[dep][i]==Sorted[Mid] && Same>0)        {            Tree[dep+1][Lpos++] = Tree[dep][i];            Same--;        }        else Tree[dep+1][Rpos++] = Tree[dep][i];        Toleft[dep][i] = Toleft[dep][Lef-1]+Lpos-Lef;    }    Build(Lef,Mid,dep+1);    Build(Mid+1,Rig,dep+1);}int Query(int Lef,int Rig,int l,int r,int dep,int K){    if(l==r) return Tree[dep][l];    int Mid = (Lef+Rig)>>1;    int Cnt = Toleft[dep][r] - Toleft[dep][l-1];    if(Cnt>=K)    {        int newL = Lef + Toleft[dep][l-1] - Toleft[dep][Lef-1];        int newR = newL + Cnt -1;        return Query(Lef,Mid,newL,newR,dep+1,K);    }    else    {        int newR = r + Toleft[dep][Rig] - Toleft[dep][r];        int newL = newR - (r-l-Cnt);        return Query(Mid+1,Rig,newL,newR,dep+1,K-Cnt);    }}int main(){//    freopen("F:\\test.txt","r",stdin);//    freopen("F:\\tsst.txt","w",stdout);    int T;scanf("%d",&T);    for(int t=1,N,M;t<=T;t++)    {        printf("Case %d:\n",t);        scanf("%d %d",&N,&M);        for(int i=1;i<=N;i++)        {            scanf("%d",&Tree[0][i]);            Sorted[i]=Tree[0][i];        }        sort(Sorted+1,Sorted+N+1);        Build(1,N,0);        for(int i=1;i<=M;i++)        {            //printf("i == %d\n",i);            int a,b,H;            scanf("%d %d %d",&a,&b,&H);            int L = 1,R=b-a+1;            while(L<=R)            {               // printf("L %d,R %d\n",L,R);                int Mid = (R+L)>>1;                int Ans = Query(1,N,a+1,b+1,0,Mid);                if(Ans>H) R = Mid-1;                else L = Mid+1;            }            printf("%d\n",L-1);        }    }}
0 0
原创粉丝点击