hdu1166 敌兵布阵--树状数组
原创
©著作权归作者所有:来自51CTO博客作者Limer123的原创作品,请联系作者获取转载授权,否则将追究法律责任
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
#include<cmath>
#include<string>
#include<stdio.h>
#define INF 1000000000
#define eps 0.0001
using namespace std;
int t;
int n;
int a[50005];
int lowBit(int pos)
{
return pos&(-pos);
}
void update(int pos, int value)
{
while (pos <= n)
{
a[pos] += value;
pos += lowBit(pos);
}
}
int sum(int pos)
{
int ans = 0;
while (pos >= 1)
{
ans += a[pos];
pos -= lowBit(pos);
}
return ans;
}
int main()
{
int cas = 1;
int v;
char s[7];
int x, y;
int l, r;
scanf("%d", &t);
while (t--)
{
memset(a, 0, sizeof(a));
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &v);
update(i, v);
}
printf("Case %d:\n", cas++);
while (scanf("%s", s))
{
if (s[0] == 'E')
break;
scanf("%d%d", &x, &y);
if (s[0] == 'A')
update(x, y);
else if (s[0] == 'S')
update(x, -y);
else
printf("%d\n", sum(y) - sum(x - 1));
}
}
return 0;
}