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>
const int MAXN=50010;
int n;
int c[MAXN];
int lowbit(int x)
{
return x&(-x);
}
void add(int i,int val)
{
while(i<=n)
{
c[i]+=val;
i+=lowbit(i);
}
}
int sum(int i)
{
int s=0;
while(i>0)
{
s+=c[i];
i-=lowbit(i);
}
return s;
}
int main()
{
int T;
int x,y;
char str[20];
scanf("%d",&T);
int iCase=0;
while(T--)
{
iCase++;
scanf("%d",&n);
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
add(i,x);
}
printf("Case %d:\n",iCase);
while(scanf("%s",&str))
{
if(strcmp(str,"End")==0)
break;
scanf("%d%d",&x,&y);
if(strcmp(str,"Add")==0)
{
add(x,y);
}
else if(strcmp(str,"Sub")==0)
{
add(x,-y);
}
else
printf("%d\n",sum(y)-sum(x-1));
}
}
return 0;
}
原文地址:http://www.cnblogs.com/luzhongshan/p/3904871.html