HDU 1166 敌兵布阵(树状数组)
2014-05-13 13:42
363 查看
请不要随便指点别人该怎么做、每个人的人生都应该自己掌握、你给不了别人一切、你也不懂别人的忧伤、
微笑不代表快乐、哭泣不一定悲伤
不努力怎么让关心你的人幸福、不努力怎么让看不起你的人绝望、
我用生命在奋斗——lx_Zz
—————————————————————————————————————————————————————————————
————————————————————————— 华丽的分割线 ————————————————————————————
—————————————————————————————————————————————————————————————
上课前切一道树状数组
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;
}
微笑不代表快乐、哭泣不一定悲伤
不努力怎么让关心你的人幸福、不努力怎么让看不起你的人绝望、
我用生命在奋斗——lx_Zz
—————————————————————————————————————————————————————————————
————————————————————————— 华丽的分割线 ————————————————————————————
—————————————————————————————————————————————————————————————
10732768 | 2014-05-13 13:39:16 | Accepted | 1166 | 156MS | 632K | 1480 B | C++ | 给我再去相.... |
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;
}
相关文章推荐
- HDU 1166 敌兵布阵 树状数组 区间求和
- hdu 1166 敌兵布阵 树状数组求区间和和修改点的值
- hdu 1166 敌兵布阵(树状数组裸题)
- 树状数组(一维)例题 HDU——1166敌兵布阵
- HDU 1166 树状数组 敌兵布阵
- hdu 1166 敌兵布阵(简单线段树or树状数组)
- HDU 1166 敌兵布阵 (树状数组)
- HDU 1166 敌兵布阵 (树状数组)
- HDU 1166 敌兵布阵 (我的树状数组加线段树点修改模板)
- HDU 1166 敌兵布阵 (树状数组)
- 【树状数组 + 简单题】杭电 hdu 1166 敌兵布阵
- HDU 1166 敌兵布阵(简单树状数组)
- HDU1166 敌兵布阵(树状数组)
- hdu 1166 敌兵布阵 (树状数组)
- HDU 1166 敌兵布阵(树状数组)
- HDU 1166 敌兵布阵(树状数组,线段树)
- hdu 1166 敌兵布阵(树状数组 | 线段树)
- HDU 1166 敌兵布阵(树状数组)
- hdu_1166 敌兵布阵(树状数组)
- hdu-1166敌兵布阵(树状数组)