HDU1556 Color the ball 树状数组(区间更新单点求值)
2015-10-10 14:06
302 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556
树状数组实现代码如下:
树状数组实现代码如下:
//树状数组纪录每个点的改变值 #include <cstdio> #include <cstring> using namespace std; const int M=100010; int a[M],n; int lowbit(int i) { return i&(-i); } void update(int i,int x) { while(i<=n) { a[i]+=x; i+=lowbit(i); } } int query(int n) { int sum=0; while(n>0) { sum+=a ; n-=lowbit(n); } return sum; } int main() { int x,y; while(scanf("%d",&n)&&n) { memset(a,0,sizeof(a)); for(int i=1;i<=n;i++) { scanf("%d%d",&x,&y); update(x,1); update(y+1,-1); } for(int i=1;i<n;i++) printf("%d ",query(i)); printf("%d\n",query(n)); } return 0; }
相关文章推荐
- 各种回调函数
- gcc -shared -o libJava.so Java.o ./libdemo.a
- 居于Web的进度条实现思路(下载百分比)
- Java基础知识强化之集合框架笔记68:Collections类概述和成员方法(备注:Collection 和 Collections区别)
- Android中调用Paint的measureText()方法取得字符串显示的宽度值
- 80端口被Apache的httpd.exe所占用
- Invalid ADAPTORNAME specified. Type 'imaqhwinfo' for a list of available ADAPTORNAMEs. Image acquisi
- java.lang.StringIndexOutOfBoundsException: String index out of range: 0错误的原因 3ff0
- Workspace目录的更改
- 【转】万网域名查询接口(API)的说明
- uart测试代码
- bzoj1646 [Usaco2007 Open]Catch That Cow 抓住那只牛
- js中获得项目名称
- block原理详细讲解(一)
- Processing 用图片填充形状
- Android快速多渠道打包
- HSB转RGB
- iGriamceV8.0 IG8.0.0 iOS8 V8基本设置和使用教程图
- iOS AFNetworking上传图片到服务器
- 规范化的软件项目演进管理--从 Github 使用说起