Qiuqiqiu  
不管道路多么崎岖坎坷,我永远不停下追逐梦想的脚步!

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

线段树,成段更新,区间最值

View Code
  1 #include <cstdio>
  2 #include <algorithm>
  3 using namespace std;
  4 
  5 #define lch (rt<<1)
  6 #define rch (rt<<1|1)
  7 const int N=100010;
  8 struct plank
  9 {
 10     int h,x,y,v;
 11     bool operator < (const plank &t)const
 12     {
 13         return h>t.h;
 14     }
 15 }e[N];
 16 struct node
 17 {
 18     int l,r;
 19     int max,c;
 20 }st[N*4];
 21 int max(int x,int y)
 22 {
 23     return x>y?x:y;
 24 }
 25 void build(int l,int r,int rt)
 26 {
 27     st[rt].l=l; st[rt].r=r;
 28     st[rt].max=0;
 29     st[rt].c=-1;
 30     if(l==r) return;
 31     int m=(l+r)/2;
 32     build(l,m,lch);
 33     build(m+1,r,rch);
 34 }
 35 void pushup(int rt)
 36 {
 37     st[rt].max=max(st[lch].max,st[rch].max);
 38 }
 39 void pushdown(int rt)
 40 {
 41     if(st[rt].c==-1) return;
 42     st[lch].c=st[rch].c=st[rt].c;
 43     st[lch].max=st[rch].max=st[rt].c;
 44     st[rt].c=-1;
 45 }
 46 void update(int a,int b,int x,int rt)
 47 {
 48     int l=st[rt].l, r=st[rt].r;
 49     if(a<=l && r<=b)
 50     {
 51         if(a==b && st[rt].max>=x) return;
 52         st[rt].c=x;
 53         st[rt].max=x;
 54         return;
 55     }
 56     pushdown(rt);
 57     int m=(l+r)/2;
 58     if(a<=m) update(a,b,x,lch);
 59     if(b>m) update(a,b,x,rch);
 60     pushup(rt);
 61 }
 62 int query(int a,int b,int rt)
 63 {
 64     int l=st[rt].l, r=st[rt].r;
 65     if(a<=l && r<=b) return st[rt].max;
 66     pushdown(rt);
 67     int m=(l+r)/2;
 68     int maxl=0,maxr=0;
 69     if(a<=m) maxl=query(a,b,lch);
 70     if(b>m) maxr=query(a,b,rch);
 71     return max(maxl,maxr);
 72 }
 73 int main()
 74 {
 75     int n;
 76     while(~scanf("%d",&n))
 77     {
 78         int maxx=0;
 79         for(int i=0;i<n;i++)
 80         {
 81             scanf("%d%d%d%d",&e[i].h,&e[i].x,&e[i].y,&e[i].v);
 82             if(e[i].y>maxx) maxx=e[i].y;
 83         }
 84         sort(e,e+n);
 85         e[0].v+=100;
 86         if(e[0].v<=0) {puts("-1"); continue;}
 87         build(0,maxx+1,1);
 88         update(e[0].x,e[0].x,e[0].v,1);
 89         update(e[0].y,e[0].y,e[0].v,1);
 90         for(int i=1;i<n;i++)
 91         {
 92             int val=query(e[i].x,e[i].y,1);
 93             if(val>0) val+=e[i].v;
 94             update(e[i].x,e[i].y,0,1);
 95             if(val<=0) continue;
 96             update(e[i].x,e[i].x,val,1);
 97             update(e[i].y,e[i].y,val,1);
 98         }
 99         printf("%d\n",st[1].max?st[1].max:-1);
100     }
101     return 0;
102 }

 

 

posted on 2012-04-10 20:11  Qiuqiqiu  阅读(121)  评论(0编辑  收藏  举报