HDU 1698 Just a Hook(线段树区间覆盖)

线段树基本操作练习,防手生

#include <cstdio>

#include <cstring>

#include <cstdlib>



#define lson l, m, rt << 1

#define rson m + 1, r, rt << 1 | 1

#define lc rt << 1

#define rc rt << 1 | 1



const int MAXN = 100100;



int sum[MAXN << 2];

int lazy[MAXN << 2];



void PushUp( int rt )

{

    sum[rt] = sum[lc] + sum[rc];

    return;

}



void PushDown( int rt, int m )

{

    if ( lazy[rt] )

    {

        lazy[lc] = lazy[rt];

        lazy[rc] = lazy[rt];



        /*这里注意写法,之前我传进来的是l, r, m

         *sum[lc] = lazy[rt]*( m - l + 1 );

         *sum[rc] = lazy[rt]*( r - m + 1 );

         *然后WA了

         **/

        sum[lc] = lazy[rt]*( m - ( m >> 1 ) );

        sum[rc] = lazy[rt]*( m >> 1 );



        lazy[rt] = 0;

    }

    return;

}



void build( int l, int r, int rt )

{

    lazy[rt] = 0;

    sum[rt] = 1;

    if ( l == r ) return;

    int m = ( l + r ) >> 1;

    build( lson );

    build( rson );

    PushUp(rt);

    return;

}



void Update( int L, int R, int c, int l, int r, int rt )

{

    if ( L <= l && r <= R )

    {

        lazy[rt] = c;

        sum[rt] = c * ( r - l + 1 );

        return;

    }



    int m = ( l + r ) >> 1;

    PushDown( rt, r - l + 1 );

    if ( L <= m ) Update( L, R, c, lson );

    if ( R > m  ) Update( L, R, c, rson );

    PushUp( rt );



    return;

}



int N, Q;



int main()

{

    int T, cas = 0;

    scanf( "%d", &T );

    while ( T-- )

    {

        scanf( "%d", &N );

        build( 1, N, 1 );

        scanf( "%d", &Q );

        while ( Q-- )

        {

            int a, b, c;

            scanf( "%d%d%d", &a, &b, &c );

            Update( a, b, c, 1, N, 1 );

        }

        printf("Case %d: The total value of the hook is %d.\n", ++cas, sum[1] );

    }

    return 0;

}

 

你可能感兴趣的:(HDU)