HDU 1698线段树成段更新

水。


 

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn=100000;
struct tree
{
    int l;
    int r;
    int sum;//用来标记当前是否被染成混合色;0表示是混合色,1不是;
}T[maxn<<2];

void pushdown(int u)
{
    if(T[u].sum>0)
    {
        T[u<<1].sum=T[u<<1|1].sum=T[u].sum;
        T[u].sum=0;
    }
}

void build(int l,int r,int u)
{
    T[u].l=l;
    T[u].r=r;
    T[u].sum=1;

    if(l==r)
        return;
    int mid=(T[u].l+T[u].r)>>1;

    build(l,mid,u<<1);
    build(mid+1,r,u<<1|1);
}

void update(int l,int r,int value,int u)
{
    if(T[u].sum==value)
    return;//如果该区域内值一样则不必修改;

    if(T[u].l==l&&T[u].r==r)//如果修改区域一样,则直接把该区域的值改为指定值
    {//若结点的value值不为0,则该值代表该结点区间内的结点值都为该结点的值;
        T[u].sum=value;
        return;
    }

    pushdown(u);//如果是纯色的,而修改区域不一致,则先将其子区域置为父值,对子区域进行操作;
    int mid=(T[u].l+T[u].r)>>1;
    //父区域为杂色是进行下面操作;
    if(l>mid)
        update(l,r,value,u<<1|1);
    else if(r<=mid)
        update(l,r,value,u<<1);
    else
    {
        update(l,mid,value,u<<1);
        update(mid+1,r,value,u<<1|1);
    }
}

int query(int u)
{
    if(T[u].sum>0)
        return (T[u].r-T[u].l+1)*T[u].sum;
    return query(u<<1)+query(u<<1|1);
}

int main()
{
    int t,m,n,a,b,value;
    scanf("%d",&t);
    for(int ii=1;ii<=t;ii++)
    {
        scanf("%d",&n);
        build(1,n,1);
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&value);
            update(a,b,value,1);
        }
        printf("Case %d: The total value of the hook is %d.\n",ii,query(1));
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Trac )

你可能感兴趣的:(HDU 1698线段树成段更新)