hdu 1698 - Just a Hook(线段树)

题目:

Just a Hook


题意:

给一串初始值全为1的数字串,X Y Z表示把下标X—Y的值全改为Z,求所有操作之后的所有数字之和


代码:

//#pragma comment(linker, "/STACK:102400000,102400000")
#include "iostream"
#include "cstring"
#include "algorithm"
#include "cmath"
#include "cstdio"
#include "sstream"
#include "queue"
#include "vector"
#include "string"
#include "stack"
#include "cstdlib"
#include "deque"
#include "fstream"
#include "map"
using namespace std;
typedef long long LL;
const int INF = 522133279;
const int MAXN = 100000+100;
#define eps 1e-14
const int mod = 1000000007;

struct tt
{
    int l,r;
    int v;              //当前区间总和
    int type;           //当前区间的元素类型(1,2,3)

    void ca(int tmp)
    {
        type = tmp;
        v = (r-l+1)*tmp;
    }

}tree[MAXN*3];

void build(int root , int l , int r)
{
    tree[root].l = l;
    tree[root].r = r;
    tree[root].ca(1);

    if(l == r)
        return ;

    int mid = (l+r)/2;

    build(root*2,l,mid);
    build(root*2+1,mid+1,r);
}

void up(int root , int l, int r , int type)
{
    if(r >= tree[root].r && l <= tree[root].l)          //假如当前覆盖区间被包含在目的更新区间内,直接改变此覆盖区间的type类型和v值
    {
        tree[root].ca(type);
        return;
    }

    //否则

    if(tree[root].l != tree[root].r && tree[root].type)  //很重要的延迟处理,当前区间的type值不为0,表示其子区间与其有相同的状态,可以刷新子区间的v值,
                                                         //否则表示其子区间必定已被修改,type肯定不同,不能更新,需进行后续处理
    {
        tree[2*root].ca(tree[root].type);
        tree[2*root+1].ca(tree[root].type);
        tree[root].type=0;
    }

    int mid = (tree[root].l + tree[root].r)/2;
    if(l <= mid)                                //如果目地区间与子区间有交集,则更新子区间
        up(root*2,l,r,type);
    if(r > mid)
        up(root*2+1,l,r,type);
    tree[root].v = tree[2*root].v + tree[2*root+1].v;
}

int main()
{
    //freopen("in","r",stdin);
    //freopen("out","w",stdout);

    int t;
    scanf("%d",&t);

    for(int ka = 1 ; ka <= t ; ka++)
    {
        int n;
        scanf("%d",&n);
        memset(tree,0,sizeof(tree));

        build(1,1,n);

        int q;
        scanf("%d",&q);

        for(int i = 0 ; i < q ; i++)
        {
            int a,x,y;
            scanf("%d%d%d",&x,&y,&a);
            up(1,x,y,a);
        }

        printf("Case %d: The total value of the hook is %d.\n",ka,tree[1].v);
    }

    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值