hdu 1556 Color the ball(树状数组)
2013-08-16 21:38
495 查看
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556
题意:N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数[a,b]之间的气球染一次色,最后问每个气球染了多少种颜色。
分析:这是树状数组的第二种应用,区间成段更新,然后求某点的值。
update(x,num)表示x位置的值增加num,sum(x)表示求1到x的和。
更新[l,r]区间时,
先update(l,k),然后update(r+1,-k),就会导致sum(l)到sum(r)的值均增加了k,然后[1,l)和(r,max)这两个范围内的sum都不变,这样sum(x)就是x这个点当前的值了。
在纸上自己画一画,模拟一下就能明白了。
AC代码如下:
View Code
题意:N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数[a,b]之间的气球染一次色,最后问每个气球染了多少种颜色。
分析:这是树状数组的第二种应用,区间成段更新,然后求某点的值。
update(x,num)表示x位置的值增加num,sum(x)表示求1到x的和。
更新[l,r]区间时,
先update(l,k),然后update(r+1,-k),就会导致sum(l)到sum(r)的值均增加了k,然后[1,l)和(r,max)这两个范围内的sum都不变,这样sum(x)就是x这个点当前的值了。
在纸上自己画一画,模拟一下就能明白了。
AC代码如下:
#include<stdio.h> #include<string.h> #define N 100010 int d ,n; int lowbit(int x) { return x&(-x); } void update(int x,int num) { while(x>0) { d[x]+=num; x-=lowbit(x); } } int sum(int x) { int s=0; while(x<=n) { s+=d[x]; x+=lowbit(x); } return s; } int main() { int i,a,b; while(scanf("%d",&n)&&n) { memset(d,0,sizeof(d)); for(i=0;i<n;i++) { scanf("%d%d",&a,&b); update(a-1,-1); update(b,1); } for(i=1;i<n;i++) printf("%d ",sum(i)); printf("%d\n",sum(n)); } return 0; }
View Code
相关文章推荐
- hdu 1556 Color the ball(树状数组)
- HDU 1556 Color the ball 树状数组-区间更新-模板
- HDU 1556 Color the ball(树状数组)
- POJ 2352 Stars + HDU 1556 Color the ball(树状数组单点更新及伪区间更新)
- hdu 1556 Color the ball (树状数组||线段树成段更新)
- HDU 1556-Color the ball(树状数组-区间修改 单点查询)
- hdu 1556 Color the ball(线段树 or 树状数组 or 前缀和)
- 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 the ball(树状数组)
- [HDU - 1556] Color the ball(树状数组)
- HDU 1556 Color the ball【线段树及lazy思想】【树状数组】
- HDU - 1556 Color the ball(树状数组 or 线段树)
- HDU 1556 Color the ball(树状数组)
- hdu-1556 Color the ball(树状数组)
- HDU 1556 Color the ball 树状数组 题解
- hdu 1556 Color the ball(树状数组)