hdu 1698 Just a Hook

线段树水题,成段更新,然后求和。注意将父节点的性质传递到子节点即可。

看了几道难的,不会啊。。。。= = 。。。先刷点水的吧。。慢慢来。。

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <climits>
#define MID(x,y) ((x+y)>>1)
#define L(x) (x<<1)
#define R(x) (x<<1|1)

using namespace std;

const int MAX = 100010;
struct Tnode{int sum,l,r;};
Tnode node[MAX*4];
void init()
{
	memset(node,0,sizeof(node));
}
void Build(int t,int l,int r)
{
	node[t].sum = 1;
	node[t].l = l; node[t].r = r;
	if( l == r - 1 ) return ;
	int mid = MID(l,r);
	Build(R(t),mid,r);
	Build(L(t),l,mid);
}

void Updata(int t,int l,int r,int sum)
{
	if( node[t].l == l && node[t].r == r )
	{
		node[t].sum = sum;
		return ;
	}
	if( node[t].sum > 0 )
	{
		node[R(t)].sum = node[t].sum;
		node[L(t)].sum = node[t].sum;
		node[t].sum = -1;
	}
	int mid = MID(node[t].l,node[t].r);
	if( l >= mid )
		Updata(R(t),l,r,sum);
	else
		if( r <= mid )
			Updata(L(t),l,r,sum);
		else
		{
			Updata(L(t),l,mid,sum);
			Updata(R(t),mid,r,sum);
		}
}

int Query(int t,int l,int r)
{
	if( node[t].sum > 0 )
		return node[t].sum * (r - l);
	int mid = MID(node[t].l,node[t].r);
	if( l >= mid )
		return Query(R(t),l,r);
	else
		if( r <= mid )
			return Query(L(t),l,r);
		else
			return Query(L(t),l,mid) + Query(R(t),mid,r);
}
int main()
{
	int ncases,ind = 1,n,m,x,y,z;
	
	scanf("%d",&ncases);
	
	while( ncases-- )
	{
		scanf("%d%d",&n,&m);
		init();
		Build(1,0,n);
		while( m-- )
		{
			scanf("%d%d%d",&x,&y,&z);
			Updata(1,x-1,y,z);
		}
		printf("Case %d: The total value of the hook is %d.\n",ind++,Query(1,0,n));
	}

return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值