Submission details
Task:Line Segment Intersection
Sender:aalto26jh_021
Submission time:2026-11-06 00:46:31 +0200
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.08 sdetails
#2ACCEPTED0.09 sdetails
#3ACCEPTED0.10 sdetails
#4ACCEPTED0.12 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails

Code

#include <bits/stdc++.h>

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
typedef long long C;
typedef complex<C> P;
#define X real()
#define Y imag()
#define FAST                                                                   \
    ios::sync_with_stdio(0);                                                   \
    cin.tie(0);

C cross(P a, P b) { return (conj(a) * b).Y; }

C pointLocToLine(P point, P line1, P line2) {
    return cross(point - line1, point - line2);
}

bool isPointOnLine(P point, P line1, P line2) {
    return pointLocToLine(point, line1, line2) == 0;
}

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

bool noOverlap(P a, P b, P c, P d) {
    auto lessP = [](const P &p, const P &q) {
        return make_pair(p.X, p.Y) < make_pair(q.X, q.Y);
    };
    if (lessP(b, a))
        swap(a, b); // a <= b
    if (lessP(d, c))
        swap(c, d); // c <= d
    return lessP(b, c) || lessP(d, a);
}

int main() {
    FAST;

    int n;
    cin >> n;

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

        P a = {x1, y1};
        P b = {x2, y2};
        P c = {x3, y3};
        P d = {x4, y4};

        bool collinear = isPointOnLine(a, c, d) && isPointOnLine(b, c, d);
        // Case 1 - All on same line
        bool case1 = collinear && !noOverlap(a, b, c, d);
        // Case 2 - Common Verex
        bool case2 = a == c || a == d || b == c || b == d;
        // Case 3 - Single point intersection
        bool case3 =
            !collinear &&
            sgn(pointLocToLine(a, c, d)) * sgn(pointLocToLine(b, c, d)) <= 0 &&
            sgn(pointLocToLine(c, a, b)) * sgn(pointLocToLine(d, a, b)) <= 0;

        cout << (case1 || case2 || case3 ? "YES" : "NO") << "\n";
    }

    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