Submission #544122

#TimeUsernameProblemLanguageResultExecution timeMemory
544122tengiz05Roads (CEOI20_roads)C++17
100 / 100
150 ms10616 KiB
#include <bits/stdc++.h>

using i64 = long long;
using ld = long double;

struct Point {
    int x, y;
    Point(int x = 0, int y = 0) : x(x), y(y) {}
};
bool operator<(const Point &a, const Point &b) {
    return std::pair(a.x, a.y) < std::pair(b.x, b.y);
}

constexpr int N = 1E5 + 5, inf = 1E8;

Point left[N], right[N];
int Time;

struct Segment {
    int i;
    mutable Point f;
    Segment(int i) : i(i) {}
};
ld pos(const Segment &a) {
    int i = a.i;
    if (Time == left[i].x)
        return left[i].y;
    ld ratio = ld(right[i].y - left[i].y) / (right[i].x - left[i].x);
    return left[i].y + ratio * (Time - left[i].x);
}
bool operator<(const Segment &a, const Segment &b) {
    return pos(a) < pos(b);
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n;
    std::cin >> n;
    
    std::vector<std::pair<Point, int>> events;
    
    for (int i = 0; i < n; i++) {
        std::cin >> left[i].x >> left[i].y >> right[i].x >> right[i].y;
        if (right[i] < left[i]) {
            std::swap(left[i], right[i]);
        }
        events.emplace_back(left[i], i + 1);
        events.emplace_back(right[i], -(i + 1));
    }
    
    left[n] = {-inf, -inf};
    right[n] = {inf, -inf};
    left[n + 1] = {-inf, inf};
    right[n + 1] = {inf, inf};
    Time = -inf;
    
    std::set<Segment> s;
    s.insert(Segment(n));
    s.insert(Segment(n + 1));
    
    s.begin()->f = {-inf, -inf};
    
    std::sort(events.begin(), events.end());
    
    for (auto [p, v] : events) {
        int i = std::abs(v) - 1;
        Time = p.x;
        if (v > 0) {
            s.insert(Segment(i));
            auto it = s.find(Segment(i));
            it->f = p;
            auto a = std::prev(it)->f;
            std::prev(it)->f = p;
            
            if (a.x != -inf) {
                std::cout << a.x << " " << a.y << " " << p.x << " " << p.y << "\n";
            }
        } else {
            auto it = s.find(Segment(i));
            std::prev(it)->f = p;
            s.erase(it);
        }
    }
    
    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...