CSES - Aalto Competitive Programming 2024 - wk10 - Homework - Results
Submission details
Task:Line Segment Intersection
Sender:ZDHKLV
Submission time:2024-11-08 13:09:21 +0200
Language:C++ (C++11)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.18 sdetails
#2ACCEPTED0.23 sdetails
#3ACCEPTED0.33 sdetails
#4ACCEPTED0.40 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails

Code

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define endl '\n'
#define Point complex<int>
#define X real()
#define Y imag()

int cross_product(Point A, Point B, Point C) {
    return (conj(B - A) * (C - A)).Y;
}

bool compare_points(const Point &a, const Point &b) {
    return (a.X == b.X) ? (a.Y < b.Y) : (a.X < b.X);
}

bool is_mid_point(Point A, Point B, Point C) {
    vector<Point> v = {A, B, C};
    sort(v.begin(), v.end(), compare_points);
    return (v[1] == C);
}

int sign(int x) {
    return (x > 0) - (x < 0);
}

bool do_intersect(Point A, Point B, Point C, Point D) {
    int crossProduct1 = cross_product(A, B, C);
    int crossProduct2 = cross_product(A, B, D);
    int crossProduct3 = cross_product(C, D, A);
    int crossProduct4 = cross_product(C, D, B);
    if (crossProduct1 == 0 && is_mid_point(A, B, C)) return true;
    if (crossProduct2 == 0 && is_mid_point(A, B, D)) return true;
    if (crossProduct3 == 0 && is_mid_point(C, D, A)) return true;
    if (crossProduct4 == 0 && is_mid_point(C, D, B)) return true;
    if (sign(crossProduct1) != sign(crossProduct2) && sign(crossProduct3) != sign(crossProduct4))
        return true;
    return false;
}

signed main() {

    int t;
    cin >> t;

    vector<vector<Point>> points(t, vector<Point>(4));

    for (int i = 0; i < t; i++) {
        int x1, y1, x2, y2, x3, y3, x4, y4;
        cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;

        points[i][0] = {x1, y1};
        points[i][1] = {x2, y2};
        points[i][2] = {x3, y3};
        points[i][3] = {x4, y4};
    }

    for (auto& point : points) {
        Point A = point[0];
        Point B = point[1];
        Point C = point[2];
        Point D = point[3];
        cout << (do_intersect(A, B, C, D) ? "YES" : "NO") << endl;
    }

    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
100000
9 7 1 8 8 -5 0 2
10 1 -1 2 -4 1 -7 3
10 2 -8 6 1 2 2 -1
-10 1 9 -7 4 -3 -5 0
...

correct output
NO
NO
NO
NO
NO
...

user output
NO
NO
NO
NO
NO
...

Test 2

Verdict: ACCEPTED

input
100000
81 745 -967 768 -451 -490 -454...

correct output
NO
NO
YES
NO
YES
...

user output
NO
NO
YES
NO
YES
...

Test 3

Verdict: ACCEPTED

input
100000
492853 -452834 -657156 -282543...

correct output
YES
YES
NO
YES
YES
...

user output
YES
YES
NO
YES
YES
...

Test 4

Verdict: ACCEPTED

input
100000
788522666 939776556 -831492125...

correct output
NO
NO
NO
NO
NO
...

user output
NO
NO
NO
NO
NO
...

Test 5

Verdict: ACCEPTED

input
1
1 6 6 6 4 4 1000000000 1000000...

correct output
YES

user output
YES

Test 6

Verdict: ACCEPTED

input
1
-1000000000 1000000000 9999999...

correct output
NO

user output
NO