HDU-1166-敌兵布阵(树状数组,附解释)
来源:互联网 发布:java汽车租赁系统代码 编辑:程序博客网 时间:2024/05/28 23:20
HDU-1166-敌兵布阵(树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=1166
初学树状数组,,
题目大意:更新点查区间,,
简单介绍一下树状数组,看下图。
c1 = a1
c3 = a3
c4 = a1 + a2 + a3 + a4
c5 = a5
c6 = a5 + a6
c7 = a7
c8 = a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8
c9=a9c10=a9+a10
对于序列a,我们设一个数组C定义C[i] = a[i – 2^k + 1] + … + a[i],k为i在二进制下末尾0的个数
求2^k
求x在二进制下末尾0的个数(第一个一出现的位置)
int lowbit(int x) { return x&(x^(x-1)); }将a[p]的值加上一个值x
void update(int p,int x){while(p<=n){c[p]+=x;p+=lowbit(p);}}求前p项和
int sum(int p) { int sum=0; while(p>0) { sum+=c[p]; p-=lowbit(p); } return sum; }题目代码:
#include<cstdio>#include<cstring>#define lowbit(x) (x&(x^(x-1)))#define maxn 50010using namespace std;int c[maxn];int n;void update(int p,int x)///给 a[p] 加上 x{ while(p<=n){ c[p]+=x; p+=lowbit(p); }}int sum(int p) ///求前P项的和{ int sum=0; while(p>0){ sum+=c[p]; p-=lowbit(p); } return sum;}int main(){ int T,t=0; char str[10]; scanf("%d",&T); while(T--) { int x,y; scanf("%d",&n); memset(c,0,sizeof(c)); for(int i=1;i<=n;i++){ scanf("%d",&x); update(i,x); } printf("Case %d:\n",++t); while(scanf("%s",str)&&str[0]!='E'){ scanf("%d %d",&x,&y); if(str[0]=='A') update(x,y); else if(str[0]=='S') update(x,-y); else if(str[0]=='Q'){ printf("%d\n",sum(y)-sum(x-1)); } } } return 0;}
参考文章:http://blog.csdn.net/cambridgeacm/article/details/7771782
阅读全文
1 0
- HDU-1166-敌兵布阵(树状数组,附解释)
- hdu 1166 敌兵布阵 (树状数组)
- (树状数组) hdu 1166 敌兵布阵
- HDU 1166 敌兵布阵【树状数组】
- HDU-1166-敌兵布阵(树状数组)
- hdu 1166 敌兵布阵 <树状数组>
- hdu 1166 敌兵布阵(裸树状数组)
- hdu - 1166 - 敌兵布阵(树状数组)
- hdu 1166 敌兵布阵【树状数组入门】
- hdu 1166 敌兵布阵 树状数组
- hdu hdoj 1166 敌兵布阵 树状数组
- HDU--1166 -- 敌兵布阵 [树状数组]
- hdu 1166 敌兵布阵 (树状数组)
- HDU 1166 - 敌兵布阵(树状数组)
- 树状数组-再说敌兵布阵(HDU 1166)
- hdu 1166 敌兵布阵 ( 树状数组 )
- HDU-1166 敌兵布阵 简单树状数组
- HDU 1166 敌兵布阵(树状数组)
- jQuery第二章选择器
- 计算机视觉class5
- 【Druid】Druid连接池(二)——配置监控界面
- L1-016. 查验身份证
- java删除服务器上文件
- HDU-1166-敌兵布阵(树状数组,附解释)
- 趣图:牛X程序员和 Bug 之间的 PK
- kubeadm搭建kubernetes集群之一:构建标准化镜像
- AR+Nike天猫欢聚日:阿里新零售的驱动力是创新与想象力
- Python 最火,R 极具潜力:2017 机器学习调查报告
- AJAX 入门介绍
- UBOOT时钟配置(基于mini2440)
- mongo "errmsg" : "None of the hosts for replica set configReplSet could be contact
- 老严程多光山明使和查先展收