Submission details
Task:Line Segment Intersection
Sender:aalto26jh_024
Submission time:2026-11-09 18:29:13 +0200
Language:Python3 (PyPy3)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.41 sdetails
#2ACCEPTED0.32 sdetails
#3ACCEPTED0.34 sdetails
#4ACCEPTED0.35 sdetails
#5ACCEPTED0.04 sdetails
#6ACCEPTED0.04 sdetails

Code

import sys

def solve():
    input_data = sys.stdin.read().split()
    if not input_data:
        return
    
    t = int(input_data[0])
    idx = 1
    
    out = []
    for _ in range(t):
        x1, y1, x2, y2, x3, y3, x4, y4 = map(int, input_data[idx:idx+8])
        idx += 8
        
        def sign(val):
            if val > 0: return 1
            if val < 0: return -1
            return 0
        
        def cp(px, py, qx, qy, rx, ry):
            return sign((qx - px) * (ry - py) - (qy - py) * (rx - px))
        
        def overlap(a, b, c, d):
            return max(min(a, b), min(c, d)) <= min(max(a, b), max(c, d))
        
        cp1 = cp(x1, y1, x2, y2, x3, y3)
        cp2 = cp(x1, y1, x2, y2, x4, y4)
        cp3 = cp(x3, y3, x4, y4, x1, y1)
        cp4 = cp(x3, y3, x4, y4, x2, y2)
        
        if cp1 * cp2 < 0 and cp3 * cp4 < 0:
            out.append("YES")
            continue
            
        if (cp1 == 0 and overlap(x1, x2, x3, x3) and overlap(y1, y2, y3, y3)) or \
           (cp2 == 0 and overlap(x1, x2, x4, x4) and overlap(y1, y2, y4, y4)) or \
           (cp3 == 0 and overlap(x3, x4, x1, x1) and overlap(y3, y4, y1, y1)) or \
           (cp4 == 0 and overlap(x3, x4, x2, x2) and overlap(y3, y4, y2, y2)):
            out.append("YES")
        else:
            out.append("NO")
            
    print("\n".join(out))

solve()

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