hdu 1556 Color the ball
2013-08-07 16:10
423 查看
Color the ball
[b]Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5970 Accepted Submission(s): 3166
[/b]
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,输入结束。
Output
每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
Sample Input
3
1 1
2 2
3 3
3
1 1
1 2
1 3
0
Sample Output
1 1 1
3 2 1
AC代码
#include <iostream> using namespace std; #define maxn 100000 #define lson l,m,rt*2 #define rson m+1,r,rt*2+1 int 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 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——T 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
- Color the ball - HDU 1556 简单树状数组
- hdu-1556 Color the ball && nyoj -123 士兵杀敌(四)----------》树状数组