您的位置:首页 > 其它

HDU 1166 敌兵布阵(树状数组)

2016-05-30 11:50 302 查看
题目:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=25681#problem/A

代码:

#include<stdio.h>
#include<string.h>
using namespace std;
int a[50005],n;
int lowbit(int t)
{
return t&(-t);
}
int insert(int t,int d)
{
while(t<=n)
{
a[t]=a[t]+d;
t=t+lowbit(t);
}
}
long long getsum(int t)
{
long long ans=0;
while(t>0)
{
ans=ans+a[t];
t=t-lowbit(t);
}
return ans;
}
int main()
{
int t,temp=0;
scanf("%d",&t);
while(t--)
{
memset(a,0,sizeof(a));
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int k;
scanf("%d",&k);
insert(i,k);
}
char str[10];
printf("Case %d:\n",++temp);
while(scanf("%s",str)&&(strcmp(str,"End")!=0))
{
int x,y;
scanf("%d%d",&x,&y);
if(strcmp(str,"Query")==0)
{
printf("%d\n",getsum(y)-getsum(x-1));
}
else if(strcmp(str,"Add")==0)
{
insert(x,y);
}
else if(strcmp(str,"Sub")==0)
{
insert(x,(-1)*y);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: