线段树单点更新模板-杭电1166

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define LL long long
#define left L,m,rt<<1
#define right m+1,R,rt<<1|1
LL num[200010];
void update(int rt)
{
	num[rt]=num[rt<<1]+num[rt<<1|1]; 
}
void build(int L,int R,int rt)
{
	if(L==R)
	{
		scanf("%I64d",&num[rt]);
		return;
	}
	int m=(L+R)/2;
	build(left);
	build(right);
	update(rt);
}
void Add(int a,int b,int L,int R,int rt)
{
	if(L==R)
	{
		num[rt]+=b;
		return;
	}
	int m=(L+R)/2;
	if(a<=m)
	{
		Add(a,b,left);
	}
	else Add(a,b,right);
	update(rt);
}
LL Query(int a,int b,int L,int R,int rt)
{
	if(a<=L&&b>=R)
	{
		return num[rt];
	}
	LL num1=0;
	int m=(L+R)/2;
	if(a<=m)
	{
		num1+=Query(a,b,left);
	}
	if(b>m)
	{
		num1+=Query(a,b,right);
	}
	return num1;
}
int main()
{
	int T,N,pos1,num1,pos2,Case=0;
	char op[20];
	int i,j;
	scanf("%d",&T);
	while(T--)
	{
		printf("Case %d:\n",++Case);
		scanf("%d",&N);
		build(1,N,1);
		while(scanf("%s",op)&&op[0]!='E')
		{
			if(op[0]=='A')
			{
				scanf("%d %d",&pos1,&num1);
				Add(pos1,num1,1,N,1);
			}
			else if(op[0]=='S')
			{
				scanf("%d %d",&pos1,&num1);
				Add(pos1,-num1,1,N,1);
			}
			else 
			{
				scanf("%d %d",&pos1,&pos2);
				
				printf("%I64d\n",Query(pos1,pos2,1,N,1));
			}
		}
	}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值