Submission details
Task:Line Segment Intersection
Sender:aalto26jh_023
Submission time:2026-11-09 04:03:59 +0200
Language:Python3 (PyPy3)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.43 sdetails
#2ACCEPTED0.38 sdetails
#3ACCEPTED0.39 sdetails
#4ACCEPTED0.40 sdetails
#5ACCEPTED0.04 sdetails
#6ACCEPTED0.04 sdetails

Code

def cross(ax, ay, bx, by, cx, cy):
    return (bx - ax) * (cy - ay) - (by - ay) * (cx - ax)

def on_segment(ax, ay, bx, by, px, py):
    return min(ax, bx) <= px <= max(ax, bx) and min(ay, by) <= py <= max(ay, by)

t = int(input())
for _ in range(t):
    x1, y1, x2, y2, x3, y3, x4, y4 = map(int, input().split())

    d1 = cross(x1, y1, x2, y2, x3, y3)
    d2 = cross(x1, y1, x2, y2, x4, y4)
    d3 = cross(x3, y3, x4, y4, x1, y1)
    d4 = cross(x3, y3, x4, y4, x2, y2)

    if ((d1 > 0 and d2 < 0) or (d1 < 0 and d2 > 0)) and \
       ((d3 > 0 and d4 < 0) or (d3 < 0 and d4 > 0)):
        print("YES")

    elif d1 == 0 and on_segment(x1, y1, x2, y2, x3, y3):
        print("YES")
    elif d2 == 0 and on_segment(x1, y1, x2, y2, x4, y4):
        print("YES")
    elif d3 == 0 and on_segment(x3, y3, x4, y4, x1, y1):
        print("YES")
    elif d4 == 0 and on_segment(x3, y3, x4, y4, x2, y2):
        print("YES")

    else:
        print("NO")

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