hdu 3016 Man Down(成段更新,单点查询+简单dp)

Man Down

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1761    Accepted Submission(s): 630


Problem 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
 

Source
 

题意:有N块木板,每块木板有四个属性,高h(h>0),左边界xl,右边界xr(0<xl<xr<100000),以及掉落在它上面,获得多少生命值value(-1000<value<1000),一个人从最高的木板开始往下跳,初始时生命值为100,问最后掉落到地面能获得的生命值最多为多少(如果生命值为0,那么这个人会死去),如果无法跳到地面,输出-1。

 
思路:这是一个简单的DP或者说最长路。但是木板的数目很大,如果建立每个木板到其他木板的转移用O(N^2)的复杂度,显然是会超时的,这时候,就可以借助线段树。将木板按h从小到大排序,初始时,更新线段树里的全部节点为地面的下标,然后往上添加木板的过程就是,单点查询每个木板的xl和xr,得到的下标就是当前木板能转移到的其他木板(或者地面),然后将这段区间更新为当前木板的下标。最后跑一个dp或者最长路都可以。
 
代码:
#include<cmath>
#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;

#define ls ( o << 1 )
#define rs ( o << 1 | 1 )
#define lson ls , l , mid
#define rson rs , mid + 1 , r
#define root 1 , 1 , n
#define rt o , l , r

#define LL long long
const int N = 100100;
const int inf = 100000000;
struct node
{
    int le,re,lazy;
    int id;
} s[N*4];

struct Line
{
    int h,l,r,w;
} line[N];

int cmp(Line a, Line b)
{
    return a.h<b.h;
}
void build (int o , int l , int r)
{
    s[o].le = l,s[o].re = r;
    s[o].id = 0;
    if (l == r)
    {
        return;
    }
    int mid = ( l + r ) >> 1;
    build(lson);
    build(rson);
}
void update(int o , int l,int r, int id)
{
    if(r < s[o].le || s[o].re < l)
    {
        return ;
    }
    if(l <= s[o].le && s[o].re <= r)
    {
        s[o].id = id;
        return ;
    }
    if(s[o].id != -1)
    {
        s[ls].id = s[rs].id = s[o].id;
        s[o].id = -1;
    }
    update(ls, l, r, id);
    update(rs, l, r, id);
}
int query(int o, int x)
{
    if(s[o].id != -1 && s[o].le <= x && x <= s[o].re)//当查询的点在一个已经覆盖区间里的时候直接返回
        return s[o].id;

    int mid = ( s[o].le + s[o].re ) >> 1;
    if(x <= mid) return query(ls, x);
    else
        return query(rs, x);
}

int lu[N][2],dp[N];
int main()
{
    //freopen("in.txt","r",stdin);
    int n, fan;
    while(~scanf("%d",&n))//题目要求多组输入..我竟然还单组输入..wrong了N次..
    {
        fan = 0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d%d%d",&line[i].h,&line[i].l,&line[i].r,&line[i].w);
            fan = max(fan,line[i].r);
        }
        build(1,1,fan);
        sort(line+1,line+n+1,cmp);
        for(int i=1; i<=n; i++)
        {
            lu[i][0]=query(1, line[i].l);
            lu[i][1]=query(1, line[i].r);
            update(1, line[i].l, line[i].r, i);
        }
        memset(dp,0,sizeof(dp));
        dp[n] = 100 + line[n].w;
        for(int i=n; i>0; i--)
        {
            if(dp[i])
            {
                dp[lu[i][0]] = max(dp[lu[i][0]],dp[i]+line[lu[i][0]].w);
                dp[lu[i][1]] = max(dp[lu[i][1]],dp[i]+line[lu[i][1]].w);
            }
        }
        if(dp[0]>0)
            printf("%d\n",dp[0]);
        else
            puts("-1");
    }
    return 0;
}
View Code
posted @ 2015-02-23 22:33  Doli  阅读(162)  评论(0)    收藏  举报