现在的位置: 首页 > 综合 > 正文

hdu 1166 树状数组简单题

2013年04月26日 ⁄ 综合 ⁄ 共 647字 ⁄ 字号 评论关闭
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;
}

 

抱歉!评论已关闭.