HDU 1199(ZOJ 2301)Color the ball (树状数组 区间覆盖)
来源:互联网 发布:淘宝活动招商入口 编辑:程序博客网 时间:2024/04/27 05:27
Color the ball
Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
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
分析:虽说本题是用树状数组解决,但是做这题还有一个更简单的方法解决,那就是当成区间覆盖问题,开个数组初始为0,记录每个气球被扫过的次数,从左向右扫一遍,遇到左端点加一,右端点减一,O(n)的时间内就能搞定,比树状数组还要快!!!代码很简单,就不给出了。
但是,现在正在学习树状数组,那就用树状数组实现一下吧。本题是树状数组的应用,初始建树状数组时的add(x,d)中间的d此时为1,因为是刷一遍,只需加一即可,稍微有点不同,处理之后,树状数组的各点sum()值,就表示每个气球被涮的次数。详见代码
不过本题据说是一道很裸的线段树区间更新,但是线段树听着太高大上了,还不会呢,以后学了,再更新吧。
AC代码:
<span style="font-size:14px;">#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <cmath>#include <cstdlib>#include <ctime>#define INF 0x7fffffffusing namespace std;const int maxn = 100000 + 10;int c[maxn];int 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 a,b; while(scanf("%d",&n)!=EOF && n) { for(int i=0;i<n+2;i++) c[i] = 0; for(int i=0; i<n; i++) { scanf("%d%d",&a,&b); { add(a,1); //加1即可 add(b+1,-1); } } for(int i=1; i<n; i++) printf("%d ",sum(i)); printf("%d\n",sum(n)); //sum(i)代表第i个气球被刷的次数 } return 0;}</span>
0 0
- HDU 1199(ZOJ 2301)Color the ball (树状数组 区间覆盖)
- hdu 1556 Color the ball (树状数组解决区间覆盖+单点查询)
- HDU 1556 Color the ball(树状数组,区间修改,单点统计)
- HDU 1556-Color the ball(树状数组-区间修改 单点查询)
- Hdu oj 1556 Color the ball(树状数组区间更新转单点更新)
- [HDU]1556 Color the ball (树状数组--区间更新,单点求值)
- HDU~1556 Color the ball(线段树区间更新||树状数组)
- HDU 1556 Color the ball【树状数组+区间更新】
- HDU 1556 Color the ball(树状数组区间更新)
- HDU 1556 Color the ball 树状数组-区间更新-模板
- HDU 1566 Color the ball [树状数组区间更新]【数据结构】
- hdu 1556 Color the ball(树状数组)
- HDU 1556 Color the ball(树状数组)(填坑)
- HDU 1556 Color the ball (树状数组简单应用)
- HDU 1556 Color the ball (简单树状数组)
- hdu 1556 Color the ball (扫描线+树状数组)
- hdu 1556 Color the ball(树状数组)
- hdu——1556Color the ball(树状数组)
- 如何附加数据库
- Nginx学习——Nginx进程间的通信
- 遇到bower Arguments to path.join must be strings
- hdu 2555 人人都能参加第30届校田径运动会了
- android 弹出软键盘将底部视图顶起有关问题
- HDU 1199(ZOJ 2301)Color the ball (树状数组 区间覆盖)
- 面试中经常问的三种简单排序方法
- 集合(Arrays)-数组变集合
- Oracle BPM开发中遇到的一些问题和解决方法
- 【HDU1978】How many ways DP行走方式
- 【TOJ 1661】Solitaire【BFS】
- HDU 1060 Leftmost Digit
- linux nc指令(抄录)
- vim的配置