题目大意:中文题
解题思路:因为数据量比较大, 普通暴力算法肯定会超时,采用线段树或者树状数组,这里用树状数组解答。树状数组的基本入门我是看了这个博客的还算挺清楚的,一个地方可能是我理解偏差,lowbit(x)计算的2*k代表的应该是,二进制下从最低位往最高位找出现的第一个1代表的数的大小。看过以后就很清晰了
ac代码:
#include <iostream>
#include <cstring>
using namespace std;
int t, a[50005], c[50005], n, temp1, temp2, cnt=1;
char com[15];
int lowbit(int x)
{
return x&(-x);
}
int getsum(int x)
{
int ans=0;
for (int i=x; i>0; i-=lowbit(i))
ans += c[i];
return ans;
}
void add(int x, int y)
{
for (int i = x; i <= n; i+=lowbit(i))
c[i] += y;
}
int main()
{
scanf("%d", &t);
while (t--){
scanf("%d", &n);
c[0] = 0;
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= n; i++){
c[i] = 0;
temp1 = lowbit(i);
for (int j = i-temp1+1; j <= i; j++)
c[i] += a[j];
}
printf("Case %d:\n", cnt++);
while (1){
scanf("%s", com);
if (!strcmp(com, "End"))
break;
scanf("%d%d", &temp1, &temp2);
if (!strcmp(com, "Query"))
printf("%d\n", getsum(temp2)-getsum(temp1-1));
else if (!strcmp(com, "Add"))
add(temp1, temp2);
else if (!strcmp(com, "Sub"))
add(temp1, -temp2);
}
}
return 0;
}