hdu1698 Just a hook (线段树 延迟标记 区间更新)

一个小小的点拖了这么久,好在终于考完试了,能好好做会题了。

这题也没自己写了,抄了一份模板。(写的确实比自己好。。)


#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
#define lchild rt << 1, l, m
#define rchild rt << 1 | 1, m + 1, r
const int N=400010;
int tree[N],lazy[N],t,ans=1,L,R,delta,n,m;


void push_down(int rt, int len) {
	if(lazy[rt])
	{
		lazy[rt << 1]=lazy[rt << 1 | 1] = lazy[rt];/*延迟标记往下传*/
    	tree[rt << 1] = lazy[rt << 1] * (len - (len >> 1));/*左子树比右子树多1*/
    	tree[rt << 1 | 1] = lazy[rt << 1 | 1] * (len >> 1);
    	lazy[rt] = 0;   /*往下传完就赋0*/
	}
}

void build(int rt, int l, int r) {
	lazy[rt]=0;               /*建树先将延迟标标为0*/
    if (l == r) {
        tree[rt]=1;
        return;
    }//注意对树的初始赋值
    int m = (l + r) >> 1;
    build(lchild); build(rchild);
    tree[rt]=tree[rt << 1]+tree[rt << 1 | 1];/*每个节点为两子节点之和*/
}

/*数据更新,这个算法在于不用每次更新到最底部,更新到对应区间的节点即可,等下次更新时再往下传递*/
void update(int rt, int l, int r) {
    if (L <= l && r <= R) {
        tree[rt] = delta * (r - l + 1);
        lazy[rt] = delta;/*这个区间需要加的数先记录,需要时再往下加,大大减少操作*/
        return;
    }
    push_down(rt, r - l + 1);
    int m = (l + r) >> 1;
    if (L <= m) update(lchild);
    if (R > m)  update(rchild);
    tree[rt]=tree[rt << 1]+tree[rt << 1 | 1];
}

int main()
{
	for(scanf("%d",&t);t--;)
	{
		scanf("%d%d",&n,&m);
		build(1,1,n);
		for(int i=0;i<m;i++)
		{
			scanf("%d%d%d",&L,&R,&delta);
			update(1,1,n);
		}
		printf("Case %d: The total value of the hook is %d.\n",ans++,tree[1]);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值