Description
Input
Output
Sample Input
1 10 1 2 3 4 5 6 7 8 9 10 Query 1 3 Add 3 6 Query 2 7 Sub 10 2 Add 6 3 Query 3 10 End
Sample Output
Case 1: 6 33 59
代码:
#include <cstdio> #include <cstring> #include <vector> #include <iostream> #include <queue> #include <stdlib.h> #define maxn 50000 using namespace std; struct node { int left,right,sum; int mid(){ return (left+right)>>1; } }tree[maxn*4]; void CreatBtree(int l,int r,int rt){ tree[rt].left=l; tree[rt].right=r; //cout<<l<<" "<<r<<endl; if(l==r){ scanf("%d",&tree[rt].sum); return; } int mid=tree[rt].mid(); CreatBtree(l,mid,rt<<1); CreatBtree(mid+1,r,rt<<1|1); tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum; } void Add(int rt,int k,int step){ if(tree[rt].left==tree[rt].right){ tree[rt].sum+=step; return; } else if(k<=tree[rt].mid()) Add(rt<<1,k,step); else Add(rt<<1|1,k,step); tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum; } int Query(int rt,int l,int r){ if(tree[rt].left==l && tree[rt].right==r)return tree[rt].sum; int ans=0; if(r<=tree[rt].mid()) ans+=Query(rt<<1,l,r); else if(l>tree[rt].mid())ans+=Query(rt<<1|1,l,r); else{ ans+=Query(rt<<1,l,tree[rt].mid())+Query(rt<<1|1,tree[rt].mid()+1,r); } return ans; } int main() { int i,j,t,n,step,k,l,r,cishu=1; char str[10]; cin>>t; while(t--){ scanf("%d",&n); printf("Case %d:\n",cishu++); CreatBtree(1,n,1); while(scanf("%s",str)){ if(str[0]=='E')break; scanf("%d%d",&k,&step); if(str[0]=='A'){ Add(1,k,step); } else if(str[0]=='S'){ Add(1,k,-step); } else printf("%d\n",Query(1,k,step)); } } return 0; }