hdu1556 Color the ball 树状数组
来源:互联网 发布:溢价域名 编辑:程序博客网 时间:2024/06/14 10:14
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
Hint
题意
题解:
树状数组更新段落值
不更新的位置在查询时会在Query里加上一开始更新却没更新到它的位置的值
EMMMM
其实就是更新区间左端端点的值为1和更新右端点之后的值为-1 询问的时候求1-i的前缀和就行
AC代码
#include<stdio.h>#include <math.h>#include <cstring>#include<algorithm>using namespace std;const int MAXN = 1e5+5;int a[MAXN];int n;void UpdateSum(int x,int y){ while (x <= n){ a[x] += y; x += x&(-x); }}int Query(int x){ int s = 0; while (x > 0){ s += a[x]; x -= x&(-x); } return s; }int main(){ while (scanf("%d",&n),n){ int aa,bb; memset(a,0,sizeof(a)); for (int i = 1;i <= n; ++i){ scanf("%d%d",&aa,&bb); UpdateSum(aa,1); UpdateSum(bb+1,-1); } for (int i = 1;i <= n; ++i){ printf("%d%c",Query(i),i==n?'\n':' '); } } 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
- Java八大排序算法之希尔排序
- 并查集:poj1611
- bzoj 2732 射箭 半平面交 解题报告
- C#调用C++库,如何捕获异常
- Python实现更相减损术求解最大公约数
- hdu1556 Color the ball 树状数组
- 2017CCPC 网络选拔赛 1003 Friend-Graph
- Java用户线程和守护线程
- MyEclipse+Tomcat+MAVEN+SVN项目完整环境搭建
- Csharp零基础:方法
- Unity基本操作认识
- 访问一个给定的元素找到其元素和子元素
- 【物联网云端对接-2】通过MQTT协议与阿里云物联网套件进行云端通信
- hdu1285(拓扑排序)