Problem p02605 (python)

Fixed submission:
import sys
input = sys.stdin.readline
n = int(input())
x = [[] for _ in range(200001)]
y = [[] for _ in range(200001)]
z = [[] for _ in range(800002)]
w = [[] for _ in range(800002)]
t = float("inf")
for _ in range(n):
  xi,yi,ui = map(str,input().split())
  xi = int(xi)
  yi = int(yi)
  if ui == "U":
    x[xi].append((yi,1))
    z[xi+yi].append((xi,-1))
    w[xi-yi+200001].append((xi,1))
  elif ui == "D":
    x[xi].append((yi,-1))
    z[xi+yi+400001].append((xi,1))
    w[xi-yi+600001].append((xi,-1))                       
  elif ui == "R":
    y[yi].append((xi,1))
    z[xi+yi].append((xi,1))
    w[xi-yi+600001].append((xi,1))  
  else:
    y[yi].append((xi,-1))
    z[xi+yi+400001].append((xi,-1))
    w[xi-yi+200001].append((xi,-1)) 
for xx in x:
  if len(xx) >= 2:
    xx.sort(key=lambda a:a[0])
    n = -1
    for i in range(len(xx)):
      if xx[i][1] == 1:
        n = xx[i][0]
      else:
        if n != -1:
          if (xx[i][0] - n)*5 < t:
            t = (xx[i][0] - n)*5
          n = -1 
for xx in y:
  if len(xx) >= 2:
    xx.sort(key=lambda a:a[0])
    n = -1
    for i in range(len(xx)):
      if xx[i][1] == 1:
        n = xx[i][0]
      else:
        if n != -1:
          if (xx[i][0] - n)*5 < t:
            t = (xx[i][0] - n)*5
          n = -1 
for xx in z:
  if len(xx) >= 2:
    xx.sort(key=lambda a:a[0])
    n = -1
    for i in range(len(xx)):
      if xx[i][1] == 1:
        n = xx[i][0]
      else:
        if n != -1:
          if (xx[i][0] - n)*10 < t:
            t = (xx[i][0] - n)*10
          n = -1 
for xx in w:
  if len(xx) >= 2:
    xx.sort(key=lambda a:a[0])
    n = -1
    for i in range(len(xx)):
      if xx[i][1] == 1:
        n = xx[i][0]
      else:
        if n != -1:
          if (xx[i][0] - n)*10 < t:
            t = (xx[i][0] - n)*10
          n = -1 
if t == float("inf"):
  print("SAFE")
else:
  print(t)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
n = int(input())
x = [[] for _ in range(200000)]
y = [[] for _ in range(200000)]
z = [[] for _ in range(400000)]
t = float("inf")
for _ in range(n):
  xi,yi,ui = map(str,input().split())
  xi = int(xi)
  yi = int(yi)
  if ui == "U":
    x[xi].append((yi,1))
    z[xi+yi].append((xi,-1))
  elif ui == "D":
    x[xi].append((yi,-1))
    z[xi+yi].append((xi,1))
  elif ui == "R":
    y[yi].append((xi,1))
    z[xi+yi].append((xi,1))
  else:
    y[yi].append((xi,-1))
    z[xi+yi].append((xi,-1))
for xx in x:
  if len(xx) >= 2:
    xx.sort(key=lambda a:a[0])
    state = True
    n = -1
    for xxi in xx:
      if xxi[1] == 1:
        n = xxi[0]
      else:
        if n != -1:
          if (xxi[0] - n)*5 < t:
            t = (xxi[0]-n)*5
          n = -1
for xx in y:
  if len(xx) >= 2:
    xx.sort(key=lambda a:a[0])
    state = True
    n = -1
    for xxi in xx:
      if xxi[1] == 1:
        n = xxi[0]
      else:
        if n != -1:
          if (xxi[0] - n)*5 < t:
            t = (xxi[0] - n)*5
          n = -1
for xx in z:
  if len(xx) >= 2:
    xx.sort(key=lambda a:a[0])
    state = True
    n = -1
    for xxi in xx:
      if xxi[1] == 1:
        n = xxi[0]
      else:
        if n != -1:
          if (xxi[0] - n)*10 < t:
            t = (xxi[0] - n)*10
          n = -1                
if t == float("inf"):
  print("SAFE")
else:
  print(t)