题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3016
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct Board { int h; int l; int r; int v; }; struct Line { int l; int r; }; struct node { int s; int e; int f; }; Board a[2000000]; node tr[1000000]; Line l[4000000]; int dp[2000000]; bool cmp(Board a,Board b) { return a.h<b.h; } void build_tree(int c,int s,int e) { tr[c].s=s; tr[c].e=e; tr[c].f=0; if (s == e) return ; build_tree(c<<1,s,(s+e)>>1); build_tree(c<<1|1,((s+e)>>1)+1,e); } void update(int c,int s,int e,int f) { if (tr[c].s > e || s > tr[c].e) return ; if (tr[c].s >= s && tr[c].e <= e) { tr[c].f=f; return ; } if (tr[c].f != -1) { tr[c<<1].f=tr[c].f; tr[c<<1|1].f=tr[c].f; tr[c].f=-1; } update(c<<1,s,e,f); update(c<<1|1,s,e,f); } int query(int c,int v) { if (tr[c].f != -1 && tr[c].s <= v && tr[c].e >= v) return tr[c].f; if (v > (tr[c].s+tr[c].e)>>1) return query(c<<1|1,v); else return query(c<<1,v); } int main() { int n,lt,i,ma; while (scanf("%d",&n) != EOF) { ma=0; for (i=1;i<=n; i++) { scanf("%d%d%d%d",&a[i].h,&a[i].l,&a[i].r,&a[i].v); if(a[i].l>ma) ma=a[i].l; if(a[i].r>ma) ma=a[i].r; } lt=0; sort(a+1,a+n+1,cmp); build_tree(1,1,ma); for (i=1; i<=n; i++) { l[i].l=query(1,a[i].l); l[i].r=query(1,a[i].r); update(1,a[i].l,a[i].r,i); } memset(dp,0,sizeof(dp)); dp[n]=100+a[n].v; for (i=n; i>=1; i--) { if (dp[i] != 0) { dp[l[i].l]=dp[l[i].l]>dp[i]+a[l[i].l].v?dp[l[i].l]:dp[i]+a[l[i].l].v; dp[l[i].r]=dp[l[i].r]>dp[i]+a[l[i].r].v?dp[l[i].r]:dp[i]+a[l[i].r].v; } } if (dp[0] <= 0) printf("-1\n"); else printf("%d\n",dp[0]); } }