HDU 1166 敌兵布阵 树状数组
2016-05-03 20:37
309 查看
看的别人的博客学的树状数组:
http://blog.csdn.net/lulipeng_cpp/article/details/7816527 http://blog.csdn.net/queuelovestack/article/details/47414119
http://blog.csdn.net/lulipeng_cpp/article/details/7816527 http://blog.csdn.net/queuelovestack/article/details/47414119
/* 树状数组 */ #include <algorithm> #include <cstdio> #include <iostream> #include <cstring> #define N 51000 using namespace std; int a ,c ,n; int lowbit(int x)/* c[t]展开以后有多少项 */ { return x&(-x); } int SUM(int i)/* 求a数组的前n项和 */ { int sum=0; while(i>0) { sum+=c[i]; i-=lowbit(i); } return sum; } void update(int x,int y)/* 单点更新数组c */ { while(x<=n) { c[x]+=y; x+=lowbit(x); } } void creat()/* 创建树状数组c */ { int i=1; while(i<=n) { update(i,a[i]); i++; } } int main() { int t; cin>>t; for(int m=1; m<=t; m++) { int i; scanf("%d",&n); memset(c,0,sizeof(c)); memset(a,0,sizeof(a)); for(i=1; i<=n; i++) scanf("%d",a+i); creat(); printf("Case %d:\n",m); getchar(); string s; while(1) { cin>>s; if(s=="End") break; int x,y; scanf("%d %d%*c",&x,&y); if(s=="Query") printf("%d\n",SUM(y)-SUM(x-1)); else if(s=="Add") update(x,y); else update(x,-y); } } return 0; }
相关文章推荐
- 数据结构实验报告2
- 继承Thread的LooperThread
- Python json模块
- 美团酒店Node全栈开发实践
- fetchxml分页和QueryExpression分页
- CentOS7配置防火墙
- 1006 Problem F
- F
- 小何讲进程: 阻塞进程函数 wait()和waitpid()
- 动态规划——图像压缩
- 十五、访问者设计模式
- LeetCode之动态规划
- 第九周进度条
- Android中Parcelable接口用法
- 输出一个为递增排序数组的旋转数组中的最小元素――8
- ECLIPSE项目中断svn连接解决方法
- 数据结构实验报告1
- Count of Smaller Numbers After Self
- 加密解密--换行符作祟
- 网络之Call Phone、Web、SMS、Email