码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1698 Just a Hook (区间更新)

时间:2015-07-22 12:23:02      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:

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 1698 Just a Hook (区间更新)

标签:

原文地址:http://www.cnblogs.com/yong-hua/p/4666549.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!