HDU 3016 man down (line segment tree + dp)

Source: Internet
Author: User

Question:

Is a man under the general layer... There is nothing to say.

Note that only vertical drops are allowed.


Train of Thought Analysis:

The process of finding the maximum value is easy to think of as a DP process.

Because each plane can only be in two States from the left to the right.

The problem we need to solve is how to quickly know where to go to the left and where to go to the right.

This is the preprocessing of the Line Segment tree.

The lecture line segments are sorted by height.

Then add the data to the tree from small to large.

Then, find the number of the line segment that has been recently covered by the left and right endpoints.


#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#define lson num<<1,s,mid#define rson num<<1|1,mid+1,e#define mid ((s+e)>>1)#define maxn 100005using namespace std;struct Line{    int h,st,ed,v;    bool operator < (const Line &cmp)const    {        return h<cmp.h;    }}scline[maxn];int cov[maxn<<2];int ldn[maxn];int rdn[maxn];void pushdown(int num){    if(cov[num])    {        cov[num<<1]=cov[num<<1|1]=cov[num];        cov[num]=0;    }}int query(int num,int s,int e,int pos){    if(cov[num])return cov[num];    if(s==e)return 0;    if(pos<=mid)return query(lson,pos);    else return query(rson,pos);}void update(int num,int s,int e,int l,int r,int val){    if(l<=s && r>=e){        cov[num]=val;        return;    }    pushdown(num);    if(l<=mid)update(lson,l,r,val);    if(r>mid)update(rson,l,r,val);}int dp[maxn];int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        int m=0;n++;        scline[1].h=0,scline[1].st=1,scline[1].ed=maxn-1,scline[1].v=0;        for(int i=2;i<=n;i++)        {            scanf("%d%d%d%d",&scline[i].h,&scline[i].st,&scline[i].ed,&scline[i].v);        }        m=maxn-1;        sort(scline+1,scline+1+n);        memset(cov,0,sizeof cov);        for(int i=1;i<=n;i++)        {            ldn[i]=query(1,1,m,scline[i].st);            rdn[i]=query(1,1,m,scline[i].ed);            update(1,1,m,scline[i].st,scline[i].ed,i);        }        for(int i=1;i<maxn;i++)dp[i]=-0x3f3f3f3f;        dp[n]=100+scline[n].v;        for(int i=n;i>=1;i--)        {            if(dp[i]>0)dp[ldn[i]]=max(dp[ldn[i]],dp[i]+scline[ldn[i]].v);            if(dp[i]>0)dp[rdn[i]]=max(dp[rdn[i]],dp[i]+scline[rdn[i]].v);        }        if(dp[1]>0)printf("%d\n",dp[1]);        else puts("-1");    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.