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);
}
}
}
}
代码:
#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);
}
}
}
}
相关文章推荐
- JSR-338 学习笔记(JPA)
- 基于Angularjs实现分页功能
- Leetcode 198. House Robber & 213. House Robber II & 337. House Robber III
- mysql简单存储过程范例
- 结合CSS3的新特性来总结垂直居中的实现方法
- android小知识——Android混淆打包及混淆后运行错误
- [整理]通过http1.1 GET, POST, PUT, PATCH, DELETE, HEAD, TRACE理解真正的RESTFUL
- Lightoj 1422 - Halloween Costumes
- 纯手工实现adaboost(adaboost源码)
- 【干货】如何大幅度提高Mac开发效率
- LeetCode:Multiply Strings
- MYSQL学习笔记3--mysql 2PC二阶段协义 与 日志闪回
- Eclipse 中 program arguments 与 VM arguments 的区别
- 网站前端性能优化
- 从电脑上向手机系统文件里面push文件操作流程:
- iOS 公开项目
- spring工作原理
- Log,Toast,SPUtil,Density,SDCard,ScreenUtil,AppVersion,KeyBoard,NetWork,HttpUtil工具类
- 洗耳翁
- 36. 对象编号获取