Submission details
Task:Line Segment Intersection
Sender:aalto26jh_034
Submission time:2026-11-10 16:56:27 +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 <iostream>
#include <complex>
#include <algorithm>

using namespace std;

typedef long long C;
typedef complex<C> P;
#define X real()
#define Y imag()

C cross_prod(P a, P b, P c) {
    return (conj(b - a) * (c - a)).Y;
}

bool on_segment(P a, P b, P p) {
    return p.X >= min(a.X, b.X) && p.X <= max(a.X, b.X) &&
           p.Y >= min(a.Y, b.Y) && p.Y <= max(a.Y, b.Y) &&
           cross_prod(a, b, p) == 0;
}

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

void solve() {
    C x1, y1, x2, y2, x3, y3, x4, y4;
    cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;
    P a = {x1, y1}, b = {x2, y2}, c = {x3, y3}, d = {x4, y4};

    C cp1 = cross_prod(a, b, c);
    C cp2 = cross_prod(a, b, d);
    C cp3 = cross_prod(c, d, a);
    C cp4 = cross_prod(c, d, b);

    if (sign(cp1) * sign(cp2) < 0 && sign(cp3) * sign(cp4) < 0) {
        cout << "YES\n";
        return;
    }

    if (on_segment(a, b, c) || on_segment(a, b, d) ||
        on_segment(c, d, a) || on_segment(c, d, b)) {
        cout << "YES\n";
        return;
    }

    cout << "NO\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    if (cin >> t) {
        while (t--) solve();
    }
    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