如题,代码如下:
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn=1e5+5;
int n,h[maxn],a[maxn],b[maxn],p[2*maxn],t[4*maxn],tot=0;
inline void update(int o,int l,int r,int x,int y,int k){
if(l==r){
t[o]=max(k,t[o]);
return ;
}
int mid=(l+r)>>1;
t[o*2]=max(t[o],t[o*2]),t[o*2+1]=max(t[o],t[o*2+1]);
if(x<=mid) update(o*2,l,mid,x,y,k);
if(y>mid) update(o*2+1,mid+1,r,x,y,k);
}
inline int query(int o,int l,int r,int k){
if(l==r) return t[o];
int mid=(l+r)>>1;
t[o*2]=max(t[o],t[o*2]),t[o*2+1]=max(t[o],t[o*2+1]);
if(k<=mid) return query(o*2,l,mid,k);
return query(o*2+1,mid+1,r,k);
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d%d%d",&h[i],&a[i],&b[i]);
p[++p[0]]=a[i],p[++p[0]]=b[i];
}
sort(p+1,p+1+p[0]);
p[0]=unique(p+1,p+1+p[0])-(p+1);
for(int i=1,y1,y2;i<=n;++i){
y1=lower_bound(p+1,p+1+p[0],a[i])-p;
y2=lower_bound(p+1,p+1+p[0],b[i])-p;
update(1,1,p[0],y1,y2-1,h[i]);
}
int preh=0,curh=0;//previous high & current high
a[++tot]=p[1],b[tot]=0;
for(int i=1;i<p[0];++i){
curh=query(1,1,p[0],i);
if(curh==preh) a[tot]=p[i+1];
else{
a[++tot]=p[i],b[tot]=curh;
a[++tot]=p[i+1],b[tot]=curh;
}
preh=curh;
}
printf("%d\n",tot+1);
for(int i=1;i<=tot;++i) printf("%d %d\n",a[i],b[i]);
printf("%d 0\n",p[p[0]]);
return 0;
}