Hdu 1556 Color the ball
来源:互联网 发布:手机淘宝如何找星店 编辑:程序博客网 时间:2024/06/05 13:21
Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 13971 Accepted Submission(s): 7011
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
思路:简单的树状数组应用,更新的是区间,查询的是点,跟之前做的跟新点,查询区间有点不同,但总体思路还是一样,对更新[a,b]可以更新[a,n],再更新[b,n],这样就能更新[a,b]区间,查询的话就求一下和就行了
AC代码如下:
#include <iostream>#include <cstring>#include <cstdio>using namespace std;const int maxn=100000+5;int c[maxn];int n;int lowbit(int n){ return n&(-n);}void add(int a,int b){ while(a<=n){ c[a]+=b; a+=lowbit(a); }}int sum(int i){ int tmp=0; while(i>0){ tmp+=c[i]; i-=lowbit(i); } return tmp;}int main(){ // int n; while(scanf("%d",&n)==1 && n ){ int a,b; memset(c,0,sizeof(c)); for(int i=1;i<=n;i++){ scanf("%d%d",&a,&b); add(a,1); add(b+1,-1); } for(int i=1;i<n;i++) cout<<sum(i)<<" "; cout<<sum(n)<<endl; } return 0 ;}
0 0
- hdu 1556 color the ball
- hdu 1556Color 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 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 1556 Color the ball
- HDU 1556 Color the ball
- HDU 1556 Color the ball
- HDU 1556 Color the ball
- activemq 发布者/订阅 springmvc maven 例子[代码参考]
- std::string和std::wstring声明和用法解释
- Andorid5.0原生下拉刷新简单实现
- Xcode插件管理工具Alcatraz的安装及使用
- 【算法】几种常用排序算法
- Hdu 1556 Color the ball
- mybatis ${}与#{}区别
- 接口测试-获取APP的接口har文件
- excel 中任意几个数相加后等于一个数
- 创业公司中运营人员的典型一天是怎么度过的?
- Linux 内核配置机制(make menuconfig、Kconfig、makefile)讲解
- http与https的写法
- xml命名空间分析
- 最大连续子序列之和