hdu 1556 Color the ball
来源:互联网 发布:seo实战密码电子书 编辑:程序博客网 时间:2024/06/05 18:26
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
【分析】
不用多说了,树状数组,其实是看了别人写的,对着敲得,想了想总算是明白了,其实很简单,主要就是那个连续用两个add是为啥,后来明白过来,是因为后一个add直接抵消了之前一个add造成的负面效果。然后结果就直接可以统计了。
【代码】
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int N = 100000 + 10;int tree[N];void add(int ind,int val){ while(ind<=N) { tree[ind]+=val; ind+=ind&(-ind); }}int sum(int ind){ int sum=0; while(ind>0) { sum+=tree[ind]; ind-=ind&(-ind); } return sum;}int main() { freopen("in.txt","r",stdin); int c; while(scanf("%d",&c),c) { memset(tree,0,sizeof(tree)); int a,b; for(int i=0;i<c;i++) { scanf("%d%d",&a,&b); add(a,1); add(b+1,-1); } for(int i=1;i<=c;i++) { printf(i==c?"%d\n":"%d ",sum(i)); } } return 0;}
阅读全文
0 0
- hdu 1556 color the ball
- hdu 1556Color 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
- 向搜索引擎提交网站的入口及技巧
- 狂热的ICO,跟进的监管
- github的角色和人员结构
- 知识图谱插件vis.min.js
- 正则表达式学习(一)
- hdu 1556 Color the ball
- QT-点击关闭按钮时提示是否退出
- mysql 死锁场景分析
- apk的位置
- ajax是可以本地运行的
- 希尔排序
- 在Spring中配置Quartz
- 字符串难题
- Android 代码中AddView 使用anko