您的位置:首页 > 其它

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

2014-05-13 13:42 363 查看
请不要随便指点别人该怎么做、每个人的人生都应该自己掌握、你给不了别人一切、你也不懂别人的忧伤、

                                                                                          微笑不代表快乐、哭泣不一定悲伤

               不努力怎么让关心你的人幸福、不努力怎么让看不起你的人绝望、

                                                                                                                                    

                                                                                                                                                              我用生命在奋斗——lx_Zz

—————————————————————————————————————————————————————————————

—————————————————————————    华丽的分割线    ————————————————————————————

—————————————————————————————————————————————————————————————
107327682014-05-13 13:39:16Accepted1166156MS632K1480 BC++给我再去相....
上课前切一道树状数组

7分钟树状数组搞定、上课去了。。。O(∩_∩)O哈哈~

/* 题目: HDU 1166 */
/* 作者: lx_Zz */
/* 时间: 2014.5.13 */
#include<cstdio>
#include<queue>
#include<vector>
#include<map>
#include<string>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 0x7fffffff
#define LL __int64

int n;
const int maxn=50005;
int num[maxn];
int c[maxn];

int lowbit(int x)
{
return x&(-x);
}

void add(int x,int d)
{
while(x<=n)
{
c[x]+=d;
x+=lowbit(x);
}
}

int sum(int x)
{
int ret=0;
while(x>0)
{
ret+=c[x];
x-=lowbit(x);
}
return ret;
}

int main()
{
//freopen("C:\\Users\\终将我要华丽的逆袭\\Desktop\\lx_Zz_in.txt","r",stdin);
//freopen("C:\\Users\\终将我要华丽的逆袭\\Desktop\\lx_Zz_out.txt","w",stdout);

int T;
int cas=1;
scanf("%d",&T);
while(T--)
{
printf("Case %d:\n",cas++);
memset(c,0,sizeof(c));
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
add(i,num[i]);
}
while(true)
{
char str[15];
scanf("%s",str);
if(strcmp(str,"End")==0)break;
else if(strcmp(str,"Add")==0)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
}
else if(strcmp(str,"Sub")==0)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,-b);
}
else if(strcmp(str,"Query")==0)
{
int a,b;
scanf("%d%d",&a,&b);
int ans=sum(b)-sum(a-1);
printf("%d\n",ans);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  树状数组