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

HDU 1698 Just a Hook(线段树区间替换)

时间:2014-08-09 11:39:07      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:acm   c语言   算法   编程   线段树   

题目地址:HDU 1698

区间替换裸题。同样利用lazy延迟标记数组,这里只是当lazy下放的时候把下面的lazy也全部改成lazy就好了。

代码如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
const int MAXN=1e5+10;
int sum[MAXN<<3], lazy[MAXN<<3];
void PushUp(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void PushDown(int rt, int m)
{
    if(lazy[rt])
    {
        lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
        sum[rt<<1]=lazy[rt]*(m-(m>>1));
        sum[rt<<1|1]=lazy[rt]*(m>>1);
        lazy[rt]=0;
    }
}
void build(int l, int r, int rt)
{
    lazy[rt]=0;
    if(l==r)
    {
        sum[rt]=1;
        return ;
    }
    int mid=l+r>>1;
    build(lson);
    build(rson);
    PushUp(rt);
}
void update(int ll, int rr, int x, int l, int r, int rt)
{
    if(ll<=l&&rr>=r)
    {
        lazy[rt]=x;
        sum[rt]=(r-l+1)*x;
        return ;
    }
    PushDown(rt, r-l+1);
    int mid=l+r>>1;
    if(ll<=mid) update(ll,rr,x,lson);
    if(rr>mid) update(ll,rr,x,rson);
    PushUp(rt);
}
int main()
{
    int n, i, a, b, c, t, q, num=0;
    scanf("%d",&t);
    while(t--)
    {
        num++;
        scanf("%d",&n);
        build(1,n,1);
        scanf("%d",&q);
        while(q--)
        {
            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",num,sum[1]);
    }
    return 0;
}


HDU 1698 Just a Hook(线段树区间替换),布布扣,bubuko.com

HDU 1698 Just a Hook(线段树区间替换)

标签:acm   c语言   算法   编程   线段树   

原文地址:http://blog.csdn.net/scf0920/article/details/38454457

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