Submission details
Task:Line Segment Intersection
Sender:aalto26jh_011
Submission time:2026-11-09 14:17:19 +0200
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#1--details
#2--details
#3--details
#4--details
#5ACCEPTED0.08 sdetails
#6ACCEPTED0.08 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 c1 *a2 == c2 *a1:
                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 = Fraction(c1 * b2 - c2 * b1, d)
        y = Fraction(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
(empty)

Test 2

Verdict:

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

correct output
NO
NO
YES
NO
YES
...

user output
(empty)

Test 3

Verdict:

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

correct output
YES
YES
NO
YES
YES
...

user output
(empty)

Test 4

Verdict:

input
100000
788522666 939776556 -831492125...

correct output
NO
NO
NO
NO
NO
...

user output
(empty)

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