| Task: | Line Segment Intersection |
| Sender: | aalto26jh_014 |
| Submission time: | 2026-11-07 15:57:19 +0200 |
| Language: | Python3 (PyPy3) |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.30 s | details |
| #2 | ACCEPTED | 0.26 s | details |
| #3 | ACCEPTED | 0.30 s | details |
| #4 | ACCEPTED | 0.30 s | details |
| #5 | ACCEPTED | 0.04 s | details |
| #6 | ACCEPTED | 0.04 s | details |
Code
import sys
def cross(a, b):
# print(a, b)
return a[0]*b[1] - a[1]*b[0]
def on_seg(a,b,p):
return (min(a[0], b[0]) <= p[0] <= max(a[0],b[0]) and
min(a[1], b[1]) <= p[1] <= max(a[1],b[1]))
def orient(a, b, c):
o = cross((b[0]-a[0], b[1]-a[1]), (c[0]-a[0], c[1]-a[1]))
return (o>0) - (o<0)
def main():
n = int(input())
l = sys.stdin.read().strip().split('\n')
out = []
for line in l:
x1, y1, x2, y2, x3, y3, x4, y4 = map(int, line.split())
a, b, c, d = (x1, y1), (x2, y2), (x3, y3), (x4, y4)
# cross (1,2,3) = cross(2-1, 3-1)
o1 = orient(a, b, c)
o2 = orient(a, b, d)
o3 = orient(c, d, a)
o4 = orient(c, d, b)
if (o1!= o2) and (o3!= o4):
out.append('YES'); continue
if (not o1) and on_seg(a, b, c):
out.append('YES'); continue
if (not o2) and on_seg(a, b, d):
out.append('YES'); continue
if (not o3) and on_seg(c, d, a):
out.append('YES'); continue
if (not o4) and on_seg(c, d, b):
out.append('YES'); continue
out.append('NO')
print("\n".join(out))
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 |
