Submission details
Task:Line Segment Intersection
Sender:aalto26jh_026
Submission time:2026-11-09 10:43:44 +0200
Language:C++ (C++23)
Status:READY
Result:
Test results
testverdicttime
#10.32 sdetails
#20.37 sdetails
#30.47 sdetails
#40.56 sdetails
#50.00 sdetails
#60.00 sdetails

Code

#include <iostream>


int main(int argc, char **argv) {
    int i;
    std::cin >> i;

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

        long D = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
        if (D == 0) {
            long long cross = (x2 - x1)*(y3 - y1) - (y2 - y1)*(x3 - x1);
            bool overlap = std::max(std::min(x1,x2), std::min(x3,x4)) <= std::min(std::max(x1,x2), std::max(x3,x4)) && std::max(std::min(y1,y2), std::min(y3,y4)) <= std::min(std::max(y1,y2), std::max(y3,y4));
            if (cross == 0 && overlap) {
                std::cout << "YES" << std::endl;
            } else {
                std::cout << "NO" << std::endl;
            }
            continue;
        }

        long num_t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
        long num_u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);

        if (D < 0) {
            D = -D;
            num_t = -num_t;
            num_u = -num_u;
        }

        if (0 <= num_t && num_t <= 1 && 0 <= num_u && num_u <= 1) {
            std::cout << "YES" << std::endl;
            continue;
        }

        std::cout << "NO" << std::endl;
    }

    return 0;
}

Test details

Test 1

Verdict:

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
...

Feedback: Incorrect character on line 6 col 1: expected "YES", got "NO"

Test 2

Verdict:

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

correct output
NO
NO
YES
NO
YES
...

user output
NO
NO
NO
NO
NO
...

Feedback: Incorrect character on line 3 col 1: expected "YES", got "NO"

Test 3

Verdict:

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

correct output
YES
YES
NO
YES
YES
...

user output
NO
NO
NO
NO
NO
...

Feedback: Incorrect character on line 1 col 1: expected "YES", got "NO"

Test 4

Verdict:

input
100000
788522666 939776556 -831492125...

correct output
NO
NO
NO
NO
NO
...

user output
NO
NO
NO
NO
NO
...

Feedback: Incorrect character on line 11 col 1: expected "YES", got "NO"

Test 5

Verdict:

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

correct output
YES

user output
NO

Feedback: Incorrect character on line 1 col 1: expected "YES", got "NO"

Test 6

Verdict:

input
1
-1000000000 1000000000 9999999...

correct output
NO

user output
YES

Feedback: Incorrect character on line 1 col 1: expected "NO", got "YES"