hdu 1698 Just a Hook (区间更新)

Problem : 1698 ( Just a Hook )     Judge Status : Accepted

 Language : G++    Author : dayang



#include<iostream>

#include<cstdio>

#include<cstring>

#define MID(a,b)  ((a + b) >> 1)

#define lson l,m,rt<<1

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



using namespace std;



const int MAXN = 100000;

int sum[MAXN << 2];

int col[MAXN << 2];



void PushUp(int rt)

{

    sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];

}



void PushDown(int x,int rt)

{

    if(col[rt])

    {

        col[rt<<1] = col[rt<<1|1] = col[rt];

        sum[rt<<1] = (x-(x>>1))*col[rt];

        sum[rt<<1|1] = (x>>1)*col[rt];

        col[rt] = 0;

    }

}



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

{

    col[rt] = 0;

    if(l == r)

    {

        sum[rt] = 1;

        return;

    }

    int m = MID(l,r);

    Build(lson);

    Build(rson);

    PushUp(rt);

}



void UpDate(int A,int B,int Z,int l,int r,int rt)

{

    if(A<=l&&r<=B)

    {

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

        col[rt] = Z;

        return;

    }

    PushDown(r-l+1,rt);

    int m = MID(l, r);

    if(A<=m)

        UpDate(A,B,Z,lson);

    if(B>m)

        UpDate(A,B,Z,rson);

    PushUp(rt);

}



int main()

{

    int T,N,M,k = 1;

    scanf("%d",&T);

    while(T--)

    {

        int A,B,Z;

        scanf("%d",&N);

        Build(1,N,1);

        scanf("%d",&M);

        while(M--)

        {

            scanf("%d%d%d",&A,&B,&Z);

            UpDate(A,B,Z,1,N,1);

        }

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

    }

}

 

你可能感兴趣的:(HDU)