Submission #1326009

#TimeUsernameProblemLanguageResultExecution timeMemory
1326009_callmelucianRoads (CEOI20_roads)C++17
0 / 100
26 ms13352 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using pl = pair<ll,ll>;
using pii = pair<int,int>;
using tpl = tuple<int,int,int>;

#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())

struct Point {
    ll x, y;

    Point() : x(0), y(0) {}
    Point (ll x, ll y) : x(x), y(y) {}

    bool operator< (const Point &o) const { return (x == o.x ? y < o.y : x < o.x); }
    bool operator> (const Point &o) const { return (x == o.x ? y > o.y : x > o.x); }
    Point operator+ (const Point &o) const { return Point(x + o.x, y + o.y); }
    Point operator- (const Point &o) const { return Point(x - o.x, y - o.y); }

    friend ll dot (const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; }
    friend ll cross (const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; }
    
    friend bool turnLeft (const Point &a, const Point &b, const Point &c) { return cross(b - a, c - b) > 0; }
    friend bool turnRight (const Point &a, const Point &b, const Point &c) { return cross(b - a, c - b) < 0; }

    friend istream& operator>> (istream &inp, Point &p) { return inp >> p.x >> p.y, inp; }
    friend ostream& operator<< (ostream &oup, const Point &p) { return oup << "(" << p.x << ", " << p.y << ")", oup; }
};

struct Segment {
    Point a, b;
    mutable Point rm;

    Segment() {}
    Segment (Point _a, Point _b) : a(_a), b(_b), rm(_a) {
        if (a.x > b.x) swap(a, b), rm = a;
    }

    bool operator< (const Segment &o) const {
        if (a.x <= o.a.x and o.a.x <= b.x) return turnLeft(a, b, o.a);
        if (a.x <= o.b.x and o.b.x <= b.x) return turnLeft(a, b, o.b);
        return turnRight(o.a, o.b, a);
    }

    bool operator< (const Point &o) const { return turnLeft(a, b, o); }
};

const int mn = 2e5 + 10;
const int oo = 1e9;

multiset<Segment, less<void>> lineSet;
vector<int> cmpSet, openEvent[mn], closeEvent[mn];

int getX (int targ) { return lower_bound(all(cmpSet), targ) - cmpSet.begin(); }

auto getToLeft (const Point &p) {
    auto itr = lineSet.lower_bound(p);
    if (itr == lineSet.begin()) {
        cout << "You may quit CP 1\n";
        exit(0);
    }
    return prev(itr);
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    // read points coordinate and prepare compressed coordinate set
    int n; cin >> n;
    vector<pair<Point, Point>> vec(n), ans;
    for (int i = 0; i < n; i++) {
        cin >> vec[i].first >> vec[i].second;
        cmpSet.push_back(vec[i].first.x);
        cmpSet.push_back(vec[i].second.x);
        if (vec[i].first > vec[i].second)
            swap(vec[i].first, vec[i].second);
    }
    cmpSet.push_back(INT_MIN);
    sort(all(cmpSet)), filter(cmpSet);

    // prepare sweep line events
    for (int i = 0; i < n; i++) {
        Point openPoint, endPoint;
        tie(openPoint, endPoint) = vec[i];
        openEvent[getX(openPoint.x)].push_back(i);
        closeEvent[getX(endPoint.x)].push_back(i);
    }
    for (int x = 0; x < cmpSet.size(); x++) {
        sort(all(openEvent[x]), [&] (int i, int j) { return vec[i].first.y < vec[j].first.y; });
        sort(all(closeEvent[x]), [&] (int i, int j) { return vec[i].second.y < vec[j].second.y; });
    }

    // perform sweep line
    lineSet.emplace(Point(-oo, -oo), Point(oo, -oo)); // an infintely low horizontal line
    for (int i = 0; i < cmpSet.size(); i++) {
        // process open events
        for (int sID : openEvent[i]) {
            auto itr = getToLeft(vec[sID].first);
            ans.emplace_back(itr->rm, vec[sID].first);
            itr->rm = vec[sID].first;
            lineSet.emplace(vec[sID].first, vec[sID].second);
        }

        // process close events
        for (int sID : closeEvent[i]) {
            auto itr = lineSet.lower_bound(vec[sID].first);
            if (itr == lineSet.end()) {
                cout << "You may quit CP 2\n";
                exit(0);
            }
            itr = lineSet.erase(itr);
            if (itr == lineSet.begin()) {
                cout << "You may quit CP 3\n";
                exit(0);
            }
            itr = prev(itr), itr->rm = vec[sID].second;
        }

        // cout << "=== UPDATE SET ===\n";
        // for (auto [a, b, c] : lineSet) cout << "[" << a << ", " << b << ", " << c << "] ";
        // cout << "\n\n";
    }

    // print result
    for (int i = 1; i < ans.size(); i++) {
        cout << ans[i].first.x << " " << ans[i].first.y << " ";
        cout << ans[i].second.x << " " << ans[i].second.y << "\n";
    }

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