Submission #328718

# Submission time Handle Problem Language Result Execution time Memory
328718 2020-11-17T18:26:40 Z dolphingarlic Roads (CEOI20_roads) C++14
100 / 100
390 ms 20048 KB
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
 
struct Point {
    ll x, y;
    int idx;
};

bool operator<(Point A, Point B) {
    if (A.x == B.x) return A.y < B.y;
    return A.x < B.x;
}
 
ostream& operator<<(ostream& out, Point P) {
    out << P.x << ' ' << P.y;
    return out;
}

struct Seg {
    Point p1, p2;
} s[100001];

ll curr_x;

bool operator<(Seg A, Seg B) {
    double yA, yB;
    if (A.p1.x == A.p2.x) yA = A.p1.y;
    else {
        double m = double(A.p2.y - A.p1.y) / (A.p2.x - A.p1.x);
        double c = A.p1.y - m * A.p1.x;
        yA = m * curr_x + c;
    }
    if (B.p1.x == B.p2.x) yB = B.p1.y;
    else {
        double m = double(B.p2.y - B.p1.y) / (B.p2.x - B.p1.x);
        double c = B.p1.y - m * B.p1.x;
        yB = m * curr_x + c;
    }
    return yA < yB;
}

bool operator==(Seg A, Seg B) {
    return A.p1.idx == B.p1.idx;
}

Point rightmost[100001];
vector<Point> events;
set<Seg> active;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> s[i].p1.x >> s[i].p1.y >> s[i].p2.x >> s[i].p2.y;
        if (s[i].p2 < s[i].p1) swap(s[i].p1, s[i].p2);
        s[i].p1.idx = s[i].p2.idx = i;
        events.push_back(s[i].p1);
        events.push_back(s[i].p2);
    }
    sort(events.begin(), events.end());
    
    Point ninf = {INT_MIN, INT_MAX, n}, inf = {INT_MAX, INT_MAX, n};
    active.insert({ninf, inf}); // The "sentinel"
    rightmost[n] = {0, 0, -1};

    for (Point i : events) {
        curr_x = i.x;
        if (active.find(s[i.idx]) != active.end()) {
            // Right endpoint of segment
            active.erase(s[i.idx]);
            auto nxt = active.lower_bound(s[i.idx]);
            rightmost[nxt->p1.idx] = i;
        } else {
            // Left endpoint of segment
            auto nxt = active.lower_bound(s[i.idx]);
            if (rightmost[nxt->p1.idx].idx != -1) {
                cout << rightmost[nxt->p1.idx] << ' ' << i << '\n';
            }
            rightmost[i.idx] = rightmost[nxt->p1.idx] = i;
            active.insert(s[i.idx]);
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 99 ms 6620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 396 KB Output is correct
3 Correct 2 ms 620 KB Output is correct
4 Correct 15 ms 2276 KB Output is correct
5 Correct 29 ms 4064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 3 ms 620 KB Output is correct
4 Correct 14 ms 2276 KB Output is correct
5 Correct 31 ms 4064 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 4 ms 620 KB Output is correct
9 Correct 21 ms 2404 KB Output is correct
10 Correct 275 ms 18208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 2 ms 620 KB Output is correct
4 Correct 14 ms 2276 KB Output is correct
5 Correct 28 ms 4064 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 2 ms 620 KB Output is correct
9 Correct 15 ms 2276 KB Output is correct
10 Correct 77 ms 9308 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 2 ms 620 KB Output is correct
5 Correct 15 ms 2404 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 2 ms 620 KB Output is correct
9 Correct 17 ms 2276 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 2 ms 620 KB Output is correct
13 Correct 15 ms 2276 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 2 ms 492 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 9 ms 1068 KB Output is correct
23 Correct 10 ms 1196 KB Output is correct
24 Correct 17 ms 1640 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 102 ms 6792 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 2 ms 620 KB Output is correct
6 Correct 15 ms 2276 KB Output is correct
7 Correct 29 ms 4064 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 2 ms 620 KB Output is correct
11 Correct 17 ms 2276 KB Output is correct
12 Correct 275 ms 18156 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 2 ms 620 KB Output is correct
16 Correct 15 ms 2276 KB Output is correct
17 Correct 77 ms 9308 KB Output is correct
18 Correct 1 ms 552 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 2 ms 492 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 9 ms 1068 KB Output is correct
27 Correct 9 ms 1068 KB Output is correct
28 Correct 17 ms 1640 KB Output is correct
29 Correct 223 ms 12252 KB Output is correct
30 Correct 265 ms 17360 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 182 ms 12124 KB Output is correct
33 Correct 184 ms 12252 KB Output is correct
34 Correct 242 ms 15952 KB Output is correct
35 Correct 247 ms 16816 KB Output is correct
36 Correct 390 ms 20048 KB Output is correct