Color the ball

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13645    Accepted Submission(s): 6850

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
 

树状数组搞定的第一题!

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAX = + ;
int c[MAX];
int n;
int lowbit(int k)
{
return k & (-k);
}
void update(int x,int num)
{ while(x <= n)
{
c[x] += num;
x += lowbit(x);
}
}
int sum(int x)
{
int s = ;
while(x > )
{
s += c[x];
x -= lowbit(x);
}
return s;
}
int main()
{
while(scanf("%d", &n) && n)
{
int a,b;
memset(c,,sizeof(c));
for(int i = ; i < n; i++)
{
scanf("%d%d",&a,&b);
update(a,);
update(b + ,-);
}
printf("%d",sum());
for(int i = ; i <= n; i++)
printf(" %d",sum(i));
printf("\n");
}
return ;
}

最新文章

  1. Android随笔之——PackageManager详解
  2. JSP开发模式2(计算器)
  3. Android自定义标题栏
  4. WCF初探-15:WCF操作协定
  5. Yii 框架创建自己的 web 应用
  6. Oracle基础(八) 数据完整性
  7. poj 3616 Milking Time
  8. ALS数学点滴
  9. Android学习----打印日志Log
  10. Lua 变长参数(variable number of arguments)
  11. Struts的使用
  12. java同时连接db2和mysql的程序
  13. Java-ServletContextAttributeListener
  14. JS INPUT输入的时候全角自动转为半角
  15. The xp_cmdshell proxy account information cannot be retrieved or is invalid. Verify that the &#39;##xp_cmdshell_proxy_account##&#39; credential exists and contains valid information.
  16. 【干货】使用SIFT取证工作站校验文件哈希----哈希一致则文件具备完整性
  17. Mac 安装JRE 1.8
  18. centos7安装pip
  19. POJ 1195 Mobile phones【二维树状数组】
  20. Eigen中的map

热门文章

  1. 自己留存:小经验在asp.net 4.5或者asp.net mvc 5解决A potentially dangerous Request.Form value was detected from the client
  2. mutex与semaphore的区别
  3. STL数组处理常用函数
  4. Linux运维初级、中级、高级
  5. 【JavaEE企业应用实战学习记录】getConnListener
  6. 我的第一个jsp程序-实现注册登录留言功能
  7. 【BZOJ 1038】【ZJOI 2008】瞭望塔
  8. JavaScript写一个拼图游戏
  9. poj1743 后缀数组求不可重叠的重复出现的子串最长长度
  10. mysql查看数据库