题目:HDU 1166
代码:
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 50005;
int n,dat[maxn<<2];
void pushplus(int k){
dat[k] = dat[k<<1]+dat[k<<1|1];
}
void init(int l,int r,int k){
if(l==r){
cin>>dat[k];
return ;
}
int m = (l+r)>>1;
init(l,m,k<<1);
init(m+1,r,k<<1|1);//或1就是加1吧。。
pushplus(k);
}
void update(int a,int b,int l,int r,int k){
if(l==r){
dat[k]+=b;
return;
}
int m = (l+r)>>1;
if(a<=m) update(a,b,l,m,k<<1);
else update(a,b,m+1,r,k<<1|1);
pushplus(k);
}
int query(int L,int R,int l,int r,int k){
if(L<=l&&R>=r) return dat[k];
int m = (l+r)>>1;
int ret = 0;
if(L<=m) ret+=query(L,R,l,m,k<<1);
if(R>m) ret+=query(L,R,m+1,r,k<<1|1);
return ret;
}
int main(){
int t;
cin>>t;
int cs = 0;
while(t--){
printf("Case %d:\n",++cs);
int n;
scanf("%d",&n);
init(1,n,1);
char ch[10];
while(scanf("%s",ch)){
if(ch[0]=='E') break;
int a,b;
scanf("%d%d",&a,&b);
if(ch[0]=='Q'){
int sum = query(a,b,1,n,1);
printf("%d\n",sum);
}
if(ch[0]=='A'){
update(a,b,1,n,1);
}
if(ch[0]=='S'){
update(a,-b,1,n,1);
}
}
}
return 0;
}