HDU 1556 Color the ball
来源:互联网 发布:mysql 禁止删除数据 编辑:程序博客网 时间:2024/06/05 15:40
Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17048 Accepted Submission(s): 8515
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
虽然网上把这道题总结为线段树的题,但听老师的讲解后,树状数组不失为一种更加简便的做法,每次在tree[a]加一,tree[b+1]减一,相当于[a,b]内对应的元素加一,而区间外的元素不受影响,而且代码简洁易懂。
代码如下:
Problem : 1556 ( Color the ball ) Judge Status : Accepted
RunId : 18038526 Language : C++ Author : 15030120010
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
RunId : 18038526 Language : C++ Author : 15030120010
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
#include<stdio.h>#include<string.h>int n,a,b;int tree[100005];int lowbit(int x){ return x&-x;}int getsum(int x){ int s=0; while(x) { s+=tree[x]; x-=lowbit(x); } return s;}void add(int x,int v){ while(x<=n) { tree[x]+=v; x+=lowbit(x); }}int main(){ int i; while(scanf("%d",&n)!=EOF) { if(n==0) return 0; memset(tree,0,sizeof(tree)); for(i=1;i<=n;++i) { scanf("%d %d",&a,&b); add(a,1),add(b+1,-1); } for(i=1;i<=n;++i) { printf("%d",getsum(i)); if(i<n) printf(" "); } printf("\n"); } 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
- 【操作系统】 Windows 10 中常用的快捷键
- 时钟中断是rt-thread的线程调度器的驱动力
- mysql系列之9.mysql日志&存储引擎
- 数据链路层-2 VLAN
- java 线程总结
- HDU 1556 Color the ball
- 百度360一起搜
- leetcode scramble string
- javascript控制HTML5的全屏操控,浏览器兼容问题
- java生成zip文件打包操作代码(包括解压缩)
- Spring4.X + Spring MVC + Mybatis3 零配置应用开发框架搭建详解 (2) - 基础框架搭建
- mysq'l系列之10.mysql优化&权限控制
- rt-thread的空闲线程源码分析
- const 与#define的异同