hdu 3016 Man Down

来源:互联网 发布:淘宝直通车基础 编辑:程序博客网 时间:2024/05/22 23:21

http://acm.hdu.edu.cn/showproblem.php?pid=3016

这个题做得比较爽,1Y,用线段树来优化的dp

用线段树来查询从当前点下落落到第几快木板上,方法是从下向上扫描,每次先查询再更新……其实可以不用离散化,因为x坐标的范围在0 到 100000之间

#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;const int maxn=200010;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int id[maxn<<2],l[maxn],r[maxn],tab[maxn],dp[maxn];struct Node{    int h,l,r,val;}p[maxn];void pushdown(int rt){    if(id[rt]!=-1){        id[rt<<1]=id[rt];        id[rt<<1|1]=id[rt];        id[rt]==-1;    }}void pushup(int rt){    if(id[rt<<1]==id[rt<<1|1]) id[rt]=id[rt<<1];    else id[rt]=-1;}void update(int L,int R,int val,int l,int r,int rt){    if(L<=l&&R>=r){        id[rt]=val;        return;    }    pushdown(rt);    int m=(l+r)>>1;    if(L<=m) update(L,R,val,lson);    if(R>m) update(L,R,val,rson);    pushup(rt);}int query(int p,int l,int r,int rt){    if(id[rt]!=-1) return id[rt];    int m=(l+r)>>1;    if(p<=m) return query(p,lson);    else return query(p,rson);}int cmp(Node a,Node b){    return a.h < b.h;}int bin(int key,int l,int r){    while(l<=r)    {        int m=(l+r)>>1;        if(tab[m]==key) return m;        if(tab[m]>key) r=m-1;        else l=m+1;    }}int main(){    int n,k;    while(scanf("%d",&n)==1)    {        p[0].h=0,p[0].l=0,p[0].r=100000,p[0].val=0;        tab[0]=0,tab[1]=100000;        k=1;        for(int i=1;i<=n;i++)           scanf("%d%d%d%d",&p[i].h,&p[i].l,&p[i].r,&p[i].val),tab[++k]=p[i].l,tab[++k]=p[i].r;        sort(p,p+n+1,cmp);        sort(tab,tab+k+1);        id[1]=0;        k=unique(tab,tab+k+1)-tab-1;        for(int i=1;i<=n;i++)        {            int pl=bin(p[i].l,0,k),pr=bin(p[i].r,0,k);            l[i]=query(pl,0,k,1);            r[i]=query(pr,0,k,1);            update(pl,pr,i,0,k,1);        }        memset(dp,-1,sizeof(dp));        dp[n]=100+p[n].val;        for(int i=n;i>=1;i--)        if(dp[i]>0)        {            dp[l[i]]=max(dp[l[i]],dp[i]+p[l[i]].val);            dp[r[i]]=max(dp[r[i]],dp[i]+p[r[i]].val);        }        printf("%d\n",dp[0]);    }    return 0;}


原创粉丝点击