Submission details
Task:Line Segment Intersection
Sender:aalto26jh_002
Submission time:2026-11-09 15:00:13 +0200
Language:Python3 (PyPy3)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.46 sdetails
#2ACCEPTED0.32 sdetails
#3ACCEPTED0.32 sdetails
#4ACCEPTED0.35 sdetails
#5ACCEPTED0.05 sdetails
#6ACCEPTED0.05 sdetails

Code

# FULL TEMPLATE: delete every block you don't use. Blocks don't depend on each other.
# Nodes are 1..n (slot 0 unused). Arrays are 0-indexed.
import sys
from collections import deque, defaultdict, Counter
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
from itertools import accumulate, permutations, combinations, product
from functools import lru_cache, cmp_to_key
from math import gcd, isqrt, comb, inf
input = sys.stdin.readline
MOD = 10**9 + 7

def I():  return int(input())                    # "5"          -> 5
def MI(): return map(int, input().split())       # "8 4"        -> n, q = MI()
def LI(): return list(map(int, input().split())) # "3 2 4 5"    -> [3, 2, 4, 5]
def S():  return input().strip()                 # "BABAZA", ".*.#" (kept as text)
def LS(): return input().split()                 # "? 2 3"      -> ['?', '2', '3']


# ==== UNION-FIND ==== uf.join(a, b) -> False if already together, uf.size(x)
class UF:
    def __init__(self, n):
        self.e = [-1] * n

    def find(self, x):
        e = self.e
        while e[x] >= 0:
            if e[e[x]] >= 0:
                e[x] = e[e[x]]
            x = e[x]
        return x

    def size(self, x):
        return -self.e[self.find(x)]

    def join(self, a, b):
        a, b = self.find(a), self.find(b)
        if a == b:
            return False
        if self.e[a] > self.e[b]:
            a, b = b, a
        self.e[a] += self.e[b]
        self.e[b] = a
        return True


# ==== FENWICK ==== ft.add(i, d): a[i] += d.  ft.query(i) = a[0] + ... + a[i-1]
class FT:
    def __init__(self, n):
        self.s = [0] * n

    def add(self, i, d):
        s, n = self.s, len(self.s)
        while i < n:
            s[i] += d
            i |= i + 1

    def query(self, i):
        s, r = self.s, 0
        while i > 0:
            r += s[i - 1]
            i &= i - 1
        return r


# ==== SEGMENT TREE (min) ==== t.update(i, v): a[i] = v.  t.query(l, r) = min of a[l..r-1]
# sum/max: change INF and the 3 lines marked "op"
class Tree:
    INF = inf

    def __init__(self, a):
        n = self.n = len(a)
        t = self.t = [self.INF] * n + list(a)
        for i in range(n - 1, 0, -1):
            t[i] = min(t[2 * i], t[2 * i + 1])         # op

    def update(self, i, v):
        t = self.t
        i += self.n
        t[i] = v
        i >>= 1
        while i:
            t[i] = min(t[2 * i], t[2 * i + 1])         # op
            i >>= 1

    def query(self, l, r):
        t, res = self.t, self.INF
        l += self.n
        r += self.n
        while l < r:
            if l & 1:
                res = min(res, t[l])                    # op
                l += 1
            if r & 1:
                r -= 1
                res = min(res, t[r])                    # op
            l >>= 1
            r >>= 1
        return res


# ==== BFS (unweighted graph) ==== -> dist[v], -1 = unreachable
def bfs(adj, start):
    dist = [-1] * len(adj)
    dist[start] = 0
    q = deque([start])
    while q:
        u = q.popleft()
        for v in adj[u]:
            if dist[v] == -1:
                dist[v] = dist[u] + 1
                q.append(v)
    return dist


# ==== DIJKSTRA ==== wadj[u] = [(v, w)] -> dist[v], inf = unreachable
def dijkstra(wadj, start):
    dist = [inf] * len(wadj)
    dist[start] = 0
    pq = [(0, start)]
    while pq:
        d, u = heappop(pq)
        if d != dist[u]:
            continue
        for v, w in wadj[u]:
            if d + w < dist[v]:
                dist[v] = d + w
                heappush(pq, (dist[v], v))
    return dist


# ==== GRID BFS ==== g[r][c], '#' = wall -> dist[r][c], -1 = unreachable
DIRS = ((-1, 0), (1, 0), (0, -1), (0, 1))              # U D L R
def grid_bfs(g, sr, sc):
    R, C = len(g), len(g[0])
    dist = [[-1] * C for _ in range(R)]
    dist[sr][sc] = 0
    q = deque([(sr, sc)])
    while q:
        r, c = q.popleft()
        for dr, dc in DIRS:
            nr, nc = r + dr, c + dc
            if 0 <= nr < R and 0 <= nc < C and g[nr][nc] != '#' and dist[nr][nc] == -1:
                dist[nr][nc] = dist[r][c] + 1
                q.append((nr, nc))
    return dist


# ==== BINARY SEARCH ON THE ANSWER ==== smallest x in [lo, hi] with ok(x) True (ok: False...False True...True)
def first_true(lo, hi, ok):
    while lo < hi:
        mid = (lo + hi) // 2
        if ok(mid):
            hi = mid
        else:
            lo = mid + 1
    return lo


# mod power is built in: pow(b, e, MOD).  inverse: pow(a, MOD - 2, MOD)

def sub(a, b):   return (a[0] - b[0], a[1] - b[1])
def cross(a, b): return a[0] * b[1] - a[1] * b[0]   # > 0: b turns LEFT from a
def dot(a, b):   return a[0] * b[0] + a[1] * b[1]   # > 0: angle < 90 degrees


def orient(a, b, c):  # cross(b - a, c - a): > 0 c left of a->b, 0 on the line, < 0 right
    return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])


def dist2(a, b):  # squared distance: stays an integer, compare these instead of sqrt
    dx, dy = a[0] - b[0], a[1] - b[1]
    return dx * dx + dy * dy


def sign(x):
    return (x > 0) - (x < 0)


def point_location(p1, p2, p3):  # CSES 2189 wording: looking from p1 to p2
    c = orient(p1, p2, p3)
    return "LEFT" if c > 0 else "RIGHT" if c < 0 else "TOUCH"


def on_segment(p, a, b):  # p on the CLOSED segment ab: on the line AND inside the bounding box
    return (orient(a, b, p) == 0 and 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 segments_intersect(a, b, c, d):  # closed segments ab and cd share at least one point
    d1, d2 = sign(orient(a, b, c)), sign(orient(a, b, d))
    d3, d4 = sign(orient(c, d, a)), sign(orient(c, d, b))
    if d1 * d2 < 0 and d3 * d4 < 0:       # book case 3: c, d on different sides AND a, b too
        return True
    # cases 1 and 2 (overlap on one line, shared endpoint, T-touch): an endpoint lies on the other.
    # Works for single-point "segments" (a == b) too.
    return on_segment(c, a, b) or on_segment(d, a, b) or on_segment(a, c, d) or on_segment(b, c, d)


def main():

    out = []
    for _ in range(I()):
        x1, y1, x2, y2, x3, y3, x4, y4 = MI()
        out.append("YES" if segments_intersect((x1, y1), (x2, y2), (x3, y3), (x4, y4)) else "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