CSES - Aalto Competitive Programming 2024 - wk10 - Homework - Results
Submission details
Task:Line Segment Intersection
Sender:louaha1
Submission time:2024-11-12 10:47:08 +0200
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#10.04 sdetails
#20.04 sdetails
#30.04 sdetails
#40.04 sdetails
#50.04 sdetails
#60.04 sdetails

Code

def on_segment(x1, y1, x2, y2, x3, y3):
    return min(x1, x2) <= x3 <= max(x1, x2) and min(y1, y2) <= y3 <= max(y1, y2)

def orientation(x1, y1, x2, y2, x3, y3):
    val = (y2 - y1) * (x3 - x2) - (x2 - x1) * (y3 - y2)
    if val == 0:
        return 0
    return 1 if val > 0 else 2

def intersect(x1, y1, x2, y2, x3, y3, x4, y4):
    o1 = orientation(x1, y1, x2, y2, x3, y3)
    o2 = orientation(x1, y1, x2, y2, x4, y4)
    o3 = orientation(x3, y3, x4, y4, x1, y1)
    o4 = orientation(x3, y3, x4, y4, x2, y2)

    if o1 != o2 and o3 != o4:
        return True

    if o1 == 0 and on_segment(x1, y1, x2, y2, x3, y3): return True
    if o2 == 0 and on_segment(x1, y1, x2, y2, x4, y4): return True
    if o3 == 0 and on_segment(x3, y3, x4, y4, x1, y1): return True
    if o4 == 0 and on_segment(x3, y3, x4, y4, x2, y2): return True

    return False

def solve(test_cases):
    results = []
    for x1, y1, x2, y2, x3, y3, x4, y4 in test_cases:
        results.append("YES" if intersect(x1, y1, x2, y2, x3, y3, x4, y4) else "NO")
    return results

# Example usage
test_cases = [
    (1, 1, 5, 3, 1, 2, 4, 3),
    (1, 1, 5, 3, 1, 1, 4, 3),
    (1, 1, 5, 3, 2, 3, 4, 1),
    (1, 1, 5, 3, 2, 4, 4, 1),
    (1, 1, 5, 3, 3, 2, 7, 4)
]
print(solve(test_cases))

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', 'YES', 'YES', 'YES', 'Y...

Test 2

Verdict:

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

correct output
NO
NO
YES
NO
YES
...

user output
['NO', 'YES', 'YES', 'YES', 'Y...

Test 3

Verdict:

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

correct output
YES
YES
NO
YES
YES
...

user output
['NO', 'YES', 'YES', 'YES', 'Y...

Test 4

Verdict:

input
100000
788522666 939776556 -831492125...

correct output
NO
NO
NO
NO
NO
...

user output
['NO', 'YES', 'YES', 'YES', 'Y...

Test 5

Verdict:

input
1
1 6 6 6 4 4 1000000000 1000000...

correct output
YES

user output
['NO', 'YES', 'YES', 'YES', 'Y...

Test 6

Verdict:

input
1
-1000000000 1000000000 9999999...

correct output
NO

user output
['NO', 'YES', 'YES', 'YES', 'Y...