Problem p02605 (python)

Fixed submission:
N = int(input())
XYUs = [list(map(str,input().split())) for _ in range(N)]

for i in range(len(XYUs)):
    XYUs[i][0] = int(XYUs[i][0])
    XYUs[i][1] = int(XYUs[i][1])


def check_the_conflict(XYUs):
    slopes = [[] for _ in range(400001)]
    for XYU in XYUs:
        if XYU[2] == "R" or XYU[2] == "D":       
            slopes[int(XYU[1])-int(XYU[0])+200000].append([int(XYU[0]),XYU[2]])    
    return slopes

def rotate_matrix(XYUs):
    converted = []
    for XYU in XYUs:
        if XYU[2] == "R":
            direction = "D"
        elif XYU[2] == "D":
            direction = "L"
        elif XYU[2] == "L":
            direction = "U"
        elif XYU[2] == "U":
            direction = "R"
        converted.append([XYU[1], 200000-XYU[0], direction])
    return converted
    
min_val = int(1e+10)

for i in range(4):
    if i > 0:
        XYUs = rotate_matrix(XYUs)
    slopes = check_the_conflict(XYUs)
    for slope in slopes:
        now_R = -int(1e+10)
        slope = sorted(slope)
        for airplane in slope:
            if airplane[1] == "R":
                now_R = airplane[0]
            elif airplane[1] == "D":
                candidate = (airplane[0] - now_R)*10
                if candidate < min_val:
                    min_val = candidate
    if i in [0,1]:
        rows = [[] for _ in range(200001)]
        for XYU in XYUs:
            rows[XYU[0]].append([XYU[1],XYU[2]])
        for row in rows:
            now_U = -int(1e+10)
            row = sorted(row)
            for airplane in row:
                if airplane[1] == "U":
                    now_U = airplane[0]
                elif airplane[1] == "D":
                    candidate = (airplane[0] - now_U)*5
                    if candidate < min_val:
                        min_val = candidate
if min_val < 3000000:
    print(min_val)
else:
    print("SAFE")

Buggy submission (verdict: Runtime Error):
import copy
f = open("/Users/naoki/Documents/Atcoder/input.txt")

N = int(f.readline())
XYUs = [list(map(str,f.readline().split())) for _ in range(N)]

up_slopes = [[] for _ in range(400005)]
down_slopes = [[] for _ in range(400005)]
xs = [[] for _ in range(200005)]
ys = [[] for _ in range(200005)]



for XYU in XYUs:
    #y切片
    up_slopes[int(XYU[1])-int(XYU[0])+200000].append([int(XYU[0]),XYU[2]])
    down_slopes[int(XYU[1])+int(XYU[0])].append([int(XYU[0]),XYU[2]])
    xs[int(XYU[0])].append([int(XYU[1]),XYU[2]])
    ys[int(XYU[1])].append([int(XYU[0]),XYU[2]])
    
min_val = int(1e+100)
for up_slope in up_slopes:
    U_posi = -1
    R_posi = -1
    incidents = copy.deepcopy(up_slope)
    incidents = sorted(incidents)
    for incident in up_slope:
        if incident[1] == "U":
            U_posi = incident[0]
        elif incident[1] == "R":
            R_posi = incident[0]
        elif incident[1] == "D" and R_posi != -1:
            accident = incident[0]-R_posi
            if min_val > accident:
                min_val = accident
        elif incident[1] == "L" and U_posi != -1:
            accident = incident[0]-U_posi
            if min_val > accident*10:
                min_val = accident*10

for down_slope in down_slopes:
    D_posi = -1
    R_posi = -1
    incidents = copy.deepcopy(down_slope)
    incidents = sorted(incidents)
    for incident in incidents:
        if incident[1] == "D":
            D_posi = incident[0]
        elif incident[1] == "R":
            R_posi = incident[0]
        elif incident[1] == "U" and R_posi != -1:
            accident = incident[0]-R_posi
            if min_val > accident*10:
                min_val = accident*10
        elif incident[1] == "L" and D_posi != -1:
            accident = incident[0]-D_posi
            if min_val > accident:
                min_val = accident


for x in xs:
    U_posi = -1
    incidents = copy.deepcopy(x)
    incidents = sorted(incidents)
    for incident in incidents:
        if incident[1] == "U":
            U_posi = incident[0]
        elif incident[1] == "D" and U_posi!=-1:
            if min_val > 10*(incident[0]-U_posi)/2:
                min_val = 10*(incident[0]-U_posi)/2
            
for y in ys:
    R_posi = -1
    incidents = copy.deepcopy(y)
    incidents = sorted(incidents)
    for incident in incidents:
        if incident[1] == "R":
            R_posi = incident[0]
        elif incident[1] == "L" and R_posi!=-1:
            if min_val > 10*(incident[0]-R_posi)/2:
                min_val = 10*(incident[0]-R_posi)/2
if min_val < 3000000:
    print(int(min_val))
else:
    print("SAFE")

Buggy submission (verdict: Runtime Error):
import copy
f = open("/Users/naoki/Documents/Atcoder/input.txt")

N = int(f.readline())
XYUs = [list(map(str,f.readline().split())) for _ in range(N)]

up_slopes = [[] for _ in range(400005)]
down_slopes = [[] for _ in range(400005)]
xs = [[] for _ in range(200005)]
ys = [[] for _ in range(200005)]



for XYU in XYUs:
    #y切片
    up_slopes[int(XYU[1])-int(XYU[0])+200000].append([int(XYU[0]),XYU[2]])
    down_slopes[int(XYU[1])+int(XYU[0])].append([int(XYU[0]),XYU[2]])
    xs[int(XYU[0])].append([int(XYU[1]),XYU[2]])
    ys[int(XYU[1])].append([int(XYU[0]),XYU[2]])
    
min_val = int(1e+100)
for up_slope in up_slopes:
    U_posi = -1
    R_posi = -1
    incidents = copy.deepcopy(up_slope)
    incidents = sorted(incidents)
    for incident in up_slope:
        if incident[1] == "U":
            U_posi = incident[0]
        elif incident[1] == "R":
            R_posi = incident[0]
        elif incident[1] == "D" and R_posi != -1:
            accident = incident[0]-R_posi
            if min_val > accident:
                min_val = accident
        elif incident[1] == "L" and U_posi != -1:
            accident = incident[0]-U_posi
            if min_val > accident:
                min_val = accident

for down_slope in down_slopes:
    D_posi = -1
    R_posi = -1
    incidents = copy.deepcopy(down_slope)
    incidents = sorted(incidents)
    for incident in incidents:
        if incident[1] == "D":
            D_posi = incident[0]
        elif incident[1] == "R":
            R_posi = incident[0]
        elif incident[1] == "U" and R_posi != -1:
            accident = incident[0]-R_posi
            if min_val > accident:
                min_val = accident
        elif incident[1] == "L" and D_posi != -1:
            accident = incident[0]-D_posi
            if min_val > accident:
                min_val = accident


for x in xs:
    U_posi = -1
    incidents = copy.deepcopy(x)
    incidents = sorted(incidents)
    for incident in incidents:
        if incident[1] == "U":
            U_posi = incident[0]
        elif incident[1] == "D" and U_posi!=-1:
            if min_val > (incident[0]-U_posi)/2:
                min_val = (incident[0]-U_posi)/2
            
for y in ys:
    R_posi = -1
    incidents = copy.deepcopy(y)
    incidents = sorted(incidents)
    for incident in incidents:
        if incident[1] == "R":
            R_posi = incident[0]
        elif incident[1] == "L" and R_posi!=-1:
            if min_val > (incident[0]-R_posi)/2:
                min_val = (incident[0]-R_posi)/2
if min_val < 300000:
    print(int(min_val*10))
else:
    print("SAFE")