线段树+DP+hdu3016

F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
      C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests
Author  lee
Mail Mail 0(0)
Control Panel  Control Panel  
Sign Out  Sign Out

不熟悉BestCoder的比赛?看看这里—>《BestCoder用户手册》下载链接

Man Down

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


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
线段树+DP+hdu3016_第1张图片

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
2009 Multi-University Training Contest 12 - Host by FZU

思路:按照高度,从小到大排序,然后每次先查询当前线段做右端点可以下落到哪条线段,然后跟新本条线段。然后进行一个简单的dp,求的答案

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn=100100;
int n;
int dp[maxn];

struct node
{
    int xl,xr,val,id,h;
    int l,r;
    bool operator < (const node & a)const
    {
        return h>1;
        build(o<<1,l,mid);
        build(o<<1|1,mid+1,r);
    }

    void pushdown(int o)
    {
        if(id[o]!=-1)
        {
            id[o<<1]=id[o<<1|1]=id[o];
            id[o]=-1;
        }
    }
    void update(int o,int l,int r,int q1,int q2,int pos)
    {

        if(l>r)return;
        if(q1<=l&&r<=q2)
        {
            id[o]=pos;
            return;
        }
        pushdown(o);
        int mid=(l+r)>>1;
        if(q1<=mid)update(o<<1,l,mid,q1,q2,pos);
        if(q2>mid)update(o<<1|1,mid+1,r,q1,q2,pos);
    }
    int query(int o,int l,int r,int pos)
    {
        if((id[o]!=-1)&&pos>=l&&pos<=r)return id[o];
        pushdown(o);
        int mid=(l+r)>>1;
        if(pos<=mid)return query(o<<1,l,mid,pos);
        else return query(o<<1|1,mid+1,r,pos);
    }
}tree;

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        int len=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d%d",&p[i].h,&p[i].xl,&p[i].xr,&p[i].val);
            p[i].id=i;
            len=max(len,p[i].xr);
        }
        sort(p+1,p+n+1);
        tree.build(1,1,len);
        for(int i=1;i<=n;i++)
        {
            p[i].l=tree.query(1,1,len,p[i].xl);
            p[i].r=tree.query(1,1,len,p[i].xr);
            tree.update(1,1,len,p[i].xl,p[i].xr,i);
        }
        memset(dp,0,sizeof(dp));
        dp[n]=100+p[n].val;
        for(int i=n;i>=1;i--)
        {
            int x=p[i].l,y=p[i].r;
            if(dp[i])
            {
                dp[x]=max(dp[x],dp[i]+p[p[i].l].val);
                dp[y]=max(dp[y],dp[i]+p[p[i].r].val);
            }
        }
        if(dp[0]>0)printf("%d\n",dp[0]);
        else printf("-1\n");
    }
    return 0;
}




你可能感兴趣的:(树状数组/线段树)