HDU 1698 Just a Hook(成段更新)

转载请注明出处忆梦http://blog.csdn.net/yimeng2013/article/details/17191439


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698

线段树功能: update:成端替换

跟着NotOnlySuccess大牛学线段树

#include<cstdio>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define maxn 111111

int sum[maxn<<2];
int col[maxn<<2];

void PushUp(int rt)
{
	sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}

void Pushdown(int rt, int len)
{
	if(col[rt])
	{
		col[rt<<1] = col[rt<<1|1] = col[rt];
		sum[rt<<1] = ( len - (len>>1) )* col[rt];
		sum[rt<<1|1] = (len >> 1) * col[rt];
		col[rt] = 0;
	}
}
void build(int l, int r, int rt)
{
	col[rt] = 0;
	if(l == r)
	{
		sum[rt] = 1;
		return ;
	}

	int m = (l + r) >> 1;
	build(lson);
	build(rson);
	PushUp(rt);
}

void update(int L, int R, int c, int l, int r, int rt)
{
	if(L <= l && r <= R)
	{
		col[rt] = c;
		sum[rt] = (r-l+1) * c;
		return ;
	}
	Pushdown(rt, r-l+1);
	int m = (l + r) >> 1;
	if(L <= m)
		update(L,R, c,lson);
	if(m < R)
		update(L, R, c, rson);
	PushUp(rt);
}

int main ()
{
	int T;
	scanf("%d", &T);
	for(int cas = 1; cas <= T; cas++)
	{
		int n, m;
		scanf("%d %d", &n, &m);

		build(1, n, 1);
		int a, b, c;
		while(m--)
		{
			scanf("%d %d %d", &a, &b, &c);
			update(a, b, c, 1, n, 1);
		}
		printf("Case %d: The total value of the hook is %d.\n", cas, sum[1]);
	}
	return 0;
}


你可能感兴趣的:(HDU 1698 Just a Hook(成段更新))