Codevs 2995 楼房
2995 楼房
时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold
1 #include<cstdio> 2 #include<queue> 3 #include<algorithm> 4 using namespace std; 5 #define N 100100 6 struct ss{ 7 int h,l,r; 8 bool operator < (const ss &a) const{ 9 if(l==a.l) return h>a.h; 10 return l<a.l; 11 } 12 }e[N<<1]; 13 struct node{ 14 int h,r; 15 node(ss x){ h=x.h;r=x.r; } 16 bool operator < (const node &a) const{ 17 return h<a.h; 18 } 19 }; 20 int n,cnt; 21 pair<int,int>ans[N<<2]; 22 priority_queue<node>que; 23 ss bfs(ss now,int x){ 24 while(!que.empty()){ 25 node nown=que.top();que.pop(); 26 if(nown.r>now.r){ 27 if(nown.h!=now.h){ 28 ans[++cnt]=make_pair(now.r,now.h); 29 ans[++cnt]=make_pair(now.r,nown.h); 30 } 31 now.r=nown.r;now.h=nown.h; 32 } 33 if(now.r>=x) return now; 34 } 35 ans[++cnt]=make_pair(now.r,now.h); 36 ans[++cnt]=make_pair(now.r,0); 37 now.h=0;now.r=0x7fffffff; 38 return now; 39 } 40 void solve(){ 41 ans[++cnt]=make_pair(e[1].l,0); 42 ans[++cnt]=make_pair(e[1].l,e[1].h); 43 ss now=e[1]; 44 for(int i=2;i<=n;i++){ 45 if(now.h>=e[i].h&&now.r>=e[i].l)que.push(e[i]); 46 if(now.h<e[i].h&&now.r>=e[i].l){ 47 que.push(now); 48 ans[++cnt]=make_pair(e[i].l,now.h); 49 now=e[i]; 50 ans[++cnt]=make_pair(e[i].l,now.h); 51 } 52 if(now.r<e[i].l){ 53 now=bfs(now,e[i].l); 54 if(now.h>=e[i].h&&now.r>=e[i].l) 55 que.push(e[i]); 56 if(now.h<e[i].h&&now.r>=e[i].l){ 57 que.push(now); 58 ans[++cnt]=make_pair(e[i].l,now.h); 59 now=e[i]; 60 ans[++cnt]=make_pair(e[i].l,now.h); 61 } 62 } 63 } 64 bfs(now,0x7fffffff); 65 } 66 int main(){ 67 scanf("%d",&n); 68 for(int i=1;i<=n;i++) 69 scanf("%d%d%d",&e[i].h,&e[i].l,&e[i].r); 70 sort(e+1,e+1+n); 71 solve(); 72 printf("%d\n",cnt); 73 for(int i=1;i<=cnt;i++) 74 printf("%d %d\n",ans[i].first,ans[i].second); 75 return 0; 76 }