【树状数组-区间更新】hdu1556 Color the ball
来源:互联网 发布:杜兰特对位科比数据 编辑:程序博客网 时间:2024/06/07 20:54
Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 21163 Accepted Submission(s): 10257
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
思路:之前学习尺取法的时候有这样一个问题就是有个人給一排小朋友发红包,总共发m次,每次发给区间[a,b]的小朋友1元钱,然后求最少的连续的小朋友的钱数和为n;期间处理发红包就有一个技巧:
这个技巧叫:扫描线!有一道关于扫描线的题目:(点击打开链接)
while(m--){ scanf("%d%d",&a,&b); num[a]++,num[b+1]--;}for(int i=1;i<=N;i++) sum[i]=sum[i-1]+num[i];
这个发红包和涂色气球不就是一样的么!
所以更新的话是:add(a,1),add(b+1,-1);
代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N=100005;int C[N];int lowbit(int x){ return x&(-x);}int sum(int x){ int ret=0; while(x>0) { ret+=C[x]; x-=lowbit(x); } return ret;}void add(int x,int d){ while(x<N) { C[x]+=d; x+=lowbit(x); }}int main(){ int n; while(~scanf("%d",&n)) { if(n==0) break; memset(C,0,sizeof(C)); for(int i=1;i<=n;i++) { int a,b; scanf("%d%d",&a,&b); add(a,1); add(b+1,-1); } for(int i=1;i<=n;i++) { if(i!=n) printf("%d ",sum(i)); else printf("%d\n",sum(i)); } } return 0;}
阅读全文
0 0
- 树状数组:HDU1556 Color the ball 【区间更新,单点查询]
- HDU1556 Color the ball【树状数组】【区间更新】
- 【树状数组-区间更新】hdu1556 Color the ball
- hdu1556 Color the ball(树状数组 修改区间,查找值)
- HDU1556——Color the ball(区间更新OR树状数组)
- HDU1556 Color the ball 树状数组(区间更新单点求值)
- HDU1556:Color the ball(线段树区间更新单点求值)&&树状数组解法
- (树状数组)hdu1556 Color the ball(区间更新,单点查询)
- hdu1556 Color the ball (树状数组)
- HDU1556 Color the ball (树状数组)
- [树状数组]HDU1556 Color the ball
- hdu1556 Color the ball(树状数组)
- Hdu1556 Color the ball [分块][树状数组]
- [HDU1556] Color the ball - 树状数组
- HDU1556 Color the ball (树状数组)
- 【HDU1556】Color the ball(树状数组)
- hdu1556 Color the ball 树状数组
- 【HDU1556-color the ball】(树状数组)
- HDU 5942 Just a Math Problem(莫比乌斯)
- 循环链表的插入删除实现
- python爬虫入门-发送请求
- ZOJ1029-Moving Tables
- Python--Lambda
- 【树状数组-区间更新】hdu1556 Color the ball
- Hadoop 历史
- 《OpenCL异构并行计算:原理、机制与优化实践》笔记(二):进入OpenCL的世界(矢量加法)
- 希尔排序(+冒泡排序)
- VMware中Ubuntu连不上网的解决方案
- object recognition(ZFNet)
- Makefile模版及一些小技巧
- 优雅的代码:将处理用户界面和处理业务逻辑的代码分开
- Android——AIDL基础实现demo以及原理探究