hdu-1166敌兵布阵(树状数组)
2014-04-04 13:39
344 查看
此处的树状数组讲解请点击:here~~~
#include<stdio.h>
#include<string.h>
int n,a[50005],q[40005];
int lowbit(int x)
{
return x&(-x);
}
int Getsum(int pos)
{
int res = 0;
while(pos > 0)
{
res += a[pos];
pos -= lowbit(pos);
}
return res;
}
void add(int pos,int num)
{
while(pos <= n)
{
a[pos] += num;
pos += lowbit(pos);
}
}
int main()
{
int t,x,y,p=1;
char s[20];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(a,0,sizeof(a));
for(int i = 1;i <= n;i++)
{
scanf("%d",&x);
add(i,x);
}
int num = 0;
while(scanf("%s",s),strcmp(s,"End")!=0)
{
scanf("%d%d",&x,&y);
if(strcmp(s,"Query")==0)
q[num++] = Getsum(y) - Getsum(x-1);
else if(strcmp(s,"Add")==0)
add(x,y);
else
add(x,-y);
}
printf("Case %d:\n",p++);
for(int i = 0;i < num;i++)
{
printf("%d\n",q[i]);
}
}
}
#include<stdio.h>
#include<string.h>
int n,a[50005],q[40005];
int lowbit(int x)
{
return x&(-x);
}
int Getsum(int pos)
{
int res = 0;
while(pos > 0)
{
res += a[pos];
pos -= lowbit(pos);
}
return res;
}
void add(int pos,int num)
{
while(pos <= n)
{
a[pos] += num;
pos += lowbit(pos);
}
}
int main()
{
int t,x,y,p=1;
char s[20];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(a,0,sizeof(a));
for(int i = 1;i <= n;i++)
{
scanf("%d",&x);
add(i,x);
}
int num = 0;
while(scanf("%s",s),strcmp(s,"End")!=0)
{
scanf("%d%d",&x,&y);
if(strcmp(s,"Query")==0)
q[num++] = Getsum(y) - Getsum(x-1);
else if(strcmp(s,"Add")==0)
add(x,y);
else
add(x,-y);
}
printf("Case %d:\n",p++);
for(int i = 0;i < num;i++)
{
printf("%d\n",q[i]);
}
}
}
相关文章推荐
- HDU 1166 敌兵布阵(树状数组)
- hdu 1166 敌兵布阵 ( 树状数组 )
- hdu-1166 敌兵布阵(树状数组)
- HDU-1166-敌兵布阵【树状数组】
- HDU 1166 敌兵布阵( 树状数组 )
- 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 敌兵布阵 【树状数组】
- HDU 1166 敌兵布阵(树状数组 or 线段树 单点修改 区间求和)
- HDU-1166 敌兵布阵 简单树状数组
- [ACM] hdu 1166 敌兵布阵(树状数组)