hdu-1698

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


实现求全部区间的和 ,修改部分区间的值。


成段更新需要用到懒惰标记 lazy。简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候。延迟标记的意思是:这个区间的左右儿子都需要被更新,但是当前区间已经更新了。


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <iomanip>

using namespace std;

#define ll(ind) (ind<<1)
#define rr(ind) (ind<<1|1)
#define Mid(a,b) (a+((b-a)>>1))

const int N = 100100;

struct node
{
	int left,right,sum;
	int mid()
	{
		return Mid(left, right);
	}

	int lazy;

	void fun(int summ)
	{
		lazy = summ; 
		sum = (right - left + 1) * lazy;
	}
};

struct segtree
{
	node tree[N*4];

	void real(int ind)//更新懒惰标记
	{
		if (tree[ind].lazy)
		{
			tree[ll(ind)].fun(tree[ind].lazy);
			tree[rr(ind)].fun(tree[ind].lazy);
			tree[ind].lazy = 0;
		}
	}

	void buildtree(int left,int right,int ind)
	{
		tree[ind].left = left;
		tree[ind].right = right;
		tree[ind].sum = right - left + 1;
		tree[ind].lazy = 1;
		if (left != right)
		{
			int mid = tree[ind].mid();
			buildtree(left, mid, ll(ind));
			buildtree(mid + 1, right, rr(ind));
		}
	}

	void update(int st, int ed, int ind, int type)
	{
		int left = tree[ind].left;
		int right = tree[ind].right;
		if (st <= left && right <= ed)
			tree[ind].fun(type);
		else
		{
			real(ind);

			int mid = tree[ind].mid();
			if (st <= mid) update(st, ed, ll(ind), type);
			if (ed > mid) update(st, ed, rr(ind), type);
			tree[ind].sum = tree[ll(ind)].sum + tree[rr(ind)].sum;
		}
	}
}seg;

int main()
{
	int ca = 1;
	int t,n,m;
	int x, y, z;
	scanf("%d",&t);
	while (t--)
	{
		scanf("%d%d", &n, &m);
		seg.buildtree(1, n, 1);
		
		
		while (m--)
		{
			scanf("%d%d%d", &x, &y, &z);
			seg.update(x, y, 1, z);
		}
	
		printf("Case %d: The total value of the hook is %d.\n", ca++, seg.tree[1].sum);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值