题意:单点修改,区间求和
树状数组入门。。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define maxn 200009
using namespace std;
int n, a[maxn], f[maxn];
int lowbit (int x)
{
return x & (-x);
}
void add (int x, int y)
{
while (x <= n)
{
f[x] += y;
x += lowbit (x);
}
}
int ask (int x)
{
int ret = 0;
while (x > 0)
{
ret += f[x];
x -= lowbit (x);
}
return ret;
}
int main ()
{
int ti;
cin >> ti;
rep (ff, 1, ti)
{
memset (f, 0, sizeof (f));
printf ("Case %d:\n", ff);
cin >> n;
rep (i, 1, n)
scanf ("%d", &a[i]), add (i, a[i]);
while (1)
{
char s[100];
scanf ("%s", s);
if (s[0] == 'E')
break;
int x, y;
scanf ("%d%d", &x, &y);
if (s[0] == 'A')
add (x, y);
else
if (s[0] == 'S')
add (x, -y);
else
printf ("%d\n", ask (y) - ask (x - 1));
}
}
return 0;
}