[树状数组]HDU1556 Color the ball
来源:互联网 发布:算法实现约瑟夫环c语言 编辑:程序博客网 时间:2024/05/19 16:20
G - Color the ball
Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
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
使用的树状数组改段求点的模板,个人感觉这是非常奇妙的一种方法,也许是对树状数组还没有深刻理解。。回去好好看一看是怎样把改点求段的线段树改成这种模式的。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int n,L,R;int b[100010]; // 辅助数组void ADD(int x, int c)//将s[1]~s[x]的值全部加上c{ for (int i=x; i>0; i-=i&(-i)) b[i] += c;}int SUM(int x)//求a[x]的和{ int s = 0; for (int i=x; i<=n; i+=i&(-i)) s += b[i]; return s;}int main(){ while(scanf("%d",&n) != EOF) { if (n==0) break; memset(b,0,sizeof(b)); for(int i =1; i<=n; i++) { scanf("%d%d",&L,&R); ADD(R,1); ADD(L-1,-1); } int i; for(i =1; i<n; i++) printf("%d ",SUM(i)); printf("%d\n",SUM(i)); } return 0;}
0 0
- hdu1556 Color the ball (树状数组)
- HDU1556 Color the ball (树状数组)
- [树状数组]HDU1556 Color the ball
- hdu1556 Color the ball(树状数组)
- Hdu1556 Color the ball [分块][树状数组]
- [HDU1556] Color the ball - 树状数组
- HDU1556 Color the ball (树状数组)
- 【HDU1556】Color the ball(树状数组)
- hdu1556 Color the ball 树状数组
- 【HDU1556-color the ball】(树状数组)
- HDU1556 color the ball (树状数组)
- 树状数组:HDU1556 Color the ball 【区间更新,单点查询]
- HDU1556 color the ball【树状数组】解题报告
- HDU1556 Color the ball【树状数组】【区间更新】
- HDU1556 color the ball(树状数组)向下查询,向上统计
- hdu1556 Color the ball(树状数组 修改区间,查找值)
- HDU1556 Color the ball(树状数组+抖机灵)
- 【树状数组-区间更新】hdu1556 Color the ball
- 浮点数的二进制表示学习笔记
- 知识储备:计算机网络
- poj1404题解
- 【BZOJ】1015 星球大战
- C++析构函数
- [树状数组]HDU1556 Color the ball
- 数字组合
- Javaweb传参乱码
- U盘启动显示Starting cmain()...
- java 输入与输出
- IOS学习之蓝牙4.0
- 数据结构与算法(5、链表的拼接)
- Oracle数据库应用(一)
- Vim正则表达式