zoukankan      html  css  js  c++  java
  • hdu 3016 Man Down(线段树)

    Description

    The Game “Man Down 100 floors” is an famous and interesting game.You can enjoy the game from 
    http://hi.baidu.com/abcdxyzk/blog/item/16398781b4f2a5d1bd3e1eed.html 

    We take a simplified version of this game. We have only two kinds of planks. One kind of the planks contains food and the other one contains nails. And if the man falls on the plank which contains food his energy will increase but if he falls on the plank which contains nails his energy will decrease. The man can only fall down vertically .We assume that the energy he can increase is unlimited and no borders exist on the left and the right. 

    First the man has total energy 100 and stands on the topmost plank of all. Then he can choose to go left or right to fall down. If he falls down from the position (Xi,Yi),he will fall onto the nearest plank which satisfies (xl <= xi <= xr)(xl is the leftmost position of the plank and xr is the rightmost).If no planks satisfies that, the man will fall onto the floor and he finishes his mission. But if the man’s energy is below or equal to 0 , he will die and the game is Over. 

    Now give you the height and position of all planks. And ask you whether the man can falls onto the floor successfully. If he can, try to calculate the maximum energy he can own when he is on the floor.(Assuming that the floor is infinite and its height is 0,and all the planks are located at different height).
     

    Input

    There are multiple test cases. 

    For each test case, The first line contains one integer N (2 <= N <= 100,000) representing the number of planks. 

    Then following N lines representing N planks, each line contain 4 integers (h,xl,xr,value)(h > 0, 0 < xl < xr < 100,000, -1000 <= value <= 1000), h represents the plank’s height, xl is the leftmost position of the plank and xr is the rightmost position. Value represents the energy the man will increase by( if value > 0) or decrease by( if value < 0) when he falls onto this plank.
     

    Output

    If the man can falls onto the floor successfully just output the maximum energy he can own when he is on the floor. But if the man can not fall down onto the floor anyway ,just output “-1”(not including the quote)
     

    Sample Input

    4
    10 5 10 10
    5 3 6 -100
    4 7 11 20
    2 2 1000 10
     

    Sample Output

    140
     
     
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int dp[100005];
    struct p1
    {
        int h,l,r,v;
        int lv,rv;
    };p1 s[100005];
    bool com(p1 a,p1 b)
    {
        return a.h<b.h;
    }
    struct p2
    {
        int l,r,v;
    };p2 tree[1000000];
    void build(int l,int r,int p)
    {
        tree[p].l=l;tree[p].r=r;
        tree[p].v=0;
        if (l==r) return ;
        int m=(l+r)/2;
        build(l,m,p*2);
        build(m+1,r,p*2+1);
    }
    void un(int l,int r,int p,int x)
    {
        if (tree[p].l==l&&tree[p].r==r)
        {
            tree[p].v=x;
            return ;
        }
        if (tree[p].v!=-1)
        {
            tree[p*2].v=tree[p*2+1].v=tree[p].v;
            tree[p].v=-1;
        }
        int m=(tree[p].l+tree[p].r)/2;
        if (l>m) un(l,r,p*2+1,x);
        else if (m>=r) un(l,r,p*2,x);
        else
        {
            un(l,m,p*2,x);
            un(m+1,r,p*2+1,x);
        }
    }
    int find(int x,int p)
    {
        if (tree[p].v!=-1) return tree[p].v;
        int m=(tree[p].l+tree[p].r)/2;
        if (x<=m) return find(x,p*2);
        return find(x,p*2+1);
    }
    int main()
    {
        int n,i,mx;
        while (~scanf("%d",&n))
        {
            mx=-1;
            for (i=1;i<=n;i++)
            {
                scanf("%d%d%d%d",&s[i].h,&s[i].l,&s[i].r,&s[i].v);
                if (mx<s[i].r) mx=s[i].r;
            }
            build(1,mx,1);
            memset(dp,0,sizeof(dp));
            sort(s+1,s+n+1,com);
            for (i=1;i<=n;i++)
            {
                s[i].lv=find(s[i].l,1);
                s[i].rv=find(s[i].r,1);
                un(s[i].l,s[i].r,1,i);
            }
            s[0].v=0;
            dp[n]=100+s[n].v;
            for (i=n;i>0;i--)
            {
                if (dp[i]<=0) continue;
                dp[s[i].lv]=max(dp[s[i].lv],dp[i]+s[s[i].lv].v);
                dp[s[i].rv]=max(dp[s[i].rv],dp[i]+s[s[i].rv].v);
            }
            if (dp[0]<=0) printf("-1
    ");
            else printf("%d
    ",dp[0]);
        }
    }
    
       
  • 相关阅读:
    View onMeasure方法介绍
    控件的3个状态
    多个Activity之间的跳转(1)
    7种形式的Android Dialog使用举例(下)
    调用摄像头采集图像
    读取播放视频
    MATLAB
    下载数据CSV文件格式
    生成数据
    java中给当前时间添加一小时
  • 原文地址:https://www.cnblogs.com/pblr/p/4747866.html
Copyright © 2011-2022 走看看