[CF35E] Parade

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 enveloping 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.

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 hi, li, ri (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

题目翻译
现在有一堆长方形放在一起,给你每个长方形的起点坐标 l i l_i ,终点坐标 r i r_i 和高度 h i h_i ,让你描述这些长方形放在一起的轮廓

这道题首先因为 l i l_i , r i r_i 都很大,而且有负数,所以我们要离散化

然后呢?

我们发现最后能够被看到的一定是高度最高的,所以我们用线段树来维护区间最大值,然后依次访问每一个点,如果和上一个不同,那么就要折一下

但是有一些细节,比如说对于这种情况

在这里插入图片描述
右边的那个→_→

在0-1之间的时候,我们访问到0的时候,他的高度和-1都是3,所以我们会把转折的位置算到1那里,那么这样就会出现错误,那么为了避免这种情况,我们要把线段树的每一个叶子结点表示两段,就是说每个叶子结点相当于两个位置,我们每次查询的时候就查询这两个中间的就好了

复杂度:离散化 O ( n l o g n ) O(nlogn) ,更新 O ( n l o g n ) O(nlogn) ,查询 O ( n l o g n ) O(nlogn) ,共计$O(nlogn)

# include <cstdio>
# include <algorithm>
# include <cstring>
# include <cmath>
# include <climits>
# include <iostream>
# include <string>
# include <queue>
# include <stack>
# include <vector>
# include <set>
# include <map>
# include <cstdlib>
# include <ctime>
using namespace std;

# define Rep(i,a,b) for(int i=a;i<=b;i++)
# define _Rep(i,a,b) for(int i=a;i>=b;i--)
# define RepG(i,u) for(int i=head[u];~i;i=e[i].next)

typedef long long ll;
const int N=1e5+5;
const int inf=0x7fffffff;
const double eps=1e-7;
template <typename T> void read(T &x){
	x=0;int f=1;
	char c=getchar();
	for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+c-'0';
	x*=f;
}

int n,sz,tot;
int b[N];
pair<int,int> ans[N];

struct Position{
	int l,r,h;	
}q[N];

struct segment_tree{
	int l,r,val;	
}seg[N<<2];

# define lc (u<<1)
# define rc (u<<1|1)

void build(int u,int l,int r){
	seg[u].l=l,seg[u].r=r;
	seg[u].val=0;
	if(l+1==r)return;
	int mid=l+r>>1;
	build(lc,l,mid);
	build(rc,mid,r);
}

inline void pushdown(int u){
	if(seg[u].val){
		seg[lc].val=max(seg[lc].val,seg[u].val);
		seg[rc].val=max(seg[rc].val,seg[u].val);
		seg[u].val=0;
	}
}

void update(int u,int l,int r,int k){
	if(seg[u].l>=l&&seg[u].r<=r){seg[u].val=max(seg[u].val,k);return;}
	pushdown(u);
	int mid=seg[u].l+seg[u].r>>1;
	if(l<mid)update(lc,l,r,k);
	if(r>mid)update(rc,l,r,k);
}

int query(int u,int l,int r){
	if(seg[u].l+1==seg[u].r)return seg[u].val;
	pushdown(u);
	int mid=seg[u].l+seg[u].r>>1;
	if(l<mid)return query(lc,l,r);
	else return query(rc,l,r);
}

int main()
{
	read(n);
	Rep(i,1,n)read(q[i].h),read(q[i].l),read(q[i].r),b[i]=q[i].l,b[i+n]=q[i].r;
	sort(b+1,b+2*n+1);
	sz=unique(b+1,b+2*n+1)-b-1;
	Rep(i,1,n)q[i].l=lower_bound(b+1,b+sz+1,q[i].l)-b;
	Rep(i,1,n)q[i].r=lower_bound(b+1,b+sz+1,q[i].r)-b;
	build(1,1,sz);
	Rep(i,1,n)update(1,q[i].l,q[i].r,q[i].h);
	int lst=0;
	Rep(i,1,sz){
		int now=query(1,i,i+1);
		if(now==lst)continue;
			ans[++tot]=make_pair(b[i],lst);
			ans[++tot]=make_pair(b[i],now);
		lst=now;
	}
	if(lst!=0){
		ans[++tot]=make_pair(b[sz],lst);
		ans[++tot]=make_pair(b[sz],0);	
	}
	printf("%d\n",tot);
	Rep(i,1,tot)printf("%d %d\n",ans[i].first,ans[i].second);
	return 0;
}
发布了21 篇原创文章 · 获赞 8 · 访问量 1616

猜你喜欢

转载自blog.csdn.net/devout_/article/details/104083387