跳到正文

题解

P6802 [CEOI2020]道路

Thunder_S约 5 分钟
原文
文章信息与版本

文章信息

发布
2022年7月5日
收录
2026年10月18日
更新
2026年11月8日
篇幅
约 5 分钟
获赞
5
题目
P6802

版本

  1. v1当前

Solution

首先为了避免斜率正无穷这种极端情况的出现,可以将点进行一个旋转。(x,y)(x,y) 逆时针转 α\alpha 角后的坐标是 (xcos⁡α−ysin⁡α,xsin⁡α+ycos⁡α)(x\cos\alpha-y\sin\alpha,x\sin\alpha+y\cos\alpha)。

计算几何题中出现线段需要统计一些结果的,一般使用扫描线来完成。

假设扫描线从左往右扫。扫描过程中,扫描线会不断被给出的线段分割成若干个区域。当扫描线碰到某条线段的左端点时,说明此时又新增了一个区域。那么我们找出此前该区域内 xx 坐标最大的点,与当前点连边即可。注意更新各区域的 xx 坐标最大的点。

当扫到线段的右端点时,就将两个区域合并,同时更新一下最大的 xx 坐标。

具体过程可以用 set\text{set} 来实现。

Code

C++
#include<set>
#include<cmath>
#include<cstdio>
#include<algorithm>
#define N 100050
#define inf 10000000000
#define ldb long double
using namespace std;
int n;
ldb nowx;
ldb Pi=acos(-1.0),alpha=Pi*(1.0*139/180),cosal=cos(alpha),sinal=sin(alpha);
struct po
{
    int rlx,rly;
    ldb x,y;
    void calc()
    {
        x=cosal*rlx-sinal*rly;
        y=cosal*rly+sinal*rlx;
    }
}a[N<<1];
struct px
{
    int id,tp;
    ldb x;
}c[N<<1];
struct line
{
    int st,ed;mutable int pre;
    ldb k,b;
    void calc()
    {
        k=(a[st].y-a[ed].y)/(a[st].x-a[ed].x);
        b=a[st].y-k*a[st].x;
    }
    bool operator <(const line x) const {return k*nowx+b<x.k*nowx+x.b;};
}now;
set<line> S;
set<line>::iterator it;
bool cmp(px x,px y) {return x.x<y.x;}
int main()
{
    freopen("network.in","r",stdin);
    freopen("network.out","w",stdout);
    scanf("%d",&n);
    for (int i=1;i<=2*n;i+=2)
    {
        scanf("%d%d",&a[i].rlx,&a[i].rly);a[i].calc();
        scanf("%d%d",&a[i+1].rlx,&a[i+1].rly);a[i+1].calc();
        if (a[i].x>a[i+1].x) swap(a[i],a[i+1]);
        c[i].x=a[i].x;c[i].id=i;c[i].tp=1;
        c[i+1].x=a[i+1].x;c[i+1].id=i+1;c[i+1].tp=0;        
    }
    sort(c+1,c+2*n+1,cmp);
    now.st=-2;now.ed=0;now.pre=0;now.k=0;now.b=-inf;S.insert(now);
    now.st=-1;now.ed=0;now.pre=0;now.k=0;now.b=inf;S.insert(now);
    for (int i=1;i<=2*n;++i)
    {
        nowx=c[i].x;
        if (c[i].tp)
        {
            now.pre=now.st=c[i].id;now.ed=c[i].id+1;
            now.calc();
            it=S.insert(now).first;--it;
            if (it->pre) printf("%d %d %d %d\n",a[it->pre].rlx,a[it->pre].rly,a[now.st].rlx,a[now.st].rly);
            it->pre=now.st;
        }
        else
        {
            now.st=c[i].id-1;now.ed=c[i].id;
            now.calc();
            it=S.find(now);--it;
            it->pre=now.ed;
            S.erase(now);
        }
    }
    return 0;
}

代码可能有亿点相似,请见谅。

感谢 s_r_f 大佬的题解->传送门

原文