模板题.
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <bits/stdc++.h> using namespace std; const int maxn=2e5+10; int s[maxn]; int a[maxn]; void built_tree(int start,int end,int node) { if(start==end) { scanf("%d",&s[node]); return; } int mid=(start+end)/2; int left_node=2*node+1; int right_node=2*node+2; built_tree(start,mid,left_node); built_tree(mid+1,end,right_node); s[node]=s[left_node]+s[right_node]; } void updata_stree(int start,int end,int node,int dex,int val) { if(start==end) { s[node]+=val; return; } int mid=(start+end)/2; int left_node=2*node+1; int right_node=2*node+2; if(dex<=mid){ updata_stree(start,mid,left_node,dex,val); } else{ updata_stree(mid+1,end,right_node,dex,val); } s[node]=s[left_node]+s[right_node]; } int query_stree(int start,int end,int node,int L,int R) { if(R<start||L>end) { return 0; } else if(L<=start&&end<=R) { return s[node]; } else if(start==end){ return s[node]; } int mid=(start+end)/2; int left_node=node*2+1; int right_node=node*2+2; int sum_left=query_stree(start,mid,left_node,L,R); int sum_right=query_stree(mid+1,end,right_node,L,R); return sum_left+sum_right; } int main() { int T; scanf("%d",&T); int flag=1; while(T--) { int n; scanf("%d",&n); built_tree(0,n-1,0); string x; printf("Case %d:\n",flag++); while(cin>>x) { if(x=="End") break; if(x[0]=='Q'){ int u,v; scanf("%d%d",&u,&v); //cout<<"u="<<u<<endl; //cout<<"v="<<v<<endl; cout<<query_stree(0,n-1,0,u-1,v-1)<<endl; } else if(x[0]=='A'){ int dex,val; scanf("%d%d",&dex,&val); //cout<<"dex="<<dex<<endl; //cout<<"val="<<val<<endl; updata_stree(0,n-1,0,dex-1,val); } else if(x[0]=='S'){ int dex,val; scanf("%d%d",&dex,&val); //cout<<"dex="<<dex<<endl; //cout<<"val="<<val<<endl; updata_stree(0,n-1,0,dex-1,-val); } } } return 0; }