思路:线段树单点修改+区间查询。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 50001
using namespace std;
string s;
int t,n,tot;
struct nond{
int l,r,sum;
}tree[MAXN*];
void up(int now){
tree[now].sum=tree[now*].sum+tree[now*+].sum;
}
void build(int now,int l,int r){
tree[now].l=l;tree[now].r=r;
if(tree[now].l==tree[now].r){
scanf("%d",&tree[now].sum);
return ;
}
int mid=(tree[now].l+tree[now].r)/;
build(now*,l,mid);
build(now*+,mid+,r);
up(now);
}
void change(int now,int x,int k){
if(tree[now].l==tree[now].r){
tree[now].sum+=k;
return ;
}
int mid=(tree[now].l+tree[now].r)/;
if(x<=mid) change(now*,x,k);
else change(now*+,x,k);
up(now);
}
int query(int now,int l,int r){
if(tree[now].l==l&&tree[now].r==r)
return tree[now].sum;
int mid=(tree[now].l+tree[now].r)/;
if(r<=mid) return query(now*,l,r);
else if(l>mid) return query(now*+,l,r);
else return query(now*,l,mid)+query(now*+,mid+,r);
}
int main(){
scanf("%d",&t);
while(t--){
tot++;printf("Case %d:\n",tot);
scanf("%d",&n);
build(,,n);
int x,y;
while(cin>>s&&s[]!='E'){
if(s[]=='Q'){ scanf("%d%d",&x,&y);printf("%d\n",query(,x,y)); }
else if(s[]=='A'){ scanf("%d%d",&x,&y);change(,x,y); }
else if(s[]=='S'){ scanf("%d%d",&x,&y);change(,x,-y); }
}
}
}
05-11 09:43