点击打开链接
///线段树模板
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
#define mem(name,value) memset(name,value,sizeof(name))
#pragma comment(linker, "/STACK:102400000,102400000")
#define lson l,m,rt<<1 ///rt 二叉树数组下标*2
#define rson m+1,r,rt<<1|1 ///rt 二叉树数组下标*2+1
const int maxn=50010;
struct SegTree
{
int sum[maxn<<2];
void pushup(int l,int r,int rt){///完全二叉树数组向上更新数值,累加
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt,int a[]){///完全二叉树建立数组形式的数组,根节点下标表示1
if(l==r){
sum[rt]=a[l];
return;
}
int m=(l+r)>>1;
build(lson,a);
build(rson,a);
pushup(l,r,rt);
}
void update(int x,int c,int l,int r,int rt){///更新
if(l==r){
sum[rt]+=c;
return;
}
int m=(l+r)>>1;
if(x<=m) update(x,c,lson);
else update(x,c,rson);
pushup(l,r,rt);
}
int query(int L,int R,int l,int r,int rt){///查询计算
if(L<=l && r<=R) return sum[rt];///如果正好查询1-n,直接返回根节点sum[1]
int m=(l+r)>>1,ans=0;
if(L<=m) ans+=query(L,R,lson);
if(m<R) ans+=query(L,R,rson);
return ans;
}
}tree;
int n;
int a[maxn];
char op[10];
int main()
{
int T,cas=1;
cin>>T;
while(T--){
printf("Case %d:\n",cas++);
cin>>n;
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
tree.build(1,n,1,a);
while(scanf("%s",&op)==1 && op[0]!='E'){
int x,y;
scanf("%d%d",&x,&y);
if(op[0]=='A') tree.update(x,y,1,n,1);
else if(op[0]=='S') tree.update(x,-y,1,n,1);
else printf("%d\n",tree.query(x,y,1,n,1));
}
}
return 0;
}