OJ题目 : click here ~~~
中文的,大概题意就不说了。树状数组的水题。
忘记清空数组,导致WA,真可恨啊~~~~~~~
AC_CODE
int n;
int num[50002];
int lowbit(int x)
{
return x&(-x);
}
int sum(int x)
{
int ret = 0;
while(x > 0)
{
ret += num[x];
x -= lowbit(x);
}
return ret;
}
void add(int x , int d)
{
while(x <= n)
{
num[x] += d;
x += lowbit(x);
}
}
int main()
{
int Case;
cin >> Case;
for(int cas = 1;cas <= Case;cas++)
{
printf("Case %d:\n",cas);
memset(num , 0 , sizeof(num));//为什么这个总是会忘记。。
scanf("%d",&n);
int i , j , a , b;
for(i = 1;i <= n;i++)
{
scanf("%d",&a);
add(i , a);
}
string command;
while(cin >> command)
{
if(command == "Query")
{ scanf("%d%d",&a,&b);
printf("%d\n",sum(b) - sum(a - 1));
}
else if(command == "Add")
{
scanf("%d%d",&a,&b);
add(a , b);
}
else if(command == "Sub")
{
scanf("%d%d",&a,&b);
add(a , -b);
}
else if(command == "End")
break;
}
}
return 0;
}