题目描述
敌兵布阵
解法:树状数组
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e4+10;
int a[MAXN], c[MAXN];
int n, t, case_cnt = 1;
int lowbit(int x){return x&(-x);}
int sum(int x)
{
int s = 0;
for(;x>=1;x-=lowbit(x))
s += c[x];
return s;
}
void update(int x, int v)
{
while(x<=n)
{
c[x] += v;
x += lowbit(x);
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(c, 0, sizeof(c));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
update(i, a[i]);
}
char ch[10];
printf("Case %d:\n",case_cnt++);
while(~scanf("%s", ch))
{
if(ch[0]=='E') break;
else if(ch[0]=='Q')
{
int l, r;
scanf("%d%d", &l, &r);
int ss = sum(r)-sum(l-1);
printf("%d\n", ss);
}
else if(ch[0]=='A')
{
int x, v;
scanf("%d%d", &x, &v);
update(x, v);
}
else
{
int x, v;
scanf("%d%d", &x, &v);
update(x, -v);
}
}
}
return 0;
}