hdu 1556 Color the ball
来源:互联网 发布:ac68u 网络唤醒 编辑:程序博客网 时间:2024/05/16 02:53
Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8455 Accepted Submission(s): 4329
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
简单的树状数组与线段树的应用。
AC代码如下:
普通写法!
#include<iostream>#include<cstring>#include<cstdio>using namespace std;int num[100005];int main(){ int n; int i,j; int a,b; while(~scanf("%d",&n)&&n) { memset(num,0,sizeof num); for(i=1;i<=n;i++) { scanf("%d%d",&a,&b); num[a]++;num[b+1]--; } int ans=0; for(i=1;i<=n;i++) { ans+=num[i]; printf("%d",ans); if(i!=n) printf(" "); } printf("\n"); } return 0;}
树状数组!
#include<iostream>#include<cstring>#include<cstdio>using namespace std;int c[100010];int n,m;int lowbit(int a){ return a&-a;}void add(int a,int b){ for(;a<=n;a+=lowbit(a)) c[a]+=b;}int sum(int a){ int ans=0; for(;a>0;a-=lowbit(a)) ans+=c[a]; return ans;}int main(){ int i,j; int a,b; while(scanf("%d",&n)&&n) { memset(c,0,sizeof c); for(i=1;i<=n;i++) { scanf("%d%d",&a,&b); add(a,1); add(b+1,-1); } int ans; for(i=1;i<=n;i++) { ans=sum(i); printf("%d",ans); if(i!=n) printf(" "); } printf("\n"); } return 0;}
1 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
- 第十六章 BIRT报表疑难杂惑清理(续2)
- linux 系统上搭建SVN
- Oracle跨数据库查询并插入
- Androidpn里的Xmpp的理解
- eclipse工程中.classpath文件的含义
- hdu 1556 Color the ball
- [C/C++语言测试题]高质量程序设计指南
- hdu 1312 Red and Black (bf、dfs)
- cxf 实现webservice 案例(转)
- 解决MYSQL出现Can't create/write to file....
- office转html文档在线预览
- 【深入Java虚拟机】之二:Class类文件结构
- C++ STL 遍历 map 的时候如何删除其中的 element
- Python fork 守护进程<转>