hdu 1556 Color the ball
来源:互联网 发布:fpga用什么软件 编辑:程序博客网 时间:2024/06/11 21:11
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
3
1 1
2 2
3 3
3
1 1
1 2
1 3
0
Sample Output
1 1 1
3 2 1
Author
8600
分析:
这个树状数组用的比较神奇。我们通常计数是一个一个计数,但是树状数组是一块的计数的。
比如说: 对 3~5染色,那么 c[3]+1,c[4]+1,c[8]+1,c[16]+1,……,;
再有 c[6]-1,c[8]-1,c[16]-1,……,
再看 getsum(3)=c[3], getsum(4)=c[3]+c[4】,getsum(5)=c[5]+c[4];
getsum[6]=0; 如果自己画画图,这时候应该清楚树状数组是怎么块计数的;
#include<iostream>#include<algorithm>#include<string>#include<cstring>#include<stdio.h>using namespace std;const int N=1e5+5;int c[N],n;int Lowbit(int x){ return x&(-x);}void update(int pos , int num) { while(pos<=n) { c[pos]+=num; pos+=Lowbit(pos); }}int getSum(int en) { int sum = 0; while(en > 0){ sum += c[en]; en -= Lowbit(en); } return sum;}int main(){ while(scanf("%d",&n),n){ memset(c,0,sizeof(c)); for(int i=1;i<=n;i++){ int a,b; scanf("%d%d",&a,&b); update(a,1); update(b+1,-1); } for(int i=1;i<=n;i++){ if(i==1) printf("%d",c[1]); else printf(" %d",getSum(i)); } printf("\n"); } 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
- C++中数组、链表和vector等容器之间的区别
- VC6调试程序时显示不了CString类的值
- 重载+异常处理+类型转换
- IO流之文件的操作
- 有功功率、无功功率、视在功率、之间的联系
- hdu 1556 Color the ball
- 欢迎使用CSDN-markdown编辑器
- 基于地图的短信追踪小练习(2)百度地图定位到自己的位置
- 目录
- [LeetCode] ZigZag Conversion 解题报告
- Android报错:JNI相关“ Error: NDK integration is deprecated in the current plugin.”
- getaddrinfo()函数详解
- MQ实现两个应用系统之间的通信----理论知识(一)
- IT学习网站总结