HDU - 1698 Just a Hook (线段树)

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1698

题解:

做的我不想说啥了…..此题使用cin的话会超时,结构体tr得开原来数据范围的4倍左右

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lc (d<<1)
#define rc (d<<1|1)
#define mid ((l+r)>>1)
using namespace std;
int n,m;
struct node
{
    int sum, lz;
}tr[400040];

void build(int d, int l, int r)
{
    tr[d].lz = 0;
    tr[d].sum = 1;
    if(l == r)
    {
        return;
    }
    build(lc, l, mid);
    build(rc, mid+1, r);
}

void lazy(int d, int l, int r)
{
    int x = r - l + 1;
    if(tr[d].lz != 0)
    {
        tr[lc].lz = tr[d].lz;
        tr[rc].lz = tr[d].lz;
        tr[lc].sum = tr[d].lz * (x - (x>>1));//这个括号贼关键,不用就wa
        tr[rc].sum = tr[d].lz * (x>>1);//这里错了好久。。忘记除2tr[d].lz = 0 ;
    }
}

void update(int d, int l, int r, int L, int R, int k)
{
    if(l >= L && r <= R)
    {
        tr[d].lz = k;
        tr[d].sum = k * (r - l + 1);
        return;
    }
    lazy(d,l,r);
    if(L <= mid) update(lc,l,mid,L,R,k);
    if(R > mid) update(rc,mid+1,r,L,R,k);
    tr[d].sum = tr[lc].sum + tr[rc].sum;
}

int main()
{
    int t,cnt = 1;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        build(1,1,n);
        scanf("%d", &m);
        while(m--)
        {
            int l,r,k;
            scanf("%d%d%d", &l,&r,&k);
            update(1,1,n,l,r,k);
        }
        printf("Case %d: The total value of the hook is %d.\n",cnt++,tr[1].sum);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值