hdu1698Just a Hook 线段树 成段更新水题

来源:互联网 发布:app下载数据统计 编辑:程序博客网 时间:2024/05/17 16:15
//简单的线段树,注意成段更新,以免超时
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std ;
const int maxn = 100010 ;
struct node
{
    int value ;
    int r , l;
    int flag ;//记录到当前区间的状态
}tree[maxn<<2] ;
void build(int l , int r ,int v)
{
    tree[v].l = l;
    tree[v].r = r;
    tree[v].flag = 0;
    if(l == r)
    {
        tree[v].value = 1;
        return ;
    }
    int mid = (l+r)>>1 ;
    build(l , mid , v<<1) ;
    build(mid+1 , r , v<<1|1) ;
    tree[v].value = tree[v<<1].value + tree[v<<1|1].value ;
}
void pushdown(int v)//向下传递
{
    if(tree[v].flag)
    {
        tree[v<<1].flag = tree[v<<1|1].flag = tree[v].flag ;
        tree[v<<1].value = (tree[v<<1].r - tree[v<<1].l + 1)*tree[v].flag ;
        tree[v<<1|1].value = (tree[v<<1|1].r - tree[v<<1|1].l + 1)*tree[v].flag ;
        tree[v].flag = 0;
    }
}
void update(int x , int  y ,int z ,int v)
{
    if(x <= tree[v].l && y >= tree[v].r)
    {
        tree[v].flag = z ;
        tree[v].value = (tree[v].r - tree[v].l +1)*z ;
        return ;
    }
    pushdown(v) ;
    int mid = (tree[v].l + tree[v].r) >> 1;
    if(x > mid) update(x , y , z ,v<<1|1) ;
    else if(y <= mid)update(x , y , z , v<<1) ;
    else
    {
        update(x , mid , z , v<<1) ;
        update(mid+1 , y , z , v<<1|1) ;
    }
    tree[v].value = tree[v<<1].value + tree[v<<1|1].value ;
}
int main()
{
    //freopen("in.txt","r" ,stdin) ;
    int T ;
    int cas = 0;
    scanf("%d" , &T) ;
    while(T--)
    {
        int N , Q ;
        scanf("%d%d" ,&N ,&Q) ;
        build(1 , N , 1) ;
        while(Q--)
        {
            int x , y , z ;
            scanf("%d%d%d" , &x ,&y ,&z) ;
            update(x , y , z ,1) ;
        }
        printf("Case %d: The total value of the hook is %d.\n" , ++cas , tree[1].value) ;
    }
    return 0;
}











































































0 0