hdu 1166 敌兵布阵

线段树水题。

有三种操作,x点加sum人,x点减sum人,求出 x 到 y 的和。

每个父节点加一个节点保存下面子树的和即可。减人的时候,增加负的值即可。1Y,嗨皮~

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

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

void Updata(int t,int l,int r,int sum)
{
	if( node[t].l == l && node[t].r == r )
	{
		node[t].sum += sum;
		return ;
	}
	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);
		}
	node[t].sum += sum;
}

int Query(int t,int l,int r)
{
	if( node[t].l == l && node[t].r == r )
		return node[t].sum;	
	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 ind = 1,ncases,n,x,y;
	char s[10];
	scanf("%d",&ncases);
	
	while( ncases-- )
	{
		scanf("%d",&n);
		for(int i=0; i<n; i++)
			scanf("%d",&num[i]);
		init();
		Build(1,0,n+1);
		printf("Case %d:\n",ind++);
		while( scanf("%s",s) && strcmp(s,"End") )
		{
			scanf("%d%d",&x,&y);
			if( s[0] == 'A' )
				Updata(1,x-1,x,y);
			else
				if( s[0] == 'S' )
					Updata(1,x-1,x,-y);
				else
					printf("%d\n",Query(1,x-1,y));
		}
	}
return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值