Color the ball
Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 28415    Accepted Submission(s): 13851
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

C/C++(线段树):

 #include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 5e5 + ; int n, a, b; struct nod
{
int L, R, val;
}node[MAX]; void build(int d, int l, int r)
{
node[d].L = l, node[d].R = r, node[d].val = ;
if (l == r) return;
int mid = (l + r) >> ;
build(d << , l, mid);
build((d << ) + , mid + , r);
} void update(int d, int l, int r)
{
if (node[d].L == l && node[d].R == r)
{
node[d].val ++;
return;
}
if (node[d].L == node[d].R) return;
int mid = (node[d].L + node[d].R) >> ;
if (r <= mid)
update(d << , l, r);
else if (mid < l)
update((d << ) + , l, r);
else
{
update(d << , l, mid);
update((d << ) + , mid + , r);
}
} int query(int d, int l, int r)
{
if (node[d].L == l && node[d].R == r) return node[d].val;
if (node[d].L == node[d].R) return ;
int mid = (node[d].L + node[d].R) >> ;
if (r <= mid)
return node[d].val + query(d << , l, r);
else if (mid < l)
return node[d].val + query((d << ) + , l, r);
else
return node[d].val + query(d << , l, mid) + query((d << ) + , mid + , r);
} int main()
{
while (scanf("%d", &n), n)
{
build(, , n);
for (int i = ; i < n; ++ i)
{
scanf("%d%d", &a, &b);
update(, a, b);
}
for (int i = ; i < n; ++ i)
printf("%d ", query(, i, i));
printf("%d\n", query(, n, n));
}
return ;
}

C/C++(技巧):

 #include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 1e5 + ; int n, a, b; int main()
{
while (scanf("%d", &n), n)
{
int num[MAX] = {}, m = ;
for (int i = ; i <= n; ++ i)
{
scanf("%d%d", &a, &b);
num[a] ++, num[b + ] --;
}
for (int i = ; i < n; ++ i)
{
m += num[i];
printf("%d ", m);
}
printf("%d\n", m + num[n]);
}
return ;
}

hdu 1556 Color the ball (技巧 || 线段树)的更多相关文章

  1. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. HDU 1556 Color the ball(线段树:区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意: N个气球,每次[a,b]之间的气球涂一次色,统计每个气球涂色的次数. 思路: 这道题目用树状数组和 ...

  3. hdu 1556 Color the ball (线段树做法)

    Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a ...

  4. hdu 1556 Color the ball(非线段树做法)

    #include<stdio.h> #include<string.h> ]; int main() { int n,i; int a,b; while(scanf(" ...

  5. hdoj 1556 Color the ball【线段树区间更新】

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. hdu 1199 Color the Ball(离散化线段树)

    Color the Ball Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  7. ZOJ 2301 / HDU 1199 Color the Ball 离散化+线段树区间连续最大和

    题意:给你n个球排成一行,初始都为黑色,现在给一些操作(L,R,color),给[L,R]区间内的求染上颜色color,'w'为白,'b'为黑.问最后最长的白色区间的起点和终点的位置. 解法:先离散化 ...

  8. hdu 1199 Color the Ball 离散线段树

    C - Color the Ball Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  9. HDU 1556 Color the ball (一维树状数组,区间更新,单点查询)

    中文题,题意就不说了 一开始接触树状数组时,只知道“单点更新,区间求和”的功能,没想到还有“区间更新,单点查询”的作用. 树状数组有两种用途(以一维树状数组举例): 1.单点更新,区间查询(即求和) ...

随机推荐

  1. PHP pa和ma

    <?php class Mouse { private $color; public $sex; public function __construct($role){ switch($role ...

  2. 用JavaScript制作banner轮播图

    JavaScript_banner轮播图 让我们一起来学习一下用js怎么实现banner轮播图呢? 直接看代码: <!DOCTYPE html> <html> <head ...

  3. boost::VS2017下编译和配置boost库

    环境: win10  vs2017  v141 1.下载  boost_1_70_0.zip. 2.以管理员方式打开 3. bootstrap.bat 4.编译64位库 b2.exe stage -- ...

  4. libevent::事件::定时器

    #include <cstdio> #include <errno.h> #include <sys/types.h> #include <event.h&g ...

  5. Java根据参数返回相应类

    问题初衷:如何根据参数变换方法的返回类型(参数为 类) 解决方案: 下面方法是放在工具类(例:YslRequestUtil) public <T> T response(Object re ...

  6. Java11新特性 - 新加一些实用的API

    1. 新的本机不可修改集合API 自从Java9开始,JDK里面为集合(List/Set/Map)都添加了of和copyOf方法,他们可以来创建不可变的集合. Question1:什么叫做不可变集合? ...

  7. 从零开始把项目发布到Nuget仓库中心

    从零开始把项目发布到Nuget仓库中心 我的项目地址 https://github.com/Ants-double/dasuan ### 前期准备 下载并注册nuget帐号 下载地址 https:// ...

  8. Centos 新建用户

    Centos 新建用户 为什么要新建用户? 因为root的权限太多,不方便多人多角色使用,所以添加一个用户 添加用户 新建用户 adduser '用户名' 添加用户密码 passwd '用户名' 输入 ...

  9. 记录面试龙腾简合-java开发工程师经历

    /** * ############ * 变强是会掉光头发的!现在的头发还是很茂盛,是该开心还是难过呢.. * ############ * / 总结下近期面试龙腾简合-java开发岗的经历.附上笔试 ...

  10. java script三大组成部分

    JavaScript是一种属于网络的脚本语言,已经被广泛用于Web应用开发,常用来为网页添加各式各样的动态功能,为用户提供更流畅美观的浏览效果.通常JavaScript脚本是通过嵌入在HTML中来实现 ...