hdu 4107 Gangster

题意:给定区间,每次对一个区间加一个值,如果区间中某个节点的值大于等于P的话,则加的值乘二

做法:线段树,维护当前区间的最大值、最小值、标记值

代码基本上都是照着别人的敲的。

http://blog.csdn.net/zxy_snow/article/details/6919762

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <vector>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <utility>
  7 #include <cstring>
  8 #include <fstream>
  9 #include <string>
 10 
 11 #define L(x) ( x << 1 )  
 12 #define R(x) ( x << 1 | 1 )  
 13 using namespace std;
 14 
 15 const int MAXN=200005 ;//区间长度
 16 const int MARK=-65535;//标记,必要时可以有多个
 17 struct tnode
 18 {
 19     int mark,l,r;//mark=-65535 为未标记状态
 20     int mid(){    return (l+r)/2;}
 21     int len(){    return (r-l);}
 22     bool in(int ll,int rr) { return l >= ll && r <= rr; }
 23     int min,max;
 24     //bool doubledamage;
 25     //自行添加特殊数据:{lmax,rmax,max,num...}
 26 };
 27 //左闭右开区间
 28 tnode node[MAXN<<2];
 29 int size,p;
 30 void build(int f,int l,int r)
 31 {
 32     node[f].l=l;
 33     node[f].r=r;
 34     node[f].mark=0;
 35     node[f].min=node[f].max=0;
 36     if(node[f].len()==1)
 37     {
 38         //初始化单位元素;
 39         return;
 40     }
 41     int mid=node[f].mid();
 42     build(f*2,l,mid);
 43     build(f*2+1,mid,r);
 44     
 45 }
 46 void pushdown(int t)
 47 {
 48     if( node[t].mark )
 49     {
 50         //为什么不需要更新node[t].max,min?因为当max,min与mark同时更新
 51         node[L(t)].mark += node[t].mark;
 52         
 53             node[L(t)].min += node[t].mark;
 54             node[L(t)].max += node[t].mark;
 55         node[R(t)].mark += node[t].mark;
 56             node[R(t)].min += node[t].mark;
 57             node[R(t)].max += node[t].mark;
 58         
 59         
 60         node[t].mark = 0;
 61     }
 62 }
 63 void modify(int f,int l,int r,int value)
 64 {
 65     if(node[f].in(l,r) )
 66     {
 67         //修改value
 68         //根据value 更改标记
 69         if(node[f].min>=p)
 70         {
 71             node[f].min+=2*value;
 72             node[f].max+=2*value;
 73             node[f].mark+=2*value;
 74             return ;
 75         }
 76         if(node[f].max<p)
 77         {
 78             node[f].min+=value;
 79             node[f].max+=value;
 80             node[f].mark+=value;
 81             return ;
 82         }
 83     }
 84     if(node[f].len()==1)
 85             return ;
 86     pushdown(f);
 87     //int mid=node[f].mid();
 88 //    if(r<node[L(f)].r)    modify(L(f),l,r,value);
 89 //    else    if(l>=node[R(f)].l)    modify(R(f),l,r,value);
 90 //    else{
 91 //        modify(L(f),l,node[L(f)].r,value);
 92 //        modify(R(f),node[R(f)].l,r,value);
 93 //    }
 94     int mid = node[f].mid();
 95     if( l < mid ) modify(L(f),l,r,value);
 96     if( r > mid ) modify(R(f),l,r,value);
 97     node[f].min=min(node[L(f)].min,node[R(f)].min);
 98     node[f].max=max(node[L(f)].max,node[R(f)].max);
 99 }
100 void query(int f)
101 {
102     if(node[f].min==node[f].max)
103     {
104         for(int i=node[f].l;i<node[f].r;i++)
105             printf(i == size-1 ? "%d\n" : "%d ", node[f].min);
106         return ;
107     }
108     if(node[f].len()==1)
109         return ;
110     pushdown(f);
111     query(L(f));
112     query(R(f));
113 }
114 int main(int argc, char* argv[])
115 {
116     int m, x, y, val;
117     
118     while( scanf("%d%d%d", &size, &m, &p)!=EOF )
119     {
120         build(1, 0, size);
121         while( m-- )
122         {
123             scanf("%d%d%d", &x, &y, &val);
124             modify(1, x-1, y, val);
125         }
126         query(1);
127     }
128 }
posted @ 2012-05-03 16:12  jzlikewei  阅读(279)  评论(0编辑  收藏  举报