hdoj-1556-Color the ball
来源:互联网 发布:学党史知国情征文500字 编辑:程序博客网 时间:2024/05/19 17:24
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<cstdio>#include<iostream>#include<cstring>using namespace std;const int MAXN=100010;int c[MAXN];int n;int lowbit(int x){ return x&(-x);}void add(int i,int val){ while(i<=n) { c[i]+=val; i+=lowbit(i); }}int sum(int i){ int s=0; while(i>0) { s+=c[i]; i-=lowbit(i); } return s;}int main(){ int a,b; while(scanf("%d",&n),n) { memset(c,0,sizeof(c)); for(int i=0;i<n;i++) { scanf("%d%d",&a,&b); add(a,1); add(b+1,-1); } for(int i=1;i<n;i++) printf("%d ",sum(i)); printf("%d\n",sum(n)); } return 0;}
0 0
- HDOJ 1556 Color the ball
- HDOJ 1556 Color the ball
- HDOJ 1556 Color the ball
- Color the ball HDOJ--1556
- hdoj 1556 Color the ball
- HDOJ 1556 Color the ball
- HDOJ-1556 Color the ball
- HDOJ 1556 Color the ball
- hdoj-1556-Color the ball
- HDOJ Color the ball
- hdoj Color the ball
- HDoj-1556-Color the ball-树状数组
- hdoj 1556 Color the ball 【树状数组】
- hdoj 1556 Color the ball(树状数组)
- HDOJ Color the ball 1556【树状数组】
- hdoj 1556 Color the ball 【线段树】
- hdoj 1556 Color the ball <区间问题,>
- [HDOJ 1556] Color the ball(线段树成段更新入门)
- TCP流量控制问题
- matlab gui————定时器
- fzu 1894 志愿者选拔(单调队列)
- Java基础-对象导论
- java中volatile关键字的含义
- hdoj-1556-Color the ball
- OpenCV Mat的实现
- 乱七八糟的存在
- EditText焦点自动带出软键盘问题解决方法总结
- 学习前端javascript笔记
- android 同时使用多个checkbox使用同一个状态监听器
- java字符串大写转小写,小写转大写
- 将字符串反转
- 通过POST请求得到数据,并添加到列表显示