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 EndSample Output
Case 1: 6 33 59
之前暑假用线段树写的,现在用树状数组写感觉好简洁。
#include
#include
#include
using namespace std;
const int mx = 5e4+10;
int node [mx<<2];
int T,n,a,b,nu;
char te[10];
void update(int x ,int k){
while(x <= n){
node[x] += k;
x += x&(-x);
}
}
int sea(int x){
int ans = 0;
while(x){
ans += node[x];
x -= x&(-x);
}
return ans;
}
int main(){
scanf("%d",&T);
for(int ca = 1; ca <= T; ca++){
printf("Case %d:\n",ca);
scanf("%d",&n);
memset(node,0,sizeof(node));
for(int i = 1; i <= n; i++){
scanf("%d",&nu);
update(i,nu);
}
while(scanf("%s",te)&& te[0] != 'E'){
if (te[0] == 'A') {
scanf("%d%d",&a, &b);
update(a,b);
}
else if (te[0] == 'S') {
scanf("%d%d",&a, &b);
update(a, -b);
}
else {
scanf("%d%d",&a, &b);
printf("%d\n",sea(b) - sea(a-1));
}
}
}
}