codeforces 35E Parade 扫描线+线段树+离散化+STL

time limit per test
4 seconds
memory limit per test
64 megabytes
input
input.txt
output
output.txt

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 hiliri (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.

Sample test(s)
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

题意:求轮廓线上的点

解法一:我们将沿y轴的线段按x从小到大排序,接着离散化,用线段树维护相邻两线段之间y的最大值,将每个点(当前x的坐标,x所在线段的最大值)存入list中,删

中间不必要的点,然后输出所有点(首尾点要自己先行输出),详见代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<list>
using namespace std;
#define L(x) (x<<1)
#define R(x) (x<<1|1)
const int MAXN=400000+100;
int n,cnt;
int G[MAXN],ans_x[MAXN],ans_y[MAXN];
map<int,int>mp;
list<pair<int,int> >lis;
list<pair<int,int> >::iterator it;
struct Line
{
    int y;
    int x1,x2;
}line[MAXN];
struct node
{
    int l,r;
    int Max;
}segtree[MAXN<<2];
void init()
{
    cnt=0;
    memset(G,0,sizeof(G));
    mp.clear();
    lis.clear();
}
void build(int rt,int l,int r)
{
    segtree[rt].l=l; segtree[rt].r=r; segtree[rt].Max=0;
    if(l+1==r)
        return ;
    int mid=(l+r)>>1;
    build(L(rt),l,mid); build(R(rt),mid,r);
}
void push_down(int rt)
{
    if(segtree[rt].l+1 == segtree[rt].r)
        return ;
    segtree[L(rt)].Max=max(segtree[rt].Max,segtree[L(rt)].Max);
    segtree[R(rt)].Max=max(segtree[rt].Max,segtree[R(rt)].Max);
}
void update(int rt,int l,int r,int x)
{
    if(segtree[rt].l==l && segtree[rt].r ==r ){
        segtree[rt].Max=max(segtree[rt].Max,x);
        return ;
    }
    push_down(rt);
    if(l>=segtree[R(rt)].l)
        update(R(rt),l,r,x);
    else if(r<=segtree[L(rt)].r)
        update(L(rt),l,r,x);
    else {
        int mid=(segtree[rt].l+segtree[rt].r)>>1;
        update(L(rt),l,mid,x);
        update(R(rt),mid,r,x);
    }
}
int query(int rt,int p)
{
    if(segtree[rt].l+1 == segtree[rt].r )
        return segtree[rt].Max;
    push_down(rt);
    int mid=(segtree[rt].l+segtree[rt].r)>>1;
    if(p<mid) //不能等于
        return query(L(rt),p);
    else
        return query(R(rt),p);
}
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    //freopen("text.txt","r",stdin);
    while(~scanf("%d",&n)){
        init();
        for(int i=1;i<=n;i++){
            scanf("%d%d%d",&line[i].y,&line[i].x1,&line[i].x2);
            G[++cnt]=line[i].x1; G[++cnt]=line[i].x2;
        }
        sort(G+1,G+cnt+1);
        int m=unique(G+1,G+cnt+1)-(G+1);
        for(int i=1;i<=m;i++) mp[G[i]]=i;
        build(1,1,m);
        for(int i=1;i<=n;i++)
            update(1,mp[line[i].x1],mp[line[i].x2],line[i].y);
        for(int i=1;i<=m;i++){
            int ans=query(1,i);
            lis.push_back(pair<int,int>(G[i],ans));
            if(i+1<=m)
                lis.push_back(pair<int,int>(G[i+1],ans));
        }
        for(it=lis.begin();it!=lis.end();){ // 删除
            int ans1=it->second;
            list<pair<int,int> >::iterator it1=it;
            it1++; if(it1==lis.end()) break;
            list<pair<int,int> >::iterator it2=it1;
            it2++; if(it2==lis.end()) break;
            int ans2=it2->second;
            if(ans1==ans2){
                lis.erase(it1);
                continue;
            }
            it++;
        }
        int top=0;
        ans_x[top]=G[1]; ans_y[top++]=0;
        for(it=lis.begin();it!=lis.end();it++){
            ans_x[top]=it->first; ans_y[top++]=it->second;
            list<pair<int,int> >::iterator it1=it;
            it1++;
            if(it1 == lis.end()) continue;
            if(it1->first == it->first) continue;
        }
        it=lis.end();it--;
        ans_x[top]=it->first ,ans_y[top++]=0;
        printf("%d\n",top);
        for(int i=0;i<top;i++)
            printf("%d %d\n",ans_x[i],ans_y[i]);
    }
    return 0;
}


解法二: multiset维护最大值,用do while 处理所有v里x轴坐标为x的点对,之后只有当高度最大值发生变化时再向vector添加点,详见代码:


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
int n;
multiset<int> s;
vector<pair<int,int> > v,w;
vector<pair<int,int> >:: const_iterator it,it1;
void init()
{
    s.clear();
    v.clear(); w.clear();
}
int main()
{
    freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
    while(~scanf("%d",&n))
    {
        init();
        int y,x1,x2;
        for(int i=0;i<n;i++){
            scanf("%d%d%d",&y,&x1,&x2);
            v.push_back(make_pair(x1,y));
            v.push_back(make_pair(x2,-y));
        }
        sort(v.begin(),v.end());
        int h;
        s.insert(h=0); //放入初始高度
        it=v.begin();
        while(it!=v.end()){
            it1=it;
            do{
                if(it1->second > 0)
                    s.insert(it1->second);
                else if(it1->second<0) //second<0,则将对应高度在multiset中删除
                    s.erase(s.find(-it1->second));
                it1++;

            }while(it1!=v.end() && it1->first == it->first); //处理完所有first=it->first的v里的点对
            if(*s.rbegin()!=h){ //最高高度发生变化,向w里添加点对
                w.push_back(make_pair(it->first,h));
                w.push_back(make_pair(it->first,h=*s.rbegin()));
            }
            it=it1;
        }
        printf("%d\n",w.size());
        for (int i = 0; i < (int)w.size(); ++i)
            printf("%d %d\n", w[i].first, w[i].second);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值