Color the ball
来源:互联网 发布:linux vi命令详解 编辑:程序博客网 时间:2024/06/05 14:07
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,输入结束。 Output 每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。 Sample Input Sample Output
当N = 0,输入结束。
31 12 23 331 11 21 30
1 1 13 2 1
非常机智的前缀和的应用,利用区间首末加一减一然后区间求和即可得到每个点被覆盖的次数。
这道题并未用到树状数组。如果打乱更新和查询的话就要考虑了。
#include<cstdio>
#include<cstring>
#define MAXN 100005
using namespace std;
int a[MAXN],sum[MAXN];
int main(){
int n;
while(1){
scanf("%d",&n);
if(n==0) break;
memset(a,0,sizeof(a));
int l,r;
for(int i=1;i<=n;i++){
scanf("%d %d",&l,&r);
a[l]++;
a[r+1]--;
}
int s=0;
for(int i=1;i<=n;i++){
s+=a[i];
sum[i]=s;
}
for(int i=1;i<n;i++)
printf("%d ",sum[i]);
printf("%d\n",sum[n]);
}
return 0;
}
#include<cstring>
#define MAXN 100005
using namespace std;
int a[MAXN],sum[MAXN];
int main(){
int n;
while(1){
scanf("%d",&n);
if(n==0) break;
memset(a,0,sizeof(a));
int l,r;
for(int i=1;i<=n;i++){
scanf("%d %d",&l,&r);
a[l]++;
a[r+1]--;
}
int s=0;
for(int i=1;i<=n;i++){
s+=a[i];
sum[i]=s;
}
for(int i=1;i<n;i++)
printf("%d ",sum[i]);
printf("%d\n",sum[n]);
}
return 0;
}
0 0
- hdu1556 color the ball
- HDOJ Color the ball
- Color the ball
- Color the Ball
- hdu1556 Color the ball
- hdu1556 Color the ball
- hdu_1556 Color the ball
- hdu1556 Color the ball
- Color the ball----HDOJ1556
- HDU1556 Color the ball
- Color the ball
- Color the ball
- hdu1556 Color the ball
- hdu1556--Color the ball
- HDU_1556 Color the ball
- Color the ball HDU1556
- HDU1556 Color the ball
- Color the ball
- stars
- 算法基础之插入排序
- mongodb 的 curd
- [Thinking in Java]
- 九度 oj 题目1072:有多少不同的面值组合?
- Color the ball
- Java网络01基本网络概念
- Servlet实现简单的用户登录
- Spring中属性配置(配置Properties属性值(通过props,props子节点 )为属性赋值)
- 设计模式之工厂方法模式
- typedef的一些用法及注意事项
- php7的一些新特性
- 写好你的JavaScrip
- 千里之行系列(2)一 Web工程整合JFinal框架