之前用过了线段树的做法,树状数组的也补上吧
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int c[],n;
int lowbit(int x)
{
return x&(-x);
}
void add(int i,int x)
{
while(i <= n)
{
c[i] += x;
i += lowbit(i);
}
}
int cal(int i)
{
int sum = ;
while(i > )
{
sum += c[i];
i -= lowbit(i);
}
return sum;
}
int main()
{
int t;
scanf("%d",&t);
int tot = ;
while(t--)
{
printf("Case %d:\n",++tot);
memset(c,,sizeof(c));
scanf("%d",&n);
for(int i = ;i <= n;i++)
{
int num;
scanf("%d",&num);
add(i,num);
}
char op[];
while(~scanf("%s",op))
{
int id,num;
if(!strcmp(op,"End"))
break;
else if(!strcmp(op,"Add"))
{
scanf("%d%d",&id,&num);
add(id,num);
}
else if(!strcmp(op,"Sub"))
{
scanf("%d%d",&id,&num);
add(id,-num);
}
else
{
scanf("%d%d",&id,&num);
printf("%d\n",cal(num) - cal(id-));
}
}
}
return ;
}