您的位置:首页 > 其它

HDU 1166 敌兵布阵 树状数组

2016-05-03 20:37 309 查看
看的别人的博客学的树状数组:
http://blog.csdn.net/lulipeng_cpp/article/details/7816527 http://blog.csdn.net/queuelovestack/article/details/47414119
/*          树状数组                */
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <cstring>
#define N 51000
using namespace std;
int a
,c
,n;
int lowbit(int x)/*     c[t]展开以后有多少项        */
{
return x&(-x);
}
int SUM(int i)/*        求a数组的前n项和               */
{
int sum=0;
while(i>0) {
sum+=c[i];
i-=lowbit(i);
}
return sum;
}
void update(int x,int y)/*      单点更新数组c      */
{
while(x<=n) {
c[x]+=y;
x+=lowbit(x);
}
}
void creat()/*      创建树状数组c                  */
{
int i=1;
while(i<=n) {
update(i,a[i]);
i++;
}
}
int main()
{
int t;
cin>>t;
for(int m=1; m<=t; m++) {
int i;
scanf("%d",&n);
memset(c,0,sizeof(c));
memset(a,0,sizeof(a));
for(i=1; i<=n; i++)
scanf("%d",a+i);
creat();
printf("Case %d:\n",m);
getchar();
string s;
while(1) {
cin>>s;
if(s=="End")
break;
int x,y;
scanf("%d %d%*c",&x,&y);
if(s=="Query")
printf("%d\n",SUM(y)-SUM(x-1));
else if(s=="Add")
update(x,y);
else
update(x,-y);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: