hdu 1166 树状数组简单题

View Code
#include<cstdio>
#include<cstring>
const int maxn = 50010;
int c[maxn];
int lowbit(int x){
return x&-x;
}
void update(int x,int d){
for(;x<=maxn;x+=lowbit(x))
c[x]+=d;
}
int query(int x){
int sum=0;
for(;x>0;x-=lowbit(x))
sum+=c[x];
return sum;
}
int main()
{
int t,n,cases=1,i,j,a,b;
scanf("%d",&t);
while(t--)
{
memset(c,0,sizeof(c));
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a);
update(i,a);
}
char s[10];
printf("Case %d:\n",cases++);
while(scanf("%s",s)!=EOF)
{
if(!strcmp(s,"End")) break;
else if(!strcmp(s,"Add"))
{
scanf("%d%d",&a,&b);
update(a,b);
}
else if(!strcmp(s,"Query"))
{
scanf("%d%d",&a,&b);
printf("%d\n",query(b)-query(a-1));
}
else
{
scanf("%d%d",&a,&b);
update(a,-b);
}
}
}
return 0;
}

 

你可能感兴趣的:(树状数组)