Submission details
Task:Line Segment Intersection
Sender:aalto26jh_011
Submission time:2026-11-09 14:10:28 +0200
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#10.40 sdetails
#2ACCEPTED0.40 sdetails
#3ACCEPTED0.51 sdetails
#4ACCEPTED0.53 sdetails
#5ACCEPTED0.04 sdetails
#6ACCEPTED0.04 sdetails

Code



import sys

def main():
    n = int(input())

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

        a1, b1 = y2 - y1, x1 - x2
        a2, b2 = y4 - y3, x3 - x4

        c1 = x1 * a1 + y1 * b1
        c2 = x3 * a2 + y3 * b2

        def get_val(a,b, xp, yp):
            return xp*b + yp *(-a)
        
        r1 = sorted([get_val(a1,b1, x1, y1), get_val(a1,b1, x2, y2)])
        r2 = sorted([get_val(a2,b2, x3, y3), get_val(a2,b2, x4, y4)])



        d = a1 * b2 - a2 * b1
        if d==0: # parallel
            if c1 == c2:
                if (r1[0] <= r2[0] <= r1[1] or r1[0] <= r2[1] <= r1[1] or r2[0] <= r1[0] <= r2[1]):
                    print("YES")
                else:
                    print("NO")
                continue
            else: 
                print("NO")
                continue
        x = (c1 * b2 - c2 * b1) / d
        y = (a1 * c2 - a2 * c1) / d



        # def check_line(xl1, yl1, xl2, yl2):


        r = get_val(a1, b1, x,y)
        if not r1[0] <= r <= r1[1]:
            print("NO")
            continue
        r = get_val(a2, b2, x,y)
        if not r2[0] <= r <= r2[1]:
            print("NO")
            continue


        print("YES")




main()

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 3431 col 1: expected "YES", got "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