HDU1698 Just a Hook

本文针对HDU 1698题目进行了解析,这是一个经典的线段树区间更新问题。文章提供了完整的代码实现,介绍了如何通过线段树进行区间赋值更新,并计算最终数组的总和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:

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

题解:

题目大意:给一个含有n个整数的数组,在给出m个操作,每操作有a,b,c三个步骤,表示将[a,b]该区间的值变为c,问最后的总和是多少。
线段树区间更新模版题。

代码:

#include <cmath>
#include <cstdio>
#include <map>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define met(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
typedef long long ll;
const ll maxn =1e5+10;
#define lchild rt << 1, l, m
#define rchild rt << 1 | 1, m + 1, r
int tree[maxn<<2];
int lazy[maxn<<2];

void push_up(int rt)
{
    tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}

void push_down(int rt,int len)
{
    if(lazy[rt])
    {
        lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
        tree[rt<<1]=lazy[rt]*(len-(len>>1));
        tree[rt<<1|1]=lazy[rt]*(len>>1);
        lazy[rt]=0;
    }
}

void build(int rt,int l,int r)
{
    tree[rt]=1;
    lazy[rt]=0;
    if(l==r)
        return;
    int m=(l+r)>>1;
    build(lchild);
    build(rchild);
    push_up(rt);
}

void updata(int L,int R,int delta,int rt,int l,int r)
{
    if(L<=l&&R>=r)
    {
        tree[rt]=delta*(r-l+1);
        lazy[rt]=delta;
        return;
    }
    if(lazy[rt])
        push_down(rt,r-l+1);
    int m=(l+r)>>1;
    if(L<=m)
        updata(L,R,delta,lchild);
    if(R>m)
        updata(L,R,delta,rchild);
    push_up(rt);
}

/*int  query(int L,int R,int rt,int l,int  r)
{
    if(L<=l&&R>=r)
        return tree[rt];
    if(lazy[rt])
        push_down(rt,r-l+1);
    int m=(l+r)>>1,ret=0;
    if(L<=m)
        ret+=query(L,R,lchild);
    if(R>m)
        ret+=query(L,R,rchild);
    return ret;
}*/

int main()
{
    int t;
    scanf("%d",&t);
    int id=1;
    while(t--)
    {
        int  n,q;
        scanf("%d%d",&n,&q);
        build(1,1,n);
        while(q--)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            updata(a,b,c,1,1,n);
        }
         printf("Case %d: The total value of the hook is %d.\n",id++, tree[1]);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值