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

Code

#include <iostream>
using namespace std;
typedef long long ll;

ll cp(ll x1, ll y1, ll x2, ll y2) {
    return x1 * y2 - x2 * y1;
}

int sign(ll val) {
    return (0 < val) - (val < 0);
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);

    int t;
    cin >> t;

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

        // sort
        if (x1 > x2 || (x1 == x2 && y1 > y2)) {
            swap(x1, x2);
            swap(y1, y2);
        }

        if (x3 > x4 || (x3 == x4 && y3 > y4)) {
            swap(x3, x4);
            swap(y3, y4);
        }

        // (p2-p1)x(p3-p1)
        cp1 = cp(x2-x1, y2-y1, x3-x1, y3-y1);
        // (p2-p1)x(p4-p1)
        cp2 = cp(x2-x1, y2-y1, x4-x1, y4-y1);
        
        // (p4-p3)x(p1-p3)
        cp3 = cp(x4-x3, y4-y3, x1-x3, y1-y3);
        // (p4-p3)x(p2-p3)
        cp4 = cp(x4-x3, y4-y3, x2-x3, y2-y3);

        if (cp1 == 0 && cp2 == 0) {
            // vertical
            if (x1 == x2) {
                if (y3 > y2 || y4 < y1) {
                    cout << "NO\n";
                } else {
                    cout << "YES\n";
                }
            // non-vertical
            } else if (x3 > x2 || x4 < x1) {
                cout << "NO\n";
            } else {
                cout << "YES\n";
            }
        } else if (sign(cp1) * sign(cp2) > 0 || sign(cp3) * sign(cp4) > 0) {
            cout << "NO\n";
        } else {
            cout << "YES\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