这题用树状数组写要简单很多,因为我学习线段树,找了这个题入门。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
#define M 50005
#define ls node<<1,l,m
#define rs node<<1|1,m+1,r
int n;
int Tree[M*4];
void buildtree(int node,int l,int r)
{
    if(l==r) scanf("%d",&Tree[node]);
    else
    {
        int m=(l+r)>>1;
        buildtree(ls);
        buildtree(rs);
        Tree[node]=Tree[node<<1]+Tree[node<<1|1];
    }
}
void update(int node,int l,int r,int ind,int add)
{
    if(l==r)
    {
        Tree[node]+=add;
        return ;
    }
    int m=(l+r)>>1;
    if(ind<=m) update(ls,ind,add);
    else update(rs,ind,add);
    Tree[node]+=add;
}
int query(int node,int l,int r,int L,int R)
{
    if(L<=l&&r<=R) return Tree[node];
    int m=(l+r)>>1;
    int ans=0;
    if(m>=L) ans+=query(ls,L,R);
    if(m<R) ans+=query(rs,L,R);
    return ans;
}
int main()
{
    freopen("in.txt","r",stdin);
    int t,cas=1,x,y;
    scanf("%d",&t);
    while(t--)
    {
        char s[9];
        printf("Case %d:\n",cas++);
        scanf("%d",&n);
        buildtree(1,1,n);
        scanf("%s",s);
        while(1)
        {
            if(s[0]=='E') break;
            scanf("%d%d",&x,&y);
            if(s[0]=='A') update(1,1,n,x,y);
            else if(s[0]=='S') update(1,1,n,x,-y);
            else printf("%d\n",query(1,1,n,x,y));
            scanf("%s",s);
        }
    }
    return 0;
}