【CodeForces】35E Parade 线段树
来源:互联网 发布:淘宝宝贝裂变软件免费 编辑:程序博客网 时间:2024/05/17 01:39
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 onn 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, thei-th building is a rectangle of height hi. Its westernmost point has the x-coordinate ofli and the easternmost — ofri. 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 anenveloping polyline 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.
The first input line contains integer n (1 ≤ n ≤ 100000). Then follown lines, each containing three integers hi, li, ri (1 ≤ hi ≤ 109, - 109 ≤ li < ri ≤ 109).
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.
23 0 24 1 3
60 00 31 31 43 43 0
53 -3 02 -1 14 2 42 3 73 6 8
14-3 0-3 30 30 21 21 02 02 44 44 26 26 38 38 0
传送门:【CodeForces】35E Parade
题目大意:在二维平面上,给你每个建筑的左右坐标以及高度,问你建筑外部轮廓的转折点。
题目分析:离散化+线段树随便搞。
PS:昨天就写好了,然后一直错在第一组数据简直不明觉厉,后来发现早期CF竟然是文件输入输出!然后我不知道还以为CF坏了,顿时感觉自己萌萌哒QUQ。。。
代码如下:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std ;#define ls ( o << 1 )#define rs ( o << 1 | 1 )#define rt o , l , r#define root 1 , 1 , n#define lson ls , l , m#define rson rs , m + 1 , r#define mid ( ( l + r ) >> 1 )#define clear( A , X ) memset ( A , X , sizeof A )const int maxN = 300005 ;struct Line {int l , r , h ;Line () {}void input () {scanf ( "%d%d%d" , &h , &l , &r ) ;}bool operator < ( const Line &t ) const {return h < t.h ;}} ;struct Node {int x , y ;} ;Line line[maxN << 1] ;Line seg[maxN << 1] ;Node ans[maxN << 1] ;int set[maxN << 2] ;int a[maxN << 2] ;int Unique ( int a[] , int n ) {int cnt = 1 ;sort ( a + 1 , a + n + 1 ) ;for ( int i = 2 ; i <= n ; ++ i ) if ( a[i] != a[cnt] ) a[++ cnt] = a[i] ;return cnt ;}int Again_Unique ( Line line[] , int n ) {int cnt = 1 ;for ( int i = 2 ; i <= n ; ++ i ) {if ( line[i].h == line[cnt].h ) {line[cnt].r = line[i].r ;}else line[++ cnt] = line[i] ;}return cnt ;}int Search ( int x , int l , int r ) {while ( l < r ) {int m = mid ;if ( a[m] >= x ) r = m ;else l = m + 1 ;}return l ;}void PushDown ( int o ) {if ( set[o] ) {set[ls] = set[rs] = set[o] ;set[o] = 0 ;}}void Update ( int L , int R , int v , int o , int l , int r ) {if ( L <= l && r <= R ) {set[o] = v ;return ;}PushDown ( o ) ;int m = mid ;if ( L <= m ) Update ( L , R , v , lson ) ;if ( m < R ) Update ( L , R , v , rson ) ;}void Query ( int o , int l , int r ) {if ( l == r ) {line[l].l = a[l] ;line[l].r = a[r + 1] ;line[l].h = set[o] ;return ;}PushDown ( o ) ;int m = mid ;Query ( lson ) ;Query ( rson ) ;}void work () {//题目需要文件输入输出freopen ( "input.txt" , "r" , stdin ) ;freopen ( "output.txt" , "w" , stdout ) ;int n , cnt = 0 ;while ( ~scanf ( "%d" , &n ) ) {clear ( set , 0 ) ;for ( int i = 1 ; i <= n ; ++ i ) {seg[i].input () ;a[++ cnt] = seg[i].l ;a[++ cnt] = seg[i].r ;}cnt = Unique ( a , cnt ) ;sort ( seg + 1 , seg + n + 1 ) ;for ( int i = 1 ; i <= n ; ++ i ) {int L = Search ( seg[i].l , 1 , cnt + 1 ) ;int R = Search ( seg[i].r , 1 , cnt + 1 ) ;Update ( L , R - 1 , seg[i].h , 1 , 1 , cnt - 1 ) ;}Query ( 1 , 1 , cnt - 1 ) ;cnt = Again_Unique ( line , cnt - 1 ) ;int __cnt = 0 ;ans[__cnt].x = line[1].l ;ans[__cnt].y = 0 ;++ __cnt ;for ( int i = 1 ; i <= cnt ; ++ i ) {ans[__cnt].x = line[i].l ;ans[__cnt].y = line[i].h ;++ __cnt ;ans[__cnt].x = line[i].r ;ans[__cnt].y = line[i].h ;++ __cnt ;}ans[__cnt].x = line[cnt].r ;ans[__cnt].y = 0 ;printf ( "%d\n" , __cnt + 1 ) ;for ( int i = 0 ; i <= __cnt ; ++ i ) printf ( "%d %d\n" , ans[i].x , ans[i].y ) ;}}int main () {work () ;return 0 ;}
- 【CodeForces】35E Parade 线段树
- Parade - CodeForces #35 (Div. 2) E 线段树
- Codeforces 35E Parade(扫描线 线段树||STL)
- codeforces 35E Parade 扫描线+线段树+离散化+STL
- CodeForces 35E Parade(线段树区间更新+离散化)
- Codeforces 35E. Parade (非递归线段树 离散化)
- Codeforces 35E Parade 扫描线 + list
- CodeforcesBeta Round #35 (Div. 2) E. Parade(线段树区间更新)
- CodeForces - 292E 线段树
- codeforces 400e 线段树
- 线段树 CodeForces 580E
- codeforces 413E 线段树
- codeforces 242E(线段树)
- Codeforces Beta Round #35 (Div. 2) E. Parade(成段更新)
- codeforces 111 160E 线段树
- Codeforces 384E 线段树+dfs序
- CodeForces 292E Copying Data (线段树)
- codeforces 474E Pillars(线段树+dp)
- Android休眠唤醒驱动流程分析(三)
- 九、CCAction
- Linux下使用rsync 同步文件夹
- iOS多线程的初步研究(九)-- dispatch源
- I/O复用模型之epoll学习
- 【CodeForces】35E Parade 线段树
- scala模式匹配和正则表达式
- ArrayList实现反转
- GDC dispatch_semaphore
- js正则表达式语法
- 【C语言复习(二十九)】可变参数的函数
- Validate and Format North American Phone Numbers (验证和格式化北美电话号码)
- 常用宏整理
- 机器学习之模型评估与模型选择(学习笔记)