Task: | Line Segment Intersection |
Sender: | laluj |
Submission time: | 2024-11-10 14:11:45 +0200 |
Language: | Python3 (PyPy3) |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.88 s | details |
#2 | ACCEPTED | 0.66 s | details |
#3 | ACCEPTED | 0.69 s | details |
#4 | ACCEPTED | 0.70 s | details |
#5 | ACCEPTED | 0.07 s | details |
#6 | ACCEPTED | 0.07 s | details |
Code
import sysfrom typing import Listfrom cmath import phasefrom collections import namedtuple# Debug functiondef debug(*args):print("DEBUG:", ", ".join(str(arg) for arg in args), file=sys.stderr)# Define a point and segment using complex numbersPoint = complexSegment = namedtuple("Segment", ["p", "q"])def crossproduct(a: Point, b: Point) -> int:return (a.conjugate() * b).imagdef on_segment(r: Point, s: Segment) -> bool:return (min(s.p.real, s.q.real) <= r.real <= max(s.p.real, s.q.real) andmin(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 # Collinearreturn 1 if val > 0 else 2 # 1 for clockwise, 2 for counterclockwisedef 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 caseif o1 != o2 and o3 != o4:return True# Special casesif o1 == 0 and on_segment(s2.p, s1): return Trueif o2 == 0 and on_segment(s2.q, s1): return Trueif o3 == 0 and on_segment(s1.p, s2): return Trueif o4 == 0 and on_segment(s1.q, s2): return Truereturn False# Main function to read input and process each test casedef 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 ... Truncated |
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 ... Truncated |
Test 3
Verdict: ACCEPTED
input |
---|
100000 492853 -452834 -657156 -282543... |
correct output |
---|
YES YES NO YES YES ... |
user output |
---|
YES YES NO YES YES ... Truncated |
Test 4
Verdict: ACCEPTED
input |
---|
100000 788522666 939776556 -831492125... |
correct output |
---|
NO NO NO NO NO ... |
user output |
---|
NO NO NO NO NO ... Truncated |
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 |