E. Parade

time limit per test:2 seconds
memory limit per test:64 megabytes
input:input.txt
output:output.txt

No Great Victory anniversary in Berland has ever passed without the war parade. This year is not an exception. That’s why the preparations are on in full strength. Tanks are building a line, artillery mounts are ready to fire, soldiers are marching on the main square... And the air forces general Mr. Generalov is in trouble again. This year a lot of sky-scrapers have been built which makes it difficult for the airplanes to fly above the city. It was decided that the planes should fly strictly from south to north. Moreover, there must be no sky scraper on a plane’s route, otherwise the anniversary will become a tragedy. The Ministry of Building gave the data on n sky scrapers (the rest of the buildings are rather small and will not be a problem to the planes). When looking at the city from south to north as a geometrical plane, the i-th building is a rectangle of height hi. Its westernmost point has the x-coordinate of li and the easternmost — of ri. The terrain of the area is plain so that all the buildings stand on one level. Your task as the Ministry of Defence’s head programmer is to find an envelopingpolyline using the data on the sky-scrapers. The polyline’s properties are as follows:

  • If you look at the city from south to north as a plane, then any part of any building will be inside or on the boarder of the area that the polyline encloses together with the land surface.
  • The polyline starts and ends on the land level, i.e. at the height equal to 0.
  • The segments of the polyline are parallel to the coordinate axes, i.e. they can only be vertical or horizontal.
  • The polyline’s vertices should have integer coordinates.
  • If you look at the city from south to north the polyline (together with the land surface) must enclose the minimum possible area.
  • The polyline must have the smallest length among all the polylines, enclosing the minimum possible area with the land.
  • The consecutive segments of the polyline must be perpendicular.

Picture to the second sample test (the enveloping polyline is marked on the right).

Input

The first input line contains integer n (1 ≤ n ≤ 100000). Then follow n lines, each containing three integers hiliri(1 ≤ hi ≤ 109,  - 109 ≤ li < ri ≤ 109).

Output

In the first line output integer m — amount of vertices of the enveloping polyline. The next m lines should contain 2 integers each — the position and the height of the polyline’s vertex. Output the coordinates of each vertex in the order of traversing the polyline from west to east. Remember that the first and the last vertices of the polyline should have the height of 0.

Examples

input

2
3 0 2
4 1 3

output

6
0 0
0 3
1 3
1 4
3 4
3 0

input

5
3 -3 0
2 -1 1
4 2 4
2 3 7
3 6 8

output

14
-3 0
-3 3
0 3
0 2
1 2
1 0
2 0
2 4
4 4
4 2
6 2
6 3
8 3
8 0
 //2017-08-11
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <vector> using namespace std; int n;
multiset<int> ms;
vector< pair<int, int> > line, ans; void init()
{
ms.clear();
line.clear();
ans.clear();
} int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
while(scanf("%d", &n)!=EOF)
{
init();
int l, r, h;
for(int i = ; i < n; i++){
scanf("%d%d%d", &h, &l, &r);
line.push_back(make_pair(l, h));
line.push_back(make_pair(r, -h));
}
sort(line.begin(), line.end());
vector< pair<int, int> >::const_iterator it, iter;
it = line.begin();
int height = ;
ms.insert(height);
while(it != line.end()){
iter = it;
do{
if(iter->second > )
ms.insert(iter->second);
else
ms.erase(ms.find(-iter->second));
iter++;
}while(iter != line.end() && iter->first == it->first);
if(*ms.rbegin() != height){
ans.push_back(make_pair(it->first, height));
ans.push_back(make_pair(it->first, height = *ms.rbegin()));
}
it = iter;
}
printf("%d\n", (int)ans.size());
for(it = ans.begin(); it != ans.end(); it++){
printf("%d %d\n", it->first, it->second);
}
}
}

Codeforces35E(扫描线)的更多相关文章

  1. 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)

    D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

  2. Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)

    题目链接:http://codeforces.com/contest/522/problem/D 题目大意:  给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...

  3. HUD 4007 [扫描线][序]

    /* 大连热身B题 不要低头,不要放弃,不要气馁,不要慌张 题意: 坐标平面内给很多个点,放置一个边长为r的与坐标轴平行的正方形,问最多有多少个点在正方形内部. 思路: 按照x先排序,然后确定x在合法 ...

  4. Atitit 图像扫描器---基于扫描线

    Atitit 图像扫描器---基于扫描线 调用范例 * @throws FileExistEx */ public static void main(String[] args) throws Fil ...

  5. 扫描线+堆 codevs 2995 楼房

    2995 楼房  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 地平线(x轴)上有n个矩(lou)形(fan ...

  6. 【BZOJ-4059】Non-boring sequences 线段树 + 扫描线 (正解暴力)

    4059: [Cerc2012]Non-boring sequences Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 440  Solved: 16 ...

  7. 【BZOJ-4422】Cow Confinement 线段树 + 扫描线 + 差分 (优化DP)

    4422: [Cerc2015]Cow Confinement Time Limit: 50 Sec  Memory Limit: 512 MBSubmit: 61  Solved: 26[Submi ...

  8. 【POJ-2482】Stars in your window 线段树 + 扫描线

    Stars in Your Window Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11706   Accepted:  ...

  9. 线段树基础模板&&扫描线

    线段树的单点更新+区间求和 hdu1166敌兵布阵 Input 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N<=),表示敌人有N个工兵营地 ,接下来有N个正整数,第i个正整 ...

随机推荐

  1. [Ynoi2016]这是我自己的发明(莫队)

    话说这道题数据是不是都是链啊,我不手动扩栈就全 \(RE\)... 不过 \(A\) 了这题还是很爽的,通过昨晚到今天早上的奋斗,终于肝出了这题 其实楼上说的也差不多了,就是把区间拆掉然后莫队瞎搞 弱 ...

  2. python 数据类型一 (重点是字符串的各种操作)

    一.python基本数据类型 1,int,整数,主要用来进行数学运算 2,bool,布尔类型,判断真假,True,False 3,str,字符串,可以保存少量数据并进行相应的操作(未来使用频率最高的一 ...

  3. 脱壳系列—— 揭开so section加密的美丽外衣

    i春秋作家:HAI_ 0×00 前言 对so的加密,https://bbs.pediy.com/thread-191649.htm大神的帖子里已经很详细的说明了.当然加密不是我们研究的重点,如何搞掉这 ...

  4. Linux巩固记录(8) Hbase shell 基本使用

    继续前几篇内容,讲解hbase基本使用 1.进入hbase shell: hbase有很多种操作方式,比如shell,java客户端,webUI等,可以直接输入hbase进行提示 [root@mast ...

  5. js基于json的级联下拉框

    级联下拉列表是项目中常用到的.比如省市县,比如企业性质等,做成一个js通用组件, 在静态页出来后可以直接插入,将数据和html静态页做一个解耦. 贴出来抛砖引玉吧. /** * @author sun ...

  6. golang 并发顺序输出数字

    参考 package main import ( "fmt" "sync/atomic" "time" ) func main() { va ...

  7. 【sping揭秘】13、Spring AOP一世&二世

    Spring AOP一世 Spring AOP中的advice Before advice 这个就是在方法执行之前执行,也就是在对应的joinpoint之前 spring AOP二世 开启aspect ...

  8. vue教程1-08 交互 get、post、jsonp

    vue教程1-08 交互 get.post.jsonp 一.如果vue想做交互,引入: vue-resouce 二.get方式 1.get获取一个普通文本数据: <!DOCTYPE html&g ...

  9. String、StringBuffer与StringBuilder之间区别 (转载)

    最近学习到StringBuffer,心中有好些疑问,搜索了一些关于String,StringBuffer,StringBuilder的东西,现在整理一下. 关于这三个类在字符串处理中的位置不言而喻,那 ...

  10. Microsoft Azure Storage架构分析

    Microsoft云存储服务分为两个部分,SQL Azure和Azure Storage.云存储系统的可扩展性和功能不可兼得,必须牺牲一定的关系数据库功能换取可扩展性.Microsoft实现云存储的思 ...