线段树Codeforces Beta Round #35 (Div. 2)
来源:互联网 发布:淘宝介入有用吗 编辑:程序博客网 时间:2024/05/29 00:29
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 nextm 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
思路:离散化,然后线段树维护区间上的最高值,然后找出每一点的最高值,记录下来就行了
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=100010;int x[maxn*2];int n;vector<pair<int,int> > ans;struct seg{ int l,r,h; seg(){} seg(int a,int b,int c):l(a),r(b),h(c){} bool operator < (const seg & a)const { return h<a.h; }}s[maxn];struct IntervalTree{ int maxv[maxn<<3]; void build() { memset(maxv,0,sizeof(maxv)); } void pushdown(int o,int l,int r) { if(l>=r)return; maxv[o<<1]=max(maxv[o<<1],maxv[o]); maxv[o<<1|1]=max(maxv[o<<1|1],maxv[o]); maxv[o]=0; } void update(int o,int l,int r,int q1,int q2,int val) { if(q1<=l&&r<=q2) { maxv[o]=max(maxv[o],val); return; } pushdown(o,l,r); int mid=(l+r)>>1; if(q1<=mid)update(o<<1,l,mid,q1,q2,val); if(q2>mid)update(o<<1|1,mid+1,r,q1,q2,val); } int query(int o,int l,int r,int pos) { if(l==r)return maxv[o]; pushdown(o,l,r); int mid=(l+r)>>1; if(pos<=mid)return query(o<<1,l,mid,pos); else return query(o<<1|1,mid+1,r,pos); }}tree;int main(){ //freopen("in.txt","r",stdin); scanf("%d",&n); int num=0; for(int i=0;i<n;i++) { int h,l,r; scanf("%d%d%d",&h,&l,&r); x[num++]=l; x[num++]=r; s[i]=seg(l,r,h); } sort(x,x+num); int nx=unique(x,x+num)-x; tree.build(); for(int i=0;i<n;i++) { int l=lower_bound(x,x+nx,s[i].l)-x; int r=lower_bound(x,x+nx,s[i].r)-x-1; tree.update(1,0,nx-1,l,r,s[i].h); } int y=0,tmpy; for(int i=0;i<nx-1;i++) { tmpy=tree.query(1,0,nx-1,i); if(y==tmpy)continue; ans.push_back(make_pair(x[i],y)); ans.push_back(make_pair(x[i],tmpy)); y=tmpy; } if(y!=0) { ans.push_back(make_pair(x[nx-1],y)); ans.push_back(make_pair(x[nx-1],0)); } int len=ans.size(); cout<<len<<endl; for(int i=0;i<len;i++) cout<<ans[i].first<<" "<<ans[i].second<<endl; return 0;}
- 线段树Codeforces Beta Round #35 (Div. 2)
- 线段树Codeforces Beta Round #99 (Div. 1)C
- Codeforces Beta Round #35 (Div. 2)
- 线段树Codeforces Round #136 (Div. 2)
- Codeforces Beta Round #19 D线段树
- Codeforces Beta Round #19D 线段树
- Codeforces Beta Round #35 (Div. 2) C (BFS~水~)
- Codeforces Beta Round #35 (Div. 2), problem: (D) Animals DP
- Codeforces Beta Round #63 (Div. 2)
- Codeforces Beta Round #65 (Div. 2)
- Codeforces Beta Round #67 (Div. 2) ABCD
- Codeforces Beta Round #80 (Div. 2 Only)
- Codeforces Beta Round #83 (Div. 2 Only)
- Codeforces Beta Round #86 (Div. 2 Only)
- Codeforces Beta Round #85 div 2
- Codeforces Beta Round #93 (Div. 2 Only)
- Codeforces Beta Round #95 (Div. 2)
- Codeforces Beta Round #96 (Div. 2)
- SQL中Union与Union All的区别
- 2014年7月微软MVP名单揭晓
- manifest.xml文件
- Leetcode--Add Two Numbers
- hdu-2089-不要62
- 线段树Codeforces Beta Round #35 (Div. 2)
- 页面视频播放器
- POJ 2063 Investment (完全背包)
- ZooKeeper
- Java反射机制
- 不能初始化action Unable to instantiate Action, homePageAction
- hdu 1420 终于知道错哪了,
- Java性能优化(概述)
- poj 2499 Binary Tree(水题)