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. shark恒破解笔记2-绕过自校验

    这集讲的是绕过自校验 主要是通过文件大小的自校验 首先查壳 有壳  可以用esp定律搞定 OD载入  右键od脱裤壳调试进程 可以看到一些信息 包括入口点252F0 修正后地址为252F0 loadP ...

  2. 一个基于C++11的单例模板类

    #ifndef _SINGLETON_H_#define _SINGLETON_H_ template<typename T>class Singleton : public Uncopy ...

  3. opencv::sift特征提取

    SIFT特征检测介绍 SIFT(Scale-Invariant Feature Transform)特征检测关键特性: -建立尺度空间,寻找极值 -关键点定位(寻找关键点准确位置与删除弱边缘) -关键 ...

  4. 微信小程序自定义弹窗(可通用)

    效果图 .wxml <cover-view class='mask' wx:if='{{isShow}}'> <cover-view class='modal'> <co ...

  5. 小白学 Python(10):基础数据结构(列表)(下)

    人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...

  6. SpringBoot 遇到 No identifier specified for entity

    No identifier specified for entity 从字面上不难看出来是没有设置主键 因为没有为标注为@Entity的实体类注明主键 import lombok.Data; impo ...

  7. Visual Studio Code 添加C/C++编译功能

    VS Code作为一个文本/代码编辑器,相较于VS比较轻量化,而且可以支持C/C++.Python等多种语言,并具有丰富的拓展模块. 但是作为一个编辑器,在VS Code上安装C/C++模块之后,并不 ...

  8. scp -本地文件上传服务器,指定端口

    scp 命令可以将本地文件上传服务器,或者将服务器上的文件下载到本地, 1.  上传服务器: scp [本地文件目录]  [服务器用户名]@[服务器名]:/[服务器上文件路径] 比如 scp /Doc ...

  9. js奥义:原型与原型链(2)

    回顾:上一篇讲了原型对象与prototype和__proto__(传送门 )三者之间的关系 三:constructor constructor [kənˈstrʌktə(r)] :构造器,  这是子类 ...

  10. django-URL别名的作用(六)

    接include函数那一节. 作用:为url地址取一个名称,这样在html中引用的时候,无论后台url怎么变,都可以访问到对应的界面,可以减少更改的次数. 基本目录: book\urls.py fro ...