标签:
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 #define MAXN 50000*4+10 6 int sum[MAXN]; 7 8 void pushplus(int rt) 9 { 10 sum[rt]=sum[2*rt]+sum[2*rt+1]; 11 } 12 13 void build(int l,int r,int rt) 14 { 15 int temp; 16 if(l==r) 17 { 18 cin>>sum[rt]; 19 return; 20 } 21 temp=(l+r)/2; 22 build(l,temp,2*rt); 23 build(temp+1,r,2*rt+1); 24 pushplus(rt); 25 } 26 27 void update(int p,int add,int l,int r,int rt) 28 { 29 int temp; 30 if(l==r) 31 { 32 sum[rt]+=add; 33 return; 34 } 35 temp=(l+r)/2; 36 if(p<=temp)update(p,add,l,temp,2*rt); 37 else update(p,add,temp+1,r,2*rt+1); 38 pushplus(rt); 39 } 40 41 int query(int L,int R,int l,int r,int rt) 42 { 43 int temp,ans=0; 44 if(l>=L&&r<=R) 45 return sum[rt]; 46 temp=(l+r)/2; 47 if(L<=temp) 48 ans+=query(L,R,l,temp,2*rt); 49 if(R>temp) 50 ans+=query(L,R,temp+1,r,2*rt+1); 51 return ans; 52 } 53 54 55 int main() 56 { 57 //freopen("in.txt","r",stdin); 58 char s[10]; 59 int m,i,n,a,b; 60 cin>>m; 61 for(i=1;i<=m;i++) 62 { 63 cout<<"Case "<<i<<":"<<endl; 64 cin>>n; 65 build(1,n,1); 66 while(scanf("%s",s)&&strcmp(s,"End")!=0) 67 { 68 cin>>a>>b; 69 if(s[0]==‘Q‘) 70 cout<<query(a,b,1,n,1)<<endl; 71 else if(s[0]==‘S‘) 72 update(a,-b,1,n,1); 73 else 74 update(a,b,1,n,1); 75 } 76 } 77 return 0; 78 }
标签:
原文地址:http://www.cnblogs.com/homura/p/4700493.html