题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1166
题意:
题解:
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MS(a) memset(a,0,sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
inline ll read(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = 5e4+10;
int sum[maxn<<2];
void pushup(int rt){
sum[rt] = sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt){
if(l == r){
sum[rt] = read();
return ;
}
int mid = (l+r)/2;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
pushup(rt);
}
void update(int p,int add,int l,int r,int rt){
if(l==r){
sum[rt] += add;
return ;
}
int mid = (l+r)/2;
if(p <= mid)
update(p,add,l,mid,rt<<1);
else
update(p,add,mid+1,r,rt<<1|1);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
if(L<=l && r<=R){
return sum[rt];
}
int mid = (l+r)/2;
int ans = 0;
if(L>mid){
ans += query(L,R,mid+1,r,rt<<1|1);
}
else if(R<=mid){
ans += query(L,R,l,mid,rt<<1);
}else{
ans += query(L,R,l,mid,rt<<1) + query(L,R,mid+1,r,rt<<1|1);
}
return ans;
}
int main(){
int T=read();
for(int cas=1; cas<=T; cas++){
cout << "Case " << cas << ":\n";
int n = read();
build(1,n,1);
char op[10];
while(scanf("%s",op) && op[0]!='E'){
int a=read(),b=read();
if(op[0]=='Q')
cout << query(a,b,1,n,1) << endl;
else if(op[0]=='S')
update(a,-b,1,n,1);
else
update(a,b,1,n,1);
}
}
return 0;
}