裸的树状数组。
#include <iostream>
#include <cstdio>
using namespace std;
const int MAX = 5e4 + 5;
int arr[MAX];
int n;
int lowbit(int x)
{
return x & (-x);
}
int update(int k, int num)
{
while (k <= n)
{
arr[k] += num;
k += lowbit(k);
}
return 0;
}
int sum(int k)
{
int res = 0;
while (k)
{
res += arr[k];
k -= lowbit(k);
}
return res;
}
int main()
{
int t, cas = 0;
scanf("%d", &t);
while (t--)
{
memset(arr, 0, sizeof(arr));
scanf("%d", &n);
int tmp;
for (int i = 1; i <= n; ++i)
{
scanf("%d", &tmp);
update(i, tmp);
}
char str[10];
printf("Case %d:\n", ++cas);
while (scanf("%s", str))
{
if (str[0] == 'E')
break;
if (str[0] == 'Q')
{
int st, ed;
scanf("%d%d", &st, &ed);
printf("%d\n", sum(ed) - sum(st - 1));
}
else if (str[0] == 'A')
{
int pos, num;
scanf("%d%d", &pos, &num);
update(pos, num);
}
else if (str[0] == 'S')
{
int pos , num;
scanf("%d%d", &pos, &num);
update(pos, -num);
}
}
}
return 0;
}