Parade - CodeForces #35 (Div. 2) E 线段树

本文介绍了一个关于寻找城市天际线轮廓多边形的问题解决方案,该问题源自一场虚拟战争游行背景下的算法挑战。主要内容涉及如何通过离散化、线段树等数据结构处理高楼坐标,以确定一个包含所有建筑物且面积最小的多边形。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

E. Parade
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

题意:给你所有楼的高度和范围,求合并后的多边形的拐点。

思路:先将所有的点赋值成最大的值,然后通过这组数据可以轻易找到结果。注意离散化的时候要留出空隙,否则过不了第二组样例,还有输入输出的格式。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
map<ll,int> match;
int n,m;
ll f[400001],p[400010],ans[400010];
struct node2
{
    int val;
    ll l,r;
}op[100010];
bool cmp(node2 a,node2 b)
{
    return a.val<b.val;
}
struct node
{
    int num,l,r;
    bool flag;
}tree[1600100];
void build(int o,int l,int r)
{
    tree[o].l=l;
    tree[o].r=r;
    tree[o].num=0;
    tree[o].flag=0;
    if(l==r)
      return;
    ll mi=(l+r)/2;
    build(o<<1,l,mi);
    build(o<<1|1,mi+1,r);
}
void update(int o,int l,int r,int val)
{
    if(tree[o].l==l && tree[o].r==r)
    {
        tree[o].num=val;
        tree[o].flag=1;
        return;
    }
    if(tree[o].flag)
    {
        tree[o<<1].flag=tree[o<<1|1].flag=1;
        tree[o<<1].num=tree[o<<1|1].num=tree[o].num;
        tree[o].flag=0;
    }
    ll mi=(tree[o].l+tree[o].r)/2;
    if(r<=mi)
      update(o<<1,l,r,val);
    else if(l>mi)
      update(o<<1|1,l,r,val);
    else
    {
        update(o<<1,l,mi,val);
        update(o<<1|1,mi+1,r,val);
    }
}
void dfs(int o)
{
    if(tree[o].l==tree[o].r)
    {
        ans[tree[o].l]=tree[o].num;
        return;
    }
    if(tree[o].flag)
    {
        tree[o<<1].flag=tree[o<<1|1].flag=1;
        tree[o<<1].num=tree[o<<1|1].num=tree[o].num;
    }
    dfs(o<<1);
    dfs(o<<1|1);
}
int main()
{
    int i,j,k;
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d%I64d%I64d",&op[i].val,&op[i].l,&op[i].r);
        f[i*4-3]=op[i].l*2;
        f[i*4-2]=op[i].l*2-1;
        f[i*4-1]=op[i].r*2;
        f[i*4]=op[i].r*2-1;
    }
    sort(f+1,f+1+n*4);
    m=0;
    f[0]=-100000000000LL;
    for(i=1;i<=n*4;i++)
       if(f[i]!=f[i-1])
       {
           m++;
           match[f[i]]=m;
           p[m]=f[i]/2;
       }
    build(1,1,m);
    sort(op+1,op+1+n,cmp);
    for(i=1;i<=n;i++)
       update(1,match[op[i].l*2],match[op[i].r*2],op[i].val);
    dfs(1);
    k=0;
    for(i=1;i<=m+1;i++)
       if(ans[i]!=ans[i-1])
         k+=2;
    printf("%d\n",k);
    for(i=1;i<=m+1;i++)
    {
        if(ans[i]>ans[i-1])
          printf("%I64d %I64d\n%I64d %I64d\n",p[i],ans[i-1],p[i],ans[i]);
        else if(ans[i]<ans[i-1])
          printf("%I64d %I64d\n%I64d %I64d\n",p[i-1],ans[i-1],p[i-1],ans[i]);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值