Query the primary line segment tree in the update segment of HDOJ 1698

Source: Internet
Author: User

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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.