HDOJ 1556 Color the ball
来源:互联网 发布:asp与php 编辑:程序博客网 时间:2024/05/19 16:38
Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14882 Accepted Submission(s): 7439
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
这个题可以用线段树做,也可以用树状数组来做,是树状数组的模板题
#include <cstdio>#include <cstring>using namespace std;int bit[100001], n, a, b;void add(int i, int x){ while (i <= n){ bit[i] += x; i += i & -i; }}int sum (int i){ int s = 0; while (i > 0){ s += bit[i]; i -= i & -i; } return s;}int main(){ while (scanf("%d", &n), n){ memset(bit, 0, sizeof(bit)); for (int i = 0; i < n; i++){ scanf("%d%d", &a, &b); add(a, 1); add(b + 1, -1); } for (int i = 1; i <= n; i++){ printf("%d", sum(i)); if (i != n) printf(" "); else printf("\n"); } } return 0;}
0 0
- HDOJ 1556 Color the ball
- HDOJ 1556 Color the ball
- HDOJ 1556 Color the ball
- Color the ball HDOJ--1556
- hdoj 1556 Color the ball
- HDOJ 1556 Color the ball
- HDOJ-1556 Color the ball
- HDOJ 1556 Color the ball
- hdoj-1556-Color the ball
- HDOJ Color the ball
- hdoj Color the ball
- HDoj-1556-Color the ball-树状数组
- hdoj 1556 Color the ball 【树状数组】
- hdoj 1556 Color the ball(树状数组)
- HDOJ Color the ball 1556【树状数组】
- hdoj 1556 Color the ball 【线段树】
- hdoj 1556 Color the ball <区间问题,>
- [HDOJ 1556] Color the ball(线段树成段更新入门)
- 编出递归函数求两个数的最大公约数
- 更新Xcode之后访问网络问题
- 第五周项目训练1 三角形2
- 什么棋AlphaGo永远赢不了人类
- 第五周项目一 三角形类雏形5
- HDOJ 1556 Color the ball
- Android HAL实例解析
- 第二周项目4——(1)用循环求π的近似值
- Java实用篇------文件的压缩
- 1的个数
- CodeForces 632A Grandma Laura and Apples(水题)
- 第四周 项目4-程序分析(1)
- 【没A】【HDU 5654】 xiaoxin and his watermelon candy|主席树
- 第二周—输出Fibonacci数列