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. UVA10228 A Star not a Tree?

    [返回模拟退火略解] 题目描述 一平面上有 nnn 个点 {Ai}\{A_i\}{Ai​},求一个点 XXX 使得σ=∑i=1ndis(Ai,X)\sigma=\sum_{i=1}^{n}{dis(A ...

  2. 不同的phper该如何区别使用swoole和workerman?

       那么我们该怎样去区别应用swoole和workerman?                workerman workerman纯php写的,swoole是php的c扩展,性能肯定更高,百度.腾 ...

  3. Markdown进阶(1)

    对于工科生来说,在书写Markdown文本时,免不了要和上下标打交道,网上的博客大多良莠不齐,不太友好,本文想尽可能地解决一些在看完基础教程后再来书写Markdown文本时容易遇到的问题. 1.上下标 ...

  4. Veins(车载通信仿真框架)入门教程(三)——多跳路由实现指导

    Veins(车载通信仿真框架)入门教程(三)——多跳路由实现指导 Veins(车载通信仿真框架)入门教程(三)——多跳路由实现指导 必要的message类实现 从下面开始是在veins/src/vei ...

  5. 如何让OKR实践变得更简单一些

    什么是OKR 近几年OKR的概念在国内开始流行起来了,之前公司也有人想实施OKR,但现在看来之前的OKR实施者只是在哪儿看了一下OKR的资料,本着跟老板邀功的想法比较功利的在推进,所以基本没有效果,今 ...

  6. 设计模式C++描述----05.职责链(Chain of Responsibility)模式

    一. 概述 职责链模式: 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止. 二. 举个例子 员工要求加薪 ...

  7. vue---Excel表格导出

    一.安装依赖 npm install file-saver --save npm install xlsx --save npm install script-loader --save-dev 二. ...

  8. firefox浏览器播放音频

    之前做的系统,在firefox浏览器下有更好的使用体验.因此要求客户统一使用firefox浏览器,前段时间客户要求在系统中加入音频效果. 在网上查了下,主要用到的标签有<bgsound>, ...

  9. iSCSI 共享存储

         iSCSI(Internet Small Computer System Interface,发音为/ˈаɪskʌzi/),Internet小型计算机系统接口,又称为IP-SAN,是一种基于 ...

  10. python基础-函数作用域

    函数 函数对象 函数是第一类对象 函数名可以被引用 函数名可以当作参数使用 函数名可以当作返回值使用 函数名可以当作容器类型的元素 函数嵌套 嵌套调用:在函数内部中调用函数 嵌套定义:在函数内部中定义 ...