HDOJ 1698 Just a Hook


分析: 只查询一次(总和)。toadd 表示以下全部替换为此值(而不是累加)。初始每个结点为1。

类型:成段更新(lazy标记)。线段树......(ps. 据说树状数组能做成段的?怎么做?再说~...

代码:

//颜色:1 2 3
//You may consider the original hook is made up of cupreous sticks.
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

#define lc e<<1
#define rc e<<1|1
#define MAXN 100000
struct node{ int l, r, mid, v, toadd; } a[MAXN*4]; 	//toadd表示颜色值{0/1/2/3}
int n;

void pushup(int e) { a[e].v=a[lc].v+a[rc].v; }
void pushdown(int e) 
{ 
	int t = a[e].toadd;
	if(t)
	{
	a[e].toadd = 0;
	a[lc].v=(a[lc].r-a[lc].l+1)*t;
	a[rc].v=(a[rc].r-a[rc].l+1)*t; 
	a[lc].toadd=a[rc].toadd=t;
	}
}
void build(int l, int r, int e)	//(1, n, 1)
{
	a[e].l=l; a[e].r=r;
	if(l==r) a[e].v=1;
	else
	{
		a[e].mid=(l+r)>>1;
		build(l, a[e].mid, lc);
		build(a[e].mid+1, r, rc);
		pushup(e);
	}
}
void update(int L, int R, int c, int l, int r, int e)
{
	if(L<=l && r<=R)
	{
		a[e].v = (a[e].r-a[e].l+1)*c;
		a[e].toadd = c;
//printf("L:%d R:%d l:%d r:%d c:%d a[e].v=%d\n", L, R, l, r, c, a[e].v);
	}
	else	// l L R r
	{
		pushdown(e);		//这里不推下去怎么对!!!2~

		if(L<=a[e].mid) update(L, R, c, l, a[e].mid, lc);
		if(a[e].mid<R) update(L, R, c, a[e].mid+1, r, rc);
		pushup(e);
//printf("e:%d, a[e].v: %d\n", e, a[e].v);
	}
}
int cal()
{
	return a[1].v;
}
int main()
{
	int t;	scanf("%d", &t);
	for(int i=0; i<t; i++)
	{
		int q;
		scanf("%d%d", &n, &q);
		memset(a, 0, sizeof(a));
		build(1, n, 1);		//费?
		while(q--)
		{
			int x, y, c;	scanf("%d%d%d", &x, &y, &c);
			update(x, y, c, 1, n, 1);
		}
		printf("Case %d: The total value of the hook is %d.\n", i+1, cal());
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值