You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.3 KiB
48 lines
1.3 KiB
#include <cstdio>
|
|
#include <cstring>
|
|
using namespace std;
|
|
const int N = 50010;
|
|
int n;
|
|
|
|
// 树状数组模板
|
|
typedef long long LL;
|
|
#define lowbit(x) (x & -x)
|
|
int c[N];
|
|
void add(int x, int d) {
|
|
for (int i = x; i < N; i += lowbit(i)) c[i] += d;
|
|
}
|
|
LL sum(int x) {
|
|
LL res = 0;
|
|
for (int i = x; i; i -= lowbit(i)) res += c[i];
|
|
return res;
|
|
}
|
|
|
|
int main() {
|
|
int T;
|
|
scanf("%d", &T);
|
|
char op[110]; // 读入指令
|
|
|
|
for (int k = 1; k <= T; k++) {
|
|
scanf("%d", &n);
|
|
// 多组测试数据,需要清空树状数组
|
|
memset(c, 0, sizeof c);
|
|
for (int i = 1; i <= n; i++) {
|
|
int x;
|
|
scanf("%d", &x);
|
|
add(i, x); // i这个营地有x个士兵,映射概念:位置+数量
|
|
}
|
|
// 输出
|
|
printf("Case %d:\n", k);
|
|
while (scanf("%s", op) && op[0] != 'E') {
|
|
int x, y;
|
|
scanf("%d %d", &x, &y);
|
|
if (op[0] == 'Q')
|
|
printf("%lld\n", sum(y) - sum(x - 1)); // 计算区间内士兵个数,前缀和
|
|
else if (op[0] == 'A')
|
|
add(x, y); // 在x号营地增加y名士兵
|
|
else
|
|
add(x, -y); // 在x号营地减少y名士兵
|
|
}
|
|
}
|
|
return 0;
|
|
} |