hdu 1556 Color the ball
来源:互联网 发布:java web开发实战宝典 编辑:程序博客网 时间:2024/05/22 22:37
Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5970 Accepted Submission(s): 3166
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
AC代码
#include <iostream>using namespace std;#define maxn 100000#define lson l,m,rt*2#define rson m+1,r,rt*2+1int sum[maxn<<2];int col[maxn<<2];void build(int l,int r,int rt){sum[rt]=0;col[rt]=0;if(l==r) return;int m=(r+l)/2;build(l,m,rt*2);build(m+1,r,rt*2+1); sum[rt]=sum[rt*2]+sum[rt*2+1];}void PushDown(int rt,int m){if (col[rt]){col[rt<<1] +=col[rt];col[rt<<1|1] += col[rt];sum[rt<<1] += (m - (m >> 1)) * col[rt];sum[rt<<1|1] += (m >> 1) * col[rt]; col[rt] = 0;}}void update(int L,int R,int c,int l,int r,int rt){if (L <= l && r <= R){ col[rt] += c;sum[rt] += c * (r - l + 1);return ;}PushDown(rt , r - l + 1);int m = (l + r) >> 1;if (L <= m) update(L , R , c ,l,m,rt*2);if (m < R) update(L , R , c , m+1,r,rt*2+1);sum[rt]=sum[rt*2]+sum[rt*2+1];}int query(int L,int R,int l,int r,int rt){if (L <= l && r <= R){return sum[rt];}PushDown(rt , r - l + 1);int m = (l + r) >> 1; int ret = 0;if (L <= m) ret += query(L , R , l,m,rt*2);if (m < R) ret += query(L , R ,m+1,r,rt*2+1); return ret;}int main(){int t,i,n,a,b;while(scanf("%d",&n)!=EOF){if(n==0)break;t=n;build(1,n,1);while (t--){scanf("%d%d",&a,&b);update(a,b,1,1,n,1);}printf("%d",query(1,1,1,n,1));for (i=2;i<=n;i++)printf(" %d",query(i,i,1,n,1));printf("\n");}return 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
- 用VBA得到EXCEL表格中的行数和列数
- C++格式化输出
- openstack keystone api 实验(curl)
- VC++6.0调试工具使用初步
- jar命令的使用
- hdu 1556 Color the ball
- html文件中插入PHP代码生效
- 使用mysqldump从5.1升级到5.5的错误
- ndk编程
- Oracle Database 10g 043
- VS2012 +OpenCv2.4.4配置
- NSDate 的一些操作(比较、创建、在现有date加减一定时间等)
- UVA 11077 Find the Permutations 置换+递推
- 九宫图代码