A - 敌兵布阵

本题按照题意来即可,别人的代码

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=50000+10;
struct node{
	int L,R,sum;
	int mid(){//计算中间值 
		return (L+R)/2;
	}
}tree[maxn*4];
int val[maxn];
void build(int root,int L,int R)
{
	tree[root].L=L,tree[root].R=R;
	if(L==R)
	{
		tree[root].sum=val[L];
		return;
	}
	build(root<<1,L,tree[root].mid());
	build(root<<1|1,tree[root].mid()+1,R);
	tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum;
}
void Insert(int root,int k,int e)
{
	tree[root].sum+=e;
	if(tree[root].L==tree[root].R)
		return;
	if(k<=tree[root].mid())
		Insert(root<<1,k,e);
	else
		Insert(root<<1|1,k,e);
}
int Query(int root,int L,int R)
{
	if(tree[root].L==L&&tree[root].R==R)
		return tree[root].sum;
	if(R<=tree[root].mid())
		return Query(root<<1,L,R);
	else if(L>tree[root].mid())
		return Query(root<<1|1,L,R);
	else
	{
		int Lsum=Query(root<<1,L,tree[root].mid());
		int Rsum=Query(root<<1|1,tree[root].mid()+1,R);
		return Lsum+Rsum;
	}
}
int main()
{
	int T,t=1;
	int n,x,y;
	//freopen("A.txt","r",stdin);
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
			scanf("%d",&val[i]);
		build(1,1,n);
		char s[10];
		printf("Case %d:\n",t++);
		while(scanf("%s",s),s[0]!='E')
		{
			scanf("%d %d",&x,&y);
			if(s[0]=='A')	Insert(1,x,y);
			else if(s[0]=='S')	Insert(1,x,-y);
			else
			{
				int ans=Query(1,x,y);
				printf("%d\n",ans);
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值