CSES - Aalto Competitive Programming 2024 - wk10 - Homework - Results
Submission details
Task:Line Segment Intersection
Sender:laluj
Submission time:2024-11-10 14:11:45 +0200
Language:Python3 (PyPy3)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.88 sdetails
#2ACCEPTED0.66 sdetails
#3ACCEPTED0.69 sdetails
#4ACCEPTED0.70 sdetails
#5ACCEPTED0.07 sdetails
#6ACCEPTED0.07 sdetails

Code

import sys
from typing import List
from cmath import phase
from collections import namedtuple

# Debug function
def debug(*args):
    print("DEBUG:", ", ".join(str(arg) for arg in args), file=sys.stderr)

# Define a point and segment using complex numbers
Point = complex
Segment = namedtuple("Segment", ["p", "q"])

def crossproduct(a: Point, b: Point) -> int:
    return (a.conjugate() * b).imag

def on_segment(r: Point, s: Segment) -> bool:
    return (min(s.p.real, s.q.real) <= r.real <= max(s.p.real, s.q.real) and
            min(s.p.imag, s.q.imag) <= r.imag <= max(s.p.imag, s.q.imag))

def orientation(r: Point, s: Segment) -> int:
    val = crossproduct(r - s.p, r - s.q)
    if val == 0:
        return 0  # Collinear
    return 1 if val > 0 else 2  # 1 for clockwise, 2 for counterclockwise

def do_intersect(s1: Segment, s2: Segment) -> bool:
    o1 = orientation(s2.p, s1)
    o2 = orientation(s2.q, s1)
    o3 = orientation(s1.p, s2)
    o4 = orientation(s1.q, s2)
        
    # General case
    if o1 != o2 and o3 != o4:
        return True

    # Special cases
    if o1 == 0 and on_segment(s2.p, s1): return True
    if o2 == 0 and on_segment(s2.q, s1): return True
    if o3 == 0 and on_segment(s1.p, s2): return True
    if o4 == 0 and on_segment(s1.q, s2): return True

    return False

# Main function to read input and process each test case
def main():
    t = int(input().strip())
    tests = []

    for _ in range(t):
        x1, y1, x2, y2, x3, y3, x4, y4 = map(int, input().strip().split())
        s1 = Segment(Point(x1, y1), Point(x2, y2))
        s2 = Segment(Point(x3, y3), Point(x4, y4))
        tests.append((s1, s2))

    for s1, s2 in tests:
        if do_intersect(s1, s2):
            print("YES")
        else:
            print("NO")

if __name__ == "__main__":
    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