HDU 1698 Just a Hook

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

题意:屠夫的钩子由等长的金属材料构成(1~N),每段的值为1,2或3,改变x~y之间的值,最后输出总和
#include <stdio.h>
const int maxn=100000;
struct SegmentTree
{
	int l, r, lazy, sum;
}st[maxn<<2];
int t, n, q, x, y, z, cnt;
void BuildTree(int v, int left, int right)
{
	st[v].l=left, st[v].r=right, st[v].lazy=1;
	if (st[v].l==st[v].r)
	{
		st[v].sum=1;
		return;
	}
	int lc=v<<1;
	int rc=lc+1;
	int mid=(left+right)>>1;
	BuildTree(lc, left, mid);
	BuildTree(rc, mid+1, right);
	st[v].sum=st[lc].sum+st[rc].sum;
}
void Insert(int left, int right, int v, int w)
{
	if (st[v].lazy==w) return;
	if (st[v].l==left&&st[v].r==right)
	{
		st[v].lazy=w;
		st[v].sum=(right-left+1)*w;
		return;
	}
	int lc=v<<1;
	int rc=lc+1;
	if (st[v].lazy!=-1)
	{
		st[lc].lazy=st[v].lazy, st[rc].lazy=st[v].lazy;
		st[lc].sum=(st[lc].r-st[lc].l+1)*st[lc].lazy;
		st[rc].sum=(st[rc].r-st[rc].l+1)*st[rc].lazy;
		st[v].lazy=-1;
	}
	int mid=(st[v].l+st[v].r)>>1;
	if (left>mid) Insert(left, right, rc, w);
	else if (right<=mid) Insert(left, right, lc, w);
	else
	{
		Insert(left, mid, lc, w);
		Insert(mid+1, right, rc, w);
	}
	st[v].sum=st[lc].sum+st[rc].sum;
}
int main()
{
	//freopen("in.txt", "r", stdin);
	scanf("%d", &t);
	cnt=1;
	while (t--)
	{
		scanf("%d", &n);
		BuildTree(1, 1, n);
		scanf("%d", &q);
		for (int i=0; i<q; i++)
		{
			scanf("%d %d %d", &x, &y, &z);
			Insert(x, y, 1, z);
		}	
		printf("Case %d: The total value of the hook is %d.\n", cnt++, st[1].sum);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值