标签:des style blog http color java os io
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17124 Accepted Submission(s): 8547
成段更新(通常这对初学者来说是一道坎),需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候
代码:
1 #include<cstring> 2 #include<cstdio> 3 const int maxn=100005; 4 struct node 5 { 6 int lef,rig,sum; 7 int cnt; 8 int mid(){ 9 return lef+(rig-lef>>1); 10 } 11 }; 12 13 node sac[maxn*3]; 14 15 void Build(int left,int right,int pos) 16 { 17 sac[pos]=(node){left,right,1}; 18 if(left==right)return ; 19 int mid=sac[pos].mid(); 20 Build(left,mid,pos<<1); 21 Build(mid+1,right,pos<<1|1); 22 sac[pos].sum=sac[pos<<1].sum+sac[pos<<1|1].sum; 23 } 24 void Update(int left,int right,int pos,int val) 25 { 26 if(left<=sac[pos].lef&&sac[pos].rig<=right){ 27 sac[pos].sum=val*(sac[pos].rig-sac[pos].lef+1); 28 sac[pos].cnt=val; 29 return ; 30 } 31 if(sac[pos].cnt!=0){ //向下更新一次 32 sac[pos<<1].sum=sac[pos].cnt*(sac[pos<<1].rig-sac[pos<<1].lef+1); 33 sac[pos<<1|1].sum=sac[pos].cnt*(sac[pos<<1|1].rig-sac[pos<<1|1].lef+1); 34 sac[pos<<1|1].cnt=sac[pos<<1].cnt=sac[pos].cnt; 35 sac[pos].cnt=0; 36 } 37 int mid=sac[pos].mid(); 38 if(mid>=left) 39 Update(left,right,pos<<1,val); 40 if(mid<right) 41 Update(left,right,pos<<1|1,val); 42 sac[pos].sum=sac[pos<<1].sum+sac[pos<<1|1].sum; 43 } 44 int main() 45 { 46 int test,n,Q; 47 int a,b,c; 48 scanf("%d",&test); 49 for(int i=1;i<=test;i++){ 50 scanf("%d%d",&n,&Q); 51 Build(1,n,1); 52 while(Q--) 53 { 54 scanf("%d%d%d",&a,&b,&c); 55 Update(a,b,1,c); 56 } 57 printf("Case %d: The total value of the hook is %d.\n",i,sac[1].sum); 58 } 59 }
hdu-------(1698)Just a Hook(线段树区间更新),布布扣,bubuko.com
hdu-------(1698)Just a Hook(线段树区间更新)
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/gongxijun/p/3898538.html