제출 #1338277

#제출 시각아이디문제언어결과실행 시간메모리
1338277tung04885Roads (CEOI20_roads)C++20
100 / 100
108 ms14088 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 100100
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
#define __buitin_popcount __builtin_popcountll
#define BIT(x,i) (((x)>>(i))&1ll)
#define MASK(i) (1ll<<(i))

template<class X,class Y> bool maximize(X &x,Y y)
{
    if(x<y)
    {
        x=y;
        return 1;
    }
    return 0;
}

template<class X,class Y> bool minimize(X &x,Y y)
{
    if(y<x)
    {
        x=y;
        return 1;
    }
    return 0;
}

const double EPS=1e-9;
const int inf=1e9+412009;
const ll INF=2e18+412009;

int sweep_x;

struct POINT
{
    int x=-inf,y=-inf;
    int id=-1;

    POINT(int a=0,int b=0,int c=0)
    {
        x=a,y=b,id=c;
    }

    void input()
    {
        cin>>x>>y;
    }

    void output()
    {
        cout<<x<<' '<<y<<' '<<id<<'\n';
    }

    bool operator == (POINT other) const
    {
        return x==other.x&&y==other.y;
    }

    bool operator < (POINT other) const
    {
        if(POINT(x,y)==other) return id<other.id;
        return x==other.x ? y<other.y : x<other.x;
    }
};

const POINT unit={-inf,-inf,-1};

struct SEGMENT
{
    POINT st,en;
    int id=0;

    SEGMENT(POINT a=unit,POINT b=unit,int _id=0)
    {
        st=min(a,b);st.id=-1;
        en=max(a,b);en.id=1;
        id=_id;
    }

    void input(int _i)
    {
        id=_i;

        POINT x,y;
        x.input();
        y.input();

        st=min(x,y);st.id=-_i;
        en=max(x,y);en.id=_i;
    }

    void output()
    {
        cout<<st.x<<' '<<st.y<<' '<<en.x<<' '<<en.y<<'\n';
    }
};

double coordinate(SEGMENT a)
{
    if(a.st.x==a.en.x) return a.st.y;
    return 1.0*a.st.y+1.0*(a.en.y-a.st.y)*(sweep_x-a.st.x)/(a.en.x-a.st.x);
}

bool operator < (SEGMENT a,SEGMENT b)
{
    if(abs(coordinate(a)-coordinate(b))>EPS) return coordinate(a)<coordinate(b);
    return a.id<b.id;
}

int n;
SEGMENT a[MAX];

void nhap()
{
    cin>>n;
    for(int i=1;i<=n;i++) a[i].input(i);
}

struct CMP
{
    bool operator()(int i,int j) const
    {
        return a[i]<a[j];
    }
};

set<int,CMP> s;
vector<POINT> events;
POINT down[MAX]={};
vector<SEGMENT> res;

void solve()
{
    for(int i=1;i<=n;i++)
    {
        events.push_back(a[i].st);
        events.push_back(a[i].en);

        down[i]=unit;
    }

    sort(all(events));

    a[n+1]=SEGMENT(unit,unit,-inf);

    down[n+1]=unit;

    s.insert(n+1);

    for(int i=0;i<(int)events.size();i++)
    {
        POINT p=events[i];

        int id=abs(p.id);

        sweep_x=p.x;

        if(p.id<0)
        {
            auto it=--s.lower_bound(id);

            if(!(down[*it]==POINT(-inf,-inf))) res.push_back(SEGMENT(down[*it],p));

            down[*it]=p;

            it=s.insert(id).fi;

            down[*it]=p;
        }
        else
        {
            auto it=s.find(id);

            it=--s.erase(it);

            down[*it]=p;
        }
    }

    for(SEGMENT it:res) it.output();
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);

    nhap();
    solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...