Color the ball ---树状数组(区间更新及单点求值)
来源:互联网 发布:神仙劫法座进阶数据 编辑:程序博客网 时间:2024/05/28 14:57
Problem Description
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
Input
每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。
当N = 0,输入结束。
当N = 0,输入结束。
Output
每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
Sample Input
31 12 23 331 11 21 30
Sample Output
1 1 13 2 1
Author
8600
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN=100010;int n, C[MAXN]; int Lowbit(int i){return i&(-i);}void Update(int i,int x){ while(i<=n){C[i]=C[i]+x;i=i+Lowbit(i);}} int Query(int i){int sum=0;while(i>0){sum+=C[i];i-=Lowbit(i);}return sum;}int main(){while(scanf("%d",&n)!=EOF){if(n==0) break;memset(C,0,sizeof(C)); for(int i=0;i<n;i++) { int a,b; scanf("%d%d",&a,&b); Update(a,1); Update(b+1,-1); } for(int i=1;i<n;i++) printf("%d ",Query(i));printf("%d\n",Query(n));}return 0;}
阅读全文
0 0
- Color the ball ---树状数组(区间更新及单点求值)
- HDU1556 Color the ball 树状数组(区间更新单点求值)
- [HDU]1556 Color the ball (树状数组--区间更新,单点求值)
- 【树状数组 区间修改,单点求值】1556 Color the ball
- HDU 1556 Color the ball (树状数组-- 区间更新,单点求值)
- HDU1556:Color the ball(线段树区间更新单点求值)&&树状数组解法
- 树状数组:HDU1556 Color the ball 【区间更新,单点查询]
- Hdu oj 1556 Color the ball(树状数组区间更新转单点更新)
- (树状数组)hdu1556 Color the ball(区间更新,单点查询)
- hdu 1556 Color the ball 【树状数组:区间修改、单点求值】
- HDU - 1556 Color the ball (一维树状数组 + 区间修改 + 单点求值)
- HDu 1556 Color the ball【线段树&&树状数组】区间更新,单点查询
- hdu1556 color the ball 树状数组区间更新单点查询(附线段树做法)与二维扩展
- HDU 1556 Color the ball【树状数组+区间更新】
- HDU1556 Color the ball【树状数组】【区间更新】
- HDU 1556 Color the ball(树状数组区间更新)
- HDU 1556 Color the ball 树状数组-区间更新-模板
- HDU 1566 Color the ball [树状数组区间更新]【数据结构】
- leetcode 540. Single Element in a Sorted Array
- 解决Echarts使用tab切换时只显示第一个tab中图表,其他tab中图表不显示或显示不全问题
- MYIR-ZYNQ7000系列-zturn教程(2):Hello_World
- C++ STL之queue常见用法
- Android 实现RecyclerView的下拉刷新和上拉加载
- Color the ball ---树状数组(区间更新及单点求值)
- 解决已经安装requests,却依然提示No module named 'requests'
- C#多线程实操
- mysql入门命令
- table样式
- valgrind定位内存问题
- 永久代(PermGen)和元空间的区别(Metaspace)
- Ecology8二次开发:HTML表单字段必填验证的添加与移除
- 使用Slf4j集成Log4j2构建项目日志系统的完美解决方案