D - Just a Hook

题目链接:https://cn.vjudge.net/contest/269834#problem/D

题目大意:刚开始给你n个点,每个点的价值是1,然后会区间更新价值,询问最后总的价值是多少。

题解:lazy算法。

代码:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

struct node
{
    int l,r,num;
    int lazy;
} s[2000000];

int h[2000000];

void creat(int t,int l,int r)//建树
{
    int mid=(l+r)>>1;
    if(l==r)
    {
        s[t].l=l;
        s[t].r=r;
        s[t].lazy=1;
        return ;
    }
    s[t].l=l;
    s[t].r=r;
    s[t].lazy=1;
    creat(t*2,l,mid);
    creat(t*2+1,mid+1,r);
}

void addre(int t,int l,int r,int e)//区间更新
{
    int mid=(s[t].l+s[t].r)>>1;
    if(s[t].l==l&&s[t].r==r)
    {
        s[t].lazy=e;
        return ;
    }
    if(s[t].lazy!=0)
    {
        s[t*2+1].lazy=s[t].lazy;
        s[t*2].lazy=s[t].lazy;
        s[t].lazy=0;
    }
    if(mid>=r)
    {

        addre(t*2,l,r,e);
    }
    else if(mid<l)
    {

        addre(t*2+1,l,r,e);
    }
    else
    {
        addre(t*2,l,mid,e);
        addre(t*2+1,mid+1,r,e);
    }
}

int query(int t)//查询总价值
{
    if(s[t].lazy==0)
    {
        return query(t*2)+query(t*2+1);
    }
    else
        return (s[t].r-s[t].l+1)*s[t].lazy;
}

int main()
{
    int T;
    int a,b,c,d,e,mm=0;
    scanf("%d",&T);
    while(T--)
    {
        ++mm;
        scanf("%d %d",&a,&b);
        creat(1,1,a);
        for(int i=1; i<=b; i++)
        {
            scanf("%d %d %d",&c,&d,&e);
            addre(1,c,d,e);
        }
        printf("Case %d: The total value of the hook is %d.\n",mm,query(1));
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值