hdu-1556-Color the ball(树状数组)
来源:互联网 发布:深入浅出数据分析 txt 编辑:程序博客网 时间:2024/05/22 14:36
Color the ball
Time Limit : 9000/3000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 17 Accepted Submission(s) : 13
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
31 12 23 331 11 21 30
Sample Output
1 1 13 2 1
题意:更新区间查点;
思路:树状数组,,左端点加1,在右端点后面减一。这样求和得到的就是这个点的染色次数了。
code:
#include<cstdio>#include<cstring>#define lowbit(x) (x&(x^(x-1)))#define maxn 100000+10using namespace std;int c[maxn];int n;void update(int p,int x)///给 a[p] 加上 x{ while(p<=n){ c[p]+=x; p+=lowbit(p); }}int sum(int p) ///求前P项的和{ int sum=0; while(p>0){ sum+=c[p]; p-=lowbit(p); } return sum;}int main(){ while(scanf("%d",&n),n){ memset(c,0,sizeof(c)); int a,b; for(int i=0;i<n;i++){ scanf("%d %d",&a,&b); update(a,1); update(b+1,-1); } for(int i=1;i<n;i++){ printf("%d ",sum(i)); } printf("%d\n",sum(n)); } return 0;}
阅读全文
1 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 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(树状数组)
- PRML中文版(马春鹏)勘误表
- 【PHP学习】Win下安装PEAR
- 配置局域网YUM源
- 算法训练 进制转换(2转10)
- android 蓝牙通信编程
- hdu-1556-Color the ball(树状数组)
- BZOJ1006 神奇的国度 【弦图染色——最大势算法MCS】
- 二叉树面试题-镜像与判断是否为完全二叉树
- 理解linux 的 inode][解决inode耗尽问题
- Windows 7
- 理解RESTful架构
- "error while loading shared libraries: xxx.so.x" 错误的原因和解决办法
- mysqldump 备份命令使用整理
- leetCode-Valid Triangle Number