hdoj 1698 Just a Hook 【线段树】

#include <cstdio>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define P(x) (x>>1)
#define PAUSE system("pause)
using namespace std;
#define MAXN 100005
int s[MAXN<<2];//
int c[MAXN<<2];//用来标记当前线段的权值,0表示该线段无效
int L, R, v;
void updata(int l, int r, int p)
{
	if(L<=l && r<=R)
	{
		c[p] = v;
		s[p] = v * (r-l+1);
		return;
	}
	int m = (r+l)>>1;
	if(c[p])
	{
		c[L(p)] = c[R(p)] = c[p];
		s[L(p)] = (m-l+1)*c[p];
		s[R(p)] = (r - m)*c[p];
		c[p] = 0;//使此节点无效
	}
	if(L <= m) updata(l,   m, L(p));
	if(m < R)  updata(m+1, r, R(p));
	
	s[p] = s[L(p)]+s[R(p)];
}
void test(int l, int r, int p)
{
	printf(" %2d %2d %2d %2d\n", l, r, s[p], c[p], p);
	if(l == r)	return;
	int m = (l+r)>>1;
	test(l, m, L(p));
	test(m+1,r,R(p));
}
int main()
{
	int n, q, T;
	scanf("%d", &T);
	for(int cas=1; cas<=T; cas++)
	{
		scanf("%d%d", &n, &q);
		s[1] = n;
		c[1] = 1;//这样可以省去build的过程
		for(int i=0; i<q; i++)
		{
			scanf("%d%d%d", &L, &R, &v);
			updata(1, n, 1);
			//test(1, n, 1);
		}
		printf("Case %d: The total value of the hook is %d.\n", 
								cas, s[1]);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值