In the blog of the NOS gods
This is usually a hurdle for beginners.
Scared me not to look at the line tree for three days = because I am a newbie .....
Before going to bed last night, I want to understand the updated line segment tree of the segment query section. The result is also true;
This line segment tree uses the delay mark and Its Operation PushDown;
The latency mark indicates the value of the current interval.
PushDown is used to push down the current point if there is a delay mark, and the rear is zero.
#include<iostream>#define MAXN 100001using namespace std;int sum[ MAXN<<2 ];int sign[ MAXN<<2 ];void PushUp( int rt ){ sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void PushDown( int rt,int m ){ if( sign[rt] ) { sign[rt<<1]=sign[rt<<1|1]=sign[rt]; sum[rt<<1]=(m-(m>>1))*sign[rt]; sum[rt<<1|1]=(m>>1)*sign[rt]; sign[rt]=0; } return ;}void build( int l,int r,int rt ){ sign[rt]=0; sum[rt]=1; if( l==r )return ; int m=(l+r)>>1; build( l,m,rt<<1 ); build( m+1,r,rt<<1|1 ); PushUp(rt); return ;}void update( int l,int r,int L,int R,int value,int root ){ if( l<=L&&R<=r ) { sign[root]=value; sum[root]=value*(R-L+1); return ; } PushDown( root,R-L+1 ); int m=( L+R )>>1; if( l<=m )update( l,r,L,m,value,root<<1 ); if( r>m )update( l,r,m+1,R,value,root<<1|1 ); PushUp(root); return ;}int main(){ int loop; int cas=1; scanf( "%d",&loop ); while( loop-- ) { int size,n,i; scanf( "%d %d",&size,&n ); build( 1,size,1 ); while( n-- ) { int a,b,c; scanf( "%d %d %d",&a,&b,&c ); update( a,b,1,size,c,1); } printf("Case %d: The total value of the hook is %d.\n",cas++ , sum[1]); } return 0; }