Submission details
Task:Line Segment Intersection
Sender:aalto26jh_011
Submission time:2026-11-09 14:22:39 +0200
Language:Python3 (PyPy3)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.44 sdetails
#2ACCEPTED0.45 sdetails
#3ACCEPTED0.57 sdetails
#4ACCEPTED0.63 sdetails
#5ACCEPTED0.07 sdetails
#6ACCEPTED0.07 sdetails

Code



import sys
from fractions import Fraction

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 /a1 == c2 /a2:
            r12 = sorted([get_val(a1,b1, x3, y3), get_val(a1,b1, x4, y4)])
            # if (a1 != 0 and c1 *a2 == c2 *a1) or (c1 *b2 == c2 *b1):
            if a1 * x3 + b1 * y3 == c1:
                if (r1[0] <= r12[0] <= r1[1] or r1[0] <= r12[1] <= r1[1] or r12[0] <= r1[0] <= r12[1]):
                    print("YES")
                else:
                    print("NO")
                continue
            else: 
                print("NO")
                continue
        # x = (c1 * b2 - c2 * b1) / d
        # y = (a1 * c2 - a2 * c1) / d
        x = (c1 * b2 - c2 * b1)
        y = (a1 * c2 - a2 * c1)

        # x = Fraction(c1 * b2 - c2 * b1, d)
        # y = Fraction(a1 * c2 - a2 * c1, d)



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


        r1 = sorted([get_val(a1,b1, x1*d, y1*d), get_val(a1,b1, x2*d, y2*d)])
        r2 = sorted([get_val(a2,b2, x3*d, y3*d), get_val(a2,b2, x4*d, y4*d)])


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