应该是最水的线段树了吧,只是区间查询和点修改,不过是第一道线段树的题,熟悉一下敲法。
/****************************************************/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <stack>
#include <map>
#include <queue>
#include <algorithm>
#include <ctime>
#define EPS 1E-8
#define MAXN 50010
#define INF (~0U >> 2)
#define MOD 10007
#define Lson l, mid, rt << 1
#define Rson mid + 1, r, rt << 1 | 1
using namespace std;
typedef long long LL;
/****************************************************/
int sum[MAXN << 2];
void push_up(int rt)
{
sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void build(int l, int r, int rt)
{
if (l == r)
{
scanf("%d", &sum[rt]);
return;
}
int mid = (l + r) >> 1;
build(Lson); build(Rson);
push_up(rt);
}
void update(int p, int add, int l, int r, int rt)
{
if (l == r)
{
sum[rt] += add;
return;
}
int mid = (l + r) >> 1;
if (p <= mid)
{
update(p, add, Lson);
}
else
{
update(p, add, Rson);
}
push_up(rt);
}
int query(int L, int R, int l, int r, int rt)
{
if (L <= l && r <= R)
{
return sum[rt];
}
int ret = 0, mid = (l + r) >> 1;
if (L <= mid)
{
ret += query(L, R, Lson);
}
if (R > mid)
{
ret += query(L, R, Rson);
}
return ret;
}
char com[10];
int main()
{
int t, now = 0;
scanf("%d", &t);
while (t--)
{
int n;
scanf("%d", &n);
build(1, n, 1);
printf("Case %d:\n", ++now);
while (scanf("%s", com)!=EOF)
{
if (strcmp(com, "End") == 0)
break;
int a, b;
scanf("%d%d", &a, &b);
if (strcmp(com, "Add") == 0)
update(a, b, 1, n, 1);
else if (strcmp(com, "Sub") == 0)
update(a, -b, 1, n, 1);
else if (strcmp(com, "Query") == 0)
{
int ans = query(a, b, 1, n, 1);
printf("%d\n", ans);
}
}
}
}