hdu-3016-Man Down(线段树)

来源:互联网 发布:osi网络模型 编辑:程序博客网 时间:2024/05/22 05:24

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
410 5 10 105 3 6 -1004 7 11 202 2 1000 10
 

Sample Output
140
 

Source
2009 Multi-University Training Contest 12 - Host by FZU

代码:

#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>#include<map>using namespace std;typedef long long ll;#define M 100005#define inf 0x3f3f3f3f#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int tree[M<<2],dp[M][2];int n;struct node{    int h,l,r,v;    bool operator <(const node & obj) const    {        return h>obj.h;    }}blank[M];inline void pushdown(int rt) //懒惰标记,用父节点更新子节点{    if(tree[rt]!=-1)    {        tree[rt<<1]=tree[rt<<1|1]=tree[rt];        tree[rt]=-1;    }}void update(int ql,int qr,int v,int l,int r,int rt){    if(ql<=l&&qr>=r)    {        tree[rt]=v;        return;    }    pushdown(rt);    int m=(l+r)>>1;    if(qr<=m)        update(ql,qr,v,lson);    else if(ql>m)        update(ql,qr,v,rson);    else    {        update(ql,qr,v,lson);        update(ql,qr,v,rson);    }}int query(int pos,int l,int r,int rt){    if(l==r)    {        return tree[rt];    }    pushdown(rt);    int m=(l+r)>>1;    if(pos<=m)        return query(pos,lson);    else        return query(pos,rson);}int main(){    int i;    while(scanf("%d",&n)!=EOF)    {        memset(tree,-1,sizeof(tree)); //注意数组是一个[l,r]的区间        memset(dp,-1,sizeof(dp));        int l=inf,r=-inf;        for(i=1;i<=n;i++)        {            scanf("%d%d%d%d",&blank[i].h,&blank[i].l,&blank[i].r,&blank[i].v);            l=min(l,blank[i].l);            r=max(r,blank[i].r);        }        sort(blank+1,blank+1+n); //将木板从高到的低排序        for(i=n;i>=1;i--)  //这里用逆推,从低到上求值        {            int j=query(blank[i].l,l,r,1); //j为点blank[i].l下方木板的下标            if(j!=-1) //下方有木板,那么下降到木板j时,选着从j的哪一端下降                dp[i][0]=max(dp[j][0],dp[j][1])+blank[j].v;            else  //下方为空,直接降落到地面                dp[i][0]=0;            j=query(blank[i].r,l,r,1);            if(j!=-1)                dp[i][1]=max(dp[j][0],dp[j][1])+blank[j].v;            else                dp[i][1]=0;            update(blank[i].l,blank[i].r,i,l,r,1); //为板blank[i]赋下标为i        }        int ans=max(dp[1][0],dp[1][1])+100+blank[1].v;        if(ans<=0) ans=-1;        printf("%d\n",ans);    }    return 0;}