重温一下线段树
线段树的模板题
#include
using namespace std;
int a[50010];
int ans;
struct Node{
int l,r,sum;
}tree[200010];
void build(int root,int left,int right){
tree[root].l=left;
tree[root].r=right;
if(left==right){
tree[root].sum=a[left];
return;
}
int mid=(left+right)/2;
build(2*root,left,mid);
build(2*root+1,mid+1,right);
tree[root].sum=tree[root*2].sum+tree[root*2+1].sum;
}
void increase(int root,int pos,int val){
if(tree[root].l==pos&&tree[root].r==pos){
tree[root].sum+=val;
return;
}
int mid=(tree[root].l+tree[root].r)/2;
if(midmid) search(root*2+1,left,right);
else if(right<=mid) search(root*2,left,right);
else{
search(root*2,left,mid);
search(root*2+1,mid+1,right);
}
return;
}
int main(){
//freopen("in.txt","r",stdin);
int test,Case=1;
cin>>test;
while(test--){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
build(1,1,n);
char str[10];
printf("Case %d:\n",Case++);
while(scanf("%s",str)&&str[0]!='E'){
if(str[0]=='A'){
int x,y;
scanf("%d%d",&x,&y);
increase(1,x,y);
}
else if(str[0]=='S'){
int x,y;
scanf("%d%d",&x,&y);
increase(1,x,-y);
}
else if(str[0]=='Q'){
int left,right;
ans=0;
scanf("%d%d",&left,&right);
search(1,left,right);
cout<