Problem:


Railroad Conflict

Nate U. Smith は大都市圏の鉄道会社を経営している．この大都市圏には，彼の鉄道会社以外にライバルの鉄道会社がある．2 社は互いに競合関係にある．

この大都市圏では，列車の運行を容易にするため，全ての路線は両端の駅を結んだ線分を経路としている．また，踏切等の設置をさけるため，全ての路線は高架または地下に敷設されている．既存路線は，全線にわたって高架を走るか，または全線にわたって地下を走るかのいずれかである．

ところで，最近，この大都市圏にある 2 つの街区 A，B がさかんに開発されていることから，Nate の会社ではこれらの街区の間に新しい路線を設けることを決断した．この新路線は，従来の路線と同様に A，B を両端とする線分を経路としたいが，経路の途中には別の路線があることから，高架あるいは地下のいずれかに全線を敷設することは不可能である．そこで，路線の一部を高架に，残りの部分を地下に敷設することにした．このとき，新路線が自社の既存路線と交わるところでは，新路線と既存路線との間の乗り継ぎを便利にするため，既存路線が高架ならば新路線も高架に，既存路線が地下ならば新路線も地下を走るようにする．また，新路線が他社の既存路線と交わるところでは，他社による妨害をさけるため，既存路線が高架ならば新路線は地下に，既存路線が地下ならば新路線は高架を走るようにする．A 駅および B 駅をそれぞれ高架あるいは地下のいずれに設けるかは特に問わない．

当然のことながら，新路線が高架から地下に，あるいは地下から高架に移るところには出入口を設けなければならない．ところが，出入口を設けるためには費用がかかるため，新路線に設ける出入口の個数は最小限に抑えたい．それには，プログラマであるあなたの助けを要すると考えた Nate は，あなたを彼の会社に呼び出した．

あなたの仕事は，A 駅，B 駅の位置，そして既存路線に関する情報が与えられたときに，新路線において最低限設けなければならない出入口の個数を求めるプログラムを作成することである．

以下の図は，サンプルとして与えた入力および出力の内容を示したものである．

Input

入力の先頭行には単一の正の整数が含まれ，これはデータセットの個数を表す．それぞれのデータセットは次の形式で与えられる．

xa ya xb yb

n

xs1 ys1 xt1 yt1 o1 l1

xs2 ys2 xt2 yt2 o2 l2

...

xsn ysn xtn ytn on ln

ただし，(xa,ya) および (xb,yb) はそれぞれ A 駅，B 駅の座標を表す．N は既存路線の数を表す 100 以下の正の整数である．(xsi,ysi) および (ysi,yti) は i 番目の既存路線における始点および終点の座標を表す．oi は i 番目の既存路線の所有者を表す整数である．これは 1 または 0 のいずれかであり，1 はその路線が自社所有であることを，0 は他社所有であることをそれぞれ表す．li は i 番目の既存路線が高架または地下のいずれにあるかを表す整数である．これも 1 または 0 のいずれかであり，1 はその路線が高架にあることを，0 は地下にあることをそれぞれ表す．

入力中に現れる x 座標および y 座標の値は -10000 から 10000 の範囲にある（両端も範囲に含まれる）．また，それぞれのデータセットにおいて，新路線を含めた全ての路線に対して，以下の条件を満たすと仮定してよい．ここで 2 点が非常に近いとは，その 2 点間の距離が 10-9 以下であることを表す．

ある交点の非常に近くに別の交点があることはない．

ある路線の端点の非常に近くを別の路線が通ることはない．

2 つの路線の一部または全部が重なることはない．

Output

それぞれのデータセットに対して，最低限設けなければならない出入口の個数を 1 行に出力しなさい．

Sample Input

2
-10 1 10 1
4
-6 2 -2 -2 0 1
-6 -2 -2 2 1 0
6 2 2 -2 0 0
6 -2 2 2 1 1
8 12 -7 -3
8
4 -5 4 -2 1 1
4 -2 4 9 1 0
6 9 6 14 1 1
-7 6 7 6 0 0
1 0 1 10 0 0
-5 0 -5 10 0 1
-7 0 7 0 0 1
-1 0 -1 -5 0 1

Output for the Sample Input

1
3



Sample input:
2
-10 1 10 1
4
-6 2 -2 -2 0 1
-6 -2 -2 2 1 0
6 2 2 -2 0 0
6 -2 2 2 1 1
8 12 -7 -3
8
4 -5 4 -2 1 1
4 -2 4 9 1 0
6 9 6 14 1 1
-7 6 7 6 0 0
1 0 1 10 0 0
-5 0 -5 10 0 1
-7 0 7 0 0 1
-1 0 -1 -5 0 1


Sample output:
1
3

Submission status: Wrong Answer

Solution code:
def getlineeq(x1,y1,x2,y2):
    if x1==x2:
        return(0,0,x1) #x=a
    else:
        a=(y2-y1)/(x2-x1)
        b=y1-a*x1
        return(1,a,b)  #y=ax+b

def getcross(d1,a1,b1,d2,a2,b2,x1,x2,xs1,xs2):
    if d1==0 and d2==0:
        cp=[0,99999,99999]
    elif d1==0:
        cp= [0,b1,a2*b1+b2]
    elif d2==0:
        cp=[0,b2,a1*b2+b1]
    else:
        if a1==a2:
            cp=[0,99999,99999]
        else:
            cp=[0,(b2-b1)/(a1-a2),(b2-b1)/(a1-a2)*a1+b1]
    if cp[1]<=max(x1,x2) and cp[1]>=min(x1,x2):
        if cp[1]<=max(xs1,xs2) and cp[1]>=min(xs1,xs2):
            cp[0]=1
    return cp

n=int(raw_input())
for i in range(n):
    [xa,ya,xb,yb]=[float(x) for x in raw_input().split()]
    m=int(raw_input())
    [d1,a1,b1]=getlineeq(xa,ya,xb,yb)
    crosspoints=[]
    for j in range(m):
        linedata=raw_input().split()
        [xs1,ys1,xs2,ys2]=[float(x) for x in linedata[:4]]
        [o,h]=[int(x) for x in linedata[4:]]
        (d2,a2,b2)=getlineeq(xs1,ys1,xs2,ys2)
        crosspoints.append((getcross(d1,a1,b1,d2,a2,b2,xa,xb,xs1,xs2),o,h))
    crosspoints.sort(key=lambda x:x[0][1])
    k=2 #height now
    S=0
    for j in range(m):
        [cpd,o,h]=crosspoints[j]
        [cd,cx,cy]=cpd
        s=1-(o^h) #height supposed to be
        if cd!=0:
            if k!=s:
                S=S+1
                k=s
    print S-1

Submission status: Wrong Answer

Solution code:
def getlineeq(x1,y1,x2,y2):
    if x1==x2:
        return(0,0,x1) #x=a
    else:
        a=(y2-y1)/(x2-x1)
        b=y1-a*x1
        return(1,a,b)  #y=ax+b

def getcross(d1,a1,b1,d2,a2,b2,x1,x2,y1,y2,xs1,xs2,ys1,ys2):
    if d1==0 and d2==0:
        cp=[0,99999,99999]
    elif d1==0:
        cp= [0,b1,a2*b1+b2]
    elif d2==0:
        cp=[0,b2,a1*b2+b1]
    else:
        if a1==a2:
            cp=[0,99999,99999]
        else:
            cp=[0,(b2-b1)/(a1-a2),(b2-b1)/(a1-a2)*a1+b1]
    if cp[1]<=max(x1,x2) and cp[1]>=min(x1,x2) and cp[2]<=max(y1,y2) and cp[2]>=min(y1,y2):
        if cp[1]<=max(xs1,xs2) and cp[1]>=min(xs1,xs2) and cp[2]<=max(ys1,ys2) and cp[2]>=min(ys1,ys2):
            cp[0]=1
    return cp

n=int(raw_input())
for i in range(n):
    [xa,ya,xb,yb]=[float(x) for x in raw_input().split()]
    m=int(raw_input())
    [d1,a1,b1]=getlineeq(xa,ya,xb,yb)
    crosspoints=[]
    for j in range(m):
        linedata=raw_input().split()
        [xs1,ys1,xs2,ys2]=[float(x) for x in linedata[:4]]
        [o,h]=[int(x) for x in linedata[4:]]
        (d2,a2,b2)=getlineeq(xs1,ys1,xs2,ys2)
        crosspoints.append((getcross(d1,a1,b1,d2,a2,b2,xa,xb,ya,yb,xs1,xs2,ys1,ys2),o,h))
    crosspoints.sort(key=lambda x:x[0][1])
    k=2 #height now
    S=0
    for j in range(m):
        [cpd,o,h]=crosspoints[j]
        [cd,cx,cy]=cpd
        s=1-(o^h) #height supposed to be
        if cd!=0:
            if k!=s:
                S=S+1
                k=s
    print S-1

Submission status: Wrong Answer

Solution code:
def getlineeq(x1,y1,x2,y2):
    if x1==x2:
        return(0,0,x1) #x=a
    else:
        a=(y2-y1)/(x2-x1)
        b=y1-a*x1
        return(1,a,b)  #y=ax+b

def getcross(d1,a1,b1,d2,a2,b2,x1,x2,y1,y2,xs1,xs2,ys1,ys2):
    if d1==0 and d2==0:
        cp=[0,99999,99999]
    elif d1==0:
        cp= [0,b1,a2*b1+b2]
    elif d2==0:
        cp=[0,b2,a1*b2+b1]
    else:
        if a1==a2:
            cp=[0,99999,99999]
        else:
            cp=[0,(b2-b1)/(a1-a2),(b2-b1)/(a1-a2)*a1+b1]
    if cp[1]<=max(x1,x2) and cp[1]>=min(x1,x2) and cp[2]<=max(y1,y2) and cp[2]>=min(y1,y2):
        if cp[1]<=max(xs1,xs2) and cp[1]>=min(xs1,xs2) and cp[2]<=max(ys1,ys2) and cp[2]>=min(ys1,ys2):
            cp[0]=1
    return cp

n=int(raw_input())
for i in range(n):
    [xa,ya,xb,yb]=[float(x) for x in raw_input().split()]
    m=int(raw_input())
    [d1,a1,b1]=getlineeq(xa,ya,xb,yb)
    crosspoints=[]
    for j in range(m):
        linedata=raw_input().split()
        [xs1,ys1,xs2,ys2]=[float(x) for x in linedata[:4]]
        [o,h]=[int(x) for x in linedata[4:]]
        (d2,a2,b2)=getlineeq(xs1,ys1,xs2,ys2)
        crosspoints.append((getcross(d1,a1,b1,d2,a2,b2,xa,xb,ya,yb,xs1,xs2,ys1,ys2),o,h))
    crosspoints.sort(key=lambda x:x[0][1])
    k=2 #height now
    S=0
    for j in range(m):
        [cpd,o,h]=crosspoints[j]
        [cd,cx,cy]=cpd
        s=1-(o^h) #height supposed to be
        if cd!=0:
            if k!=s:
                S=S+1
                k=s
    print max(S-1,0)

Submission status: Wrong Answer

Solution code:
def getlineeq(x1,y1,x2,y2):
    if x1==x2:
        return(0,0,x1) #x=a
    else:
        a=(y2-y1)/(x2-x1)
        b=y1-a*x1
        return(1,a,b)  #y=ax+b

def getcross(d1,a1,b1,d2,a2,b2,x1,x2,y1,y2,xs1,xs2,ys1,ys2):
    if d1==0 and d2==0:
        cp=[0,99999,99999]
    elif d1==0:
        cp= [0,b1,a2*b1+b2]
    elif d2==0:
        cp=[0,b2,a1*b2+b1]
    else:
        if a1==a2:
            cp=[0,99999,99999]
        else:
            cp=[0,(b2-b1)/(a1-a2),(b2-b1)/(a1-a2)*a1+b1]
    if cp[1]<=max(x1,x2) and cp[1]>=min(x1,x2) and cp[2]<=max(y1,y2) and cp[2]>=min(y1,y2):
        if cp[1]<=max(xs1,xs2) and cp[1]>=min(xs1,xs2) and cp[2]<=max(ys1,ys2) and cp[2]>=min(ys1,ys2):
            cp[0]=1
    return cp

n=int(raw_input())
for i in range(n):
    [xa,ya,xb,yb]=[float(x) for x in raw_input().split()]
    m=int(raw_input())
    [d1,a1,b1]=getlineeq(xa,ya,xb,yb)
    crosspoints=[]
    for j in range(m):
        linedata=raw_input().split()
        [xs1,ys1,xs2,ys2]=[float(x) for x in linedata[:4]]
        [o,h]=[int(x) for x in linedata[4:]]
        (d2,a2,b2)=getlineeq(xs1,ys1,xs2,ys2)
        crosspoints.append((getcross(d1,a1,b1,d2,a2,b2,xa,xb,ya,yb,xs1,xs2,ys1,ys2),o,h))
    crosspoints.sort(key=lambda x:x[0][1])
    k=2 #height now
    S=0
    print crosspoints
    for j in range(m):
        [cpd,o,h]=crosspoints[j]
        [cd,cx,cy]=cpd
        s=1-(o^h) #height supposed to be
        if cd!=0:
            if k!=s:
                S=S+1
                k=s
    print max(S-1,0)

Submission status: Wrong Answer

Solution code:
m = int(input())
while m:
    m -= 1
    ax, ay, bx, by = map(int, input().split())
    n = int(input())
    dx, dy = bx - ax, by - ay
    intersections = set()
    while n:
        n -= 1
        sx, sy, tx, ty, o, l = map(int, input().split())
        tdx, tdy = tx - sx, ty - sy
        asx, asy = sx - ax, sy - ay
        denom = dx * tdy - dy * tdx
        if not denom:
            continue
        r = (tdy * asx - tdx * asy) / denom
        s = (dy * asx - dx * asy) / denom
        if not 0 < r < 1 or not 0 < s < 1:
            continue
        px = ax + r * dx
        intersections.add((px, o == l))
    pl, counter = -1, -1
    for px, l in sorted(intersections):
        if pl != l:
            counter += 1
            pl = l
    print(counter)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def intersection(a1, a2, b1, b2):
    x1,y1 = a1
    x2,y2 = a2
    x3,y3 = b1
    x4,y4 = b2

    ksi = (y4 - y3) * (x4 - x1) - (x4 - x3) * (y4 - y1)
    eta = (x2 - x1) * (y4 - y1) - (y2 - y1) * (x4 - x1)
    delta = (x2 - x1) * (y4 - y3) - (y2 - y1) * (x4 - x3)
    if delta == 0:
        return None
    ramda = ksi / delta;
    mu = eta / delta;
    if ramda >= 0 and ramda <= 1 and mu >= 0 and mu <= 1:
        return (x1 + ramda * (x2 - x1), y1 + ramda * (y2 - y1))

    return None

def main():
    rr = []

    n = I()
    ni = 0

    while ni < n:
        ni += 1
        xa,ya,xb,yb = LI()
        a1 = (xa,ya)
        a2 = (xb,yb)
        m = I()
        a = [LI() for _ in range(m)]
        t = []
        for xs,ys,xt,yt,o,l in a:
            k = intersection(a1,a2,(xs,ys),(xt,yt))
            if k is None:
                continue
            t.append((k,o^l))
        if len(t) == 0:
            rr.append(0)
            continue

        t.sort()
        c = t[0][1]
        r = 0
        for _,tc in t:
            if tc != c:
                r += 1
                c = tc

        rr.append(r)


    return '\n'.join(map(str, rr))



print(main())

Submission status: Accepted

Solution code:
m = int(input())
while m:
    m -= 1
    ax, ay, bx, by = map(int, input().split())
    n = int(input())
    dx, dy = bx - ax, by - ay
    intersections = set()
    while n:
        n -= 1
        sx, sy, tx, ty, o, l = map(int, input().split())
        tdx, tdy = tx - sx, ty - sy
        asx, asy = sx - ax, sy - ay
        denom = dx * tdy - dy * tdx
        if not denom:
            continue
        r = (tdy * asx - tdx * asy) / denom
        s = (dy * asx - dx * asy) / denom
        if not 0 < r < 1 or not 0 < s < 1:
            continue
        px = ax + r * dx
        intersections.add((px, o == l))

    if not intersections:
        print(0)
        continue

    pl, counter = -1, -1
    for px, l in sorted(intersections):
        if pl != l:
            counter += 1
            pl = l
    print(counter)

Submission status: Accepted

Solution code:
m = int(input())
while m:
    m -= 1
    ax, ay, bx, by = map(int, input().split())
    n = int(input())
    dx, dy = bx - ax, by - ay
    intersections = set()
    while n:
        n -= 1
        sx, sy, tx, ty, o, l = map(int, input().split())
        tdx, tdy = tx - sx, ty - sy
        asx, asy = sx - ax, sy - ay
        denom = dx * tdy - dy * tdx
        if not denom:
            continue
        r = (tdy * asx - tdx * asy) / denom
        s = (dy * asx - dx * asy) / denom
        if not 0 < r < 1 or not 0 < s < 1:
            continue
        px = ax + r * dx
        intersections.add((px, o == l))

    pl, counter = -1, -1
    for px, l in sorted(intersections):
        counter += pl != l
        pl = l
    print(max(counter, 0))

Submission status: Accepted

Solution code:
m = int(input())
while m:
    m -= 1
    ax, ay, bx, by = map(int, input().split())
    n = int(input())
    dx, dy = bx - ax, by - ay
    intersections = set()
    while n:
        n -= 1
        sx, sy, tx, ty, o, l = map(int, input().split())
        tdx, tdy = tx - sx, ty - sy
        asx, asy = sx - ax, sy - ay
        denom = dx * tdy - dy * tdx
        if not denom:
            continue
        r = (tdy * asx - tdx * asy) / denom
        s = (dy * asx - dx * asy) / denom
        if not 0 < r < 1 or not 0 < s < 1:
            continue
        intersections.add((r, o == l))

    pl, counter = -1, -1
    for r, l in sorted(intersections):
        counter += pl != l
        pl = l
    print(max(counter, 0))

Submission status: Accepted

Solution code:
m = int(input())
while m:
    m -= 1
    ax, ay, bx, by = map(int, input().split())
    n = int(input())
    dx, dy = bx - ax, by - ay
    intersections = set()
    while n:
        n -= 1
        sx, sy, tx, ty, o, l = map(int, input().split())
        tdx, tdy = tx - sx, ty - sy
        asx, asy = sx - ax, sy - ay
        denom = dx * tdy - dy * tdx
        if not denom:
            continue
        r = (tdy * asx - tdx * asy) / denom
        s = (dy * asx - dx * asy) / denom
        if not 0 < r < 1 or not 0 < s < 1:
            continue
        intersections.add((r, o == l))

    intersections = [l[1] for l in sorted(intersections)]
    print(sum(i != j for i, j in zip(intersections, intersections[1:])))

Submission status: Accepted

Solution code:
for i in range(int(input())):
    xa, ya, xb, yb = map(int, input().split())
    n = int(input())
    ip = []
    for j in range(n):
        xs, ys, xt, yt, o, l = map(int, input().split())
        d = (xb - xa) * (yt - ys) - (yb - ya) * (xt - xs)
        if d == 0:
            continue
        t0 = ((yt - ys) * (xs - xa) - (xt - xs) * (ys - ya)) / d
        t1 = ((yb - ya) * (xs - xa) - (xb - xa) * (ys - ya)) / d
        if (t0 > 0 and t0 < 1) and (t1 > 0 and t1 < 1):
            ip.append((t0, o == l))
    count = 0
    if len(ip) > 0:
        ip.sort(key=lambda x: x[0])
        for j in range(len(ip) - 1):
            if ip[j][1] != ip[j + 1][1]:
                count += 1
    print(count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(10000000)

def in_sec(a, b, c):
    return min(a, b) <= c <= max(a, b)

q = input()

for loop in range(q):
    x0, y0, x1, y1 = map(int, raw_input().split())
    n = input()
    ls = []
    for i in range(n):
        x2, y2, x3, y3, o, l = map(int, raw_input().split())
        flag = (o + l) % 2
        #(y1 - y0)(x - x0) = (x1 - x0)(y - y0)
        a1, b1, c1 = [y1 - y0, x0 - x1, x0 * (y1 - y0) - y0 * (x1 - x0)]
        a2, b2, c2 = [y3 - y2, x2 - x3, x2 * (y3 - y2) - y2 * (x3 - x2)]
        det = a1 * b2 - b1 * a2
        if det == 0:
            dx, dy = [x2 - x0, y2 - y0]
            if dx * a1 + dy * b1 != 0:
                pass
            elif max(x0, x1) < min(x2, x3) or max(x2, x3) < min(x0, x1) \
                or max(y0, y1) < min(y2, y3) or max(y2, y3) < min(y0, y1):
                pass
            else:
                p = [(x0, y0), (x1, y1), (x2, y2), (x3, y3)]
                ls += [(p[i], flag) for i in range(4) for j in range(4) if p[i] == p[j]]
            continue
        
        xdet = c1 * b2 - c2 * b1
        ydet = c2 * a1 - c1 * a2
        if in_sec(det * x0, det * x1, xdet) and in_sec(det * y0, det * y1, ydet) \
            and in_sec(det * x2, det * x3, xdet) and in_sec(det * y2, det * y3, ydet):
            det = float(det)
            ls += [((xdet / det, ydet / det), flag)]
    ls.sort()
    ans = 0
    for i in range(len(ls) - 1):
        if ls[i][1] != ls[i + 1][1]:
            ans += 1
    print ans

Submission status: Accepted

Solution code:
from sys import stdin

def solve():
    file_input = stdin
    data_sets_num = int(file_input.readline())
    for i in range(data_sets_num):
        xa, ya, xb, yb = map(int, file_input.readline().split())
        x_ab = xb - xa
        y_ab = yb - ya
        n = int(file_input.readline())
        cross_points = []
        for j in range(n):
            xs, ys, xt, yt, o, l = map(int, file_input.readline().split())
            # Intersection judgment
            c_abs = x_ab * (ys - ya) - y_ab * (xs - xa)
            c_abt = x_ab * (yt - ya) - y_ab * (xt - xa)
            if c_abs * c_abt <= 0:
                x_st = xt - xs
                y_st = yt - ys
                c_sta = x_st * (ya - ys) - y_st * (xa - xs)
                c_stb = x_st * (yb - ys) - y_st * (xb - xs)
                if c_sta * c_stb < 0:
                    # find cross point
                    r = -c_abs / (x_ab * y_st - y_ab * x_st)
                    cross_points.append((xs + r * x_st, ys + r * y_st, o, l))
        if cross_points:
            cross_points.sort()
            owner = cross_points[0][2]
            pos = cross_points[0][3]
            ans = 0
            for x_cp, y_cp, o, l in cross_points[1:]:
                if (o == owner and l != pos ) or (o != owner and l == pos):
                    ans += 1
                owner = o
                pos = l
            print(ans)
        else:
            print(0)

solve()

Submission status: Runtime Error

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def intersection(a1, a2, b1, b2):
    x1,y1 = a1
    x2,y2 = a2
    x3,y3 = b1
    x4,y4 = b2

    ksi = (y4 - y3) * (x4 - x1) - (x4 - x3) * (y4 - y1)
    eta = (x2 - x1) * (y4 - y1) - (y2 - y1) * (x4 - x1)
    delta = (x2 - x1) * (y4 - y3) - (y2 - y1) * (x4 - x3)
    ramda = ksi / delta;
    mu = eta / delta;
    if ramda >= 0 and ramda <= 1 and mu >= 0 and mu <= 1:
        return (x1 + ramda * (x2 - x1), y1 + ramda * (y2 - y1))

    return None

def main():
    rr = []

    n = I()
    ni = 0

    while ni < n:
        ni += 1
        xa,ya,xb,yb = LI()
        a1 = (xa,ya)
        a2 = (xb,yb)
        m = I()
        a = [LI() for _ in range(m)]
        t = []
        for xs,ys,xt,yt,o,l in a:
            k = intersection(a1,a2,(xs,ys),(xt,yt))
            if k is None:
                continue
            t.append((k,o^l))
        if len(t) == 0:
            rr.append(0)
            continue

        t.sort()
        c = t[0][1]
        r = 0
        for _,tc in t:
            if tc != c:
                r += 1
                c = tc

        rr.append(r)


    return '\n'.join(map(str, rr))



print(main())

Submission status: Runtime Error

Solution code:
for i in range(int(input())):
    xa, ya, xb, yb = map(int, input().split(' '))
    n = int(input())
    ip = []
    for j in range(n):
        xs, ys, xt, yt, o, l = map(int, input().split(' '))
        d = (xb - xa) * (yt - ys) - (yb - ya) * (xt - xs)
        if d == 0:
            continue
        t0 = ((yt - ys) * (xs - xa) - (xt - xs) * (ys - ya)) / d
        t1 = ((yb - ya) * (xs - xa) - (xb - xa) * (ys - ya)) / d
        if t0 < 0 or t0 > 1 or t1 < 0 or t1 > 1:
            continue
        ip.append((t0, o, l))
    ip.sort(key=lambda x: x[0])
    count = 0
    if len(ip) > 0:
        l = ip[0][2] if ip[0][1] == 1 else abs(ip[0][2] - 1)
        for p in ip[1:]:
            if p[1] == 0 and l == p[2]:
                l = abs(l - 1)
                count += 1
            elif p[1] == 1 and l != p[2]:
                l = abs(l - 1)
                count += 1
    print(count)

Submission status: Runtime Error

Solution code:
for i in range(int(input())):
    xa, ya, xb, yb = map(int, input().split(' '))
    n = int(input())
    ip = []
    for j in range(n):
        xs, ys, xt, yt, o, l = map(int, input().split(' '))
        d = (xb - xa) * (yt - ys) - (yb - ya) * (xt - xs)
            continue
        t0 = ((yt - ys) * (xs - xa) - (xt - xs) * (ys - ya)) / d
        t1 = ((yb - ya) * (xs - xa) - (xb - xa) * (ys - ya)) / d
        if (t0 >= 0 and t0 <= 1) and (t1 >= 0 and t1 <= 1):
            ip.append((t0, o == l))
    count = 0
    if len(ip) > 0
        ip.sort(key=lambda x: x[0])
        for j in range(len(ip) - 1):
            if ip[i][1] != ip[i + 1][1]:
                count += 1
    print(count)

Submission status: Runtime Error

Solution code:
for i in range(int(input())):
    xa, ya, xb, yb = map(int, input().split(' '))
    n = int(input())
    ip = []
    for j in range(n):
        xs, ys, xt, yt, o, l = map(int, input().split(' '))
        d = (xb - xa) * (yt - ys) - (yb - ya) * (xt - xs)
        if d == 0:
            continue
        t0 = ((yt - ys) * (xs - xa) - (xt - xs) * (ys - ya)) / d
        t1 = ((yb - ya) * (xs - xa) - (xb - xa) * (ys - ya)) / d
        if (t0 >= 0 and t0 <= 1) and (t1 >= 0 and t1 <= 1):
            ip.append((t0, o == l))
    count = 0
    if len(ip) > 0
        ip.sort(key=lambda x: x[0])
        for j in range(len(ip) - 1):
            if ip[i][1] != ip[i + 1][1]:
                count += 1
    print(count)

Submission status: Runtime Error

Solution code:
for i in range(int(input())):
    xa, ya, xb, yb = map(int, input().split(' '))
    n = int(input())
    ip = []
    for j in range(n):
        xs, ys, xt, yt, o, l = map(int, input().split(' '))
        d = (xb - xa) * (yt - ys) - (yb - ya) * (xt - xs)
        if d == 0:
            continue
        t0 = ((yt - ys) * (xs - xa) - (xt - xs) * (ys - ya)) / d
        t1 = ((yb - ya) * (xs - xa) - (xb - xa) * (ys - ya)) / d
        if (t0 > 0 and t0 < 1) and (t1 > 0 and t1 < 1):
            ip.append((t0, o == l))
    count = 0
    if len(ip) > 0:
        ip.sort(key=lambda x: x[0])
        for j in range(len(ip) - 1):
            if ip[j][1] != ip[j + 1][1]:
                count += 1
    print(count)