Problem p02605 (python)

Fixed submission:
from collections import defaultdict
from bisect import bisect_right

n = int(input())
XYU = [list(input().split()) for _ in range(n)]

dis = 10 ** 19 

# 正面衝突用
box = {"U":defaultdict(list),"D":defaultdict(list),"L":defaultdict(list),"R":defaultdict(list)}

for x, y, u in XYU:
  x = int(x)
  y = int(y)
  if u in ["U","D"]:
    box[u][x].append(y) 
  else:
    box[u][y].append(x)
    
for k1 in box.keys():
  for k2 in box[k1].keys():
    box[k1][k2] = sorted(box[k1][k2])
    
for k, v in box["U"].items():
  if k in box["D"]:
    for pos in v:
      if pos < box["D"][k][-1]:
        a = bisect_right(box["D"][k], pos)
        dis = min(dis, (box["D"][k][a] - pos) * 10 // 2)

for k, v in box["R"].items():
  if k in box["L"]:
    for pos in v:
      if pos < box["L"][k][-1]:  
        a = bisect_right(box["L"][k], pos)
        dis = min(dis, (box["L"][k][a] - pos) * 10 // 2)

# 右肩下がりの同一直線上から発進した機体の衝突用
cox = {"U":defaultdict(list),"D":defaultdict(list),"L":defaultdict(list),"R":defaultdict(list)}

for x, y, u in XYU:
  x = int(x)
  y = int(y)
  cox[u][x+y].append(x) 
    
for k1 in cox.keys():
  for k2 in cox[k1].keys():
    cox[k1][k2] = sorted(cox[k1][k2])
    
for k, v in cox["R"].items():
  if k in cox["U"]:
    for pos in v:
      if pos < cox["U"][k][-1]:
        a = bisect_right(cox["U"][k], pos)
        dis = min(dis, (cox["U"][k][a] - pos) * 10)

for k, v in cox["D"].items():
  if k in cox["L"]:
    for pos in v:
      if pos < cox["L"][k][-1]:  
        a = bisect_right(cox["L"][k], pos)
        dis = min(dis, (cox["L"][k][a] - pos) * 10)

# 右肩上がりの同一直線上から発進した機体の衝突用
dox = {"U":defaultdict(list),"D":defaultdict(list),"L":defaultdict(list),"R":defaultdict(list)}

for x, y, u in XYU:
  x = int(x)
  y = int(y)
  dox[u][x-y].append(x) 
    
for k1 in dox.keys():
  for k2 in dox[k1].keys():
    dox[k1][k2] = sorted(dox[k1][k2])
    
for k, v in dox["U"].items():
  if k in dox["L"]:
    for pos in v:
      if pos < dox["L"][k][-1]:
        a = bisect_right(dox["L"][k], pos)
        dis = min(dis, (dox["L"][k][a] - pos) * 10)

for k, v in dox["R"].items():
  if k in dox["D"]:
    for pos in v:
      if pos < dox["D"][k][-1]:  
        a = bisect_right(dox["D"][k], pos)
        dis = min(dis, (dox["D"][k][a] - pos) * 10)

if dis == 10 ** 19:
  print("SAFE")
else:
  print(dis)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
from bisect import bisect_left

n = int(input())
XYU = [list(input().split()) for _ in range(n)]

dis = 10 ** 19 

# 正面衝突用
box = {"U":defaultdict(list),"D":defaultdict(list),"L":defaultdict(list),"R":defaultdict(list)}

for x, y, u in XYU:
  x = int(x)
  y = int(y)
  if u in ["U","D"]:
    box[u][x].append(y) 
  else:
    box[u][y].append(x)
    
for k1 in box.keys():
  for k2 in box[k1].keys():
    box[k1][k2] = sorted(box[k1][k2])
    
for k, v in box["U"].items():
  if k in box["D"]:
    for pos in v:
      if pos < box["D"][k][0]:
        a = bisect_left(box["D"][k], pos)
        dis = min(dis, box["D"][k][a] - pos) * 10 // 2

for k, v in box["R"].items():
  if k in box["L"]:
    for pos in v:
      if pos < box["L"][k][0]:  
        a = bisect_left(box["L"][k], pos)
        dis = min(dis, box["L"][k][a] - pos) * 10 // 2

# 右肩下がりの同一直線上から発進した機体の衝突用
cox = {"U":defaultdict(list),"D":defaultdict(list),"L":defaultdict(list),"R":defaultdict(list)}

for x, y, u in XYU:
  x = int(x)
  y = int(y)
  cox[u][x+y].append(x) 
    
for k1 in cox.keys():
  for k2 in cox[k1].keys():
    cox[k1][k2] = sorted(cox[k1][k2])
    
for k, v in cox["U"].items():
  if k in cox["R"]:
    for pos in v:
      if pos > cox["R"][k][0]:
        a = bisect_left(cox["R"][k], pos)
        dis = min(dis, cox["R"][k][a] - pos) * 10

for k, v in cox["D"].items():
  if k in cox["L"]:
    for pos in v:
      if pos < cox["L"][k][0]:  
        a = bisect_left(cox["L"][k], pos)
        dis = min(dis, cox["L"][k][a] - pos) * 10

# 右肩上がりの同一直線上から発進した機体の衝突用
dox = {"U":defaultdict(list),"D":defaultdict(list),"L":defaultdict(list),"R":defaultdict(list)}

for x, y, u in XYU:
  x = int(x)
  y = int(y)
  dox[u][x-y].append(x) 
    
for k1 in dox.keys():
  for k2 in dox[k1].keys():
    dox[k1][k2] = sorted(dox[k1][k2])
    
for k, v in dox["U"].items():
  if k in dox["L"]:
    for pos in v:
      if pos < dox["L"][k][0]:
        a = bisect_left(dox["L"][k], pos)
        dis = min(dis, dox["L"][k][a] - pos) * 10

for k, v in dox["D"].items():
  if k in dox["R"]:
    for pos in v:
      if pos > dox["R"][k][0]:  
        a = bisect_left(dox["R"][k], pos)
        dis = min(dis, dox["R"][k][a] - pos) * 10

if dis == 10 ** 19:
  print("SAFE")
else:
  print(dis)