1 10 1 2 3 4 5 6 7 8 9 10 Query 1 3 Add 3 6 Query 2 7 Sub 10 2 Add 6 3 Query 3 10 End
Case 1: 6 33 59
#include<stdio.h>
#include<string.h>
#define maxn 55555
int N ;
int a[maxn] ;
int lowbit(int x)
{
return x & (-x) ;//返回的是i的二进制最低位1所在位对应的值,例如,3的二进制数为0000 0011,则返回的就是1,
}
//实现加减
void update(int x, int y )
{
while(x <= N)
{
a[x] += y ;
x += lowbit(x) ;
}
}
//求和
int Sum(int x)
{
int sum = 0 ;
while( x > 0 )
{
sum += a[x] ;
x -= lowbit(x) ;
}
return sum ;
}
int main()
{
int T = 0 ;
scanf("%d" , &T) ;
int m = 1 ;
while(T--)
{
scanf("%d" , &N);
memset(a,0,sizeof(a)) ;
int n = 0 ;
for(int i = 1 ; i <= N ; i++)
{
scanf("%d",&n);//每赋一次值,更新一次
update(i , n ) ;
}
printf("Case %d:\n",m++);
char w[10] ;
int a = 0 , b = 0 ;
while(1)
{
scanf("%s",w);
if(w[0] == ‘E‘)
break ;
scanf("%d%d" , &a , &b);//第a位和第b位
int y = 0 ;
if(w[0] == ‘Q‘)
{
y = Sum(b) - Sum(a-1) ;
printf("%d\n", y) ;
}
if(w[0] == ‘A‘)
update(a , b) ;
if(w[0] == ‘S‘)
update(a , -b) ;
}
}
return 0 ;
}
原文地址:http://blog.csdn.net/bluedream1219/article/details/38036679