Problem p02605 (python)

Fixed submission:
import sys
input = sys.stdin.readline
from collections import defaultdict
n = int(input())
UR = defaultdict(list)
UD = defaultdict(list)
UL = defaultdict(list)
DR = defaultdict(list)
DL = defaultdict(list)
RL = defaultdict(list)

for i in range(n):
    x,y,u = input().split()
    x,y = int(x), int(y)
    if u == "U":    
        UD[x].append((y,1))
        UR[x+y].append((x-y,0))
        UL[y-x].append((x+y,1))
    elif u == "D":    
        UD[x].append((y,0))
        DR[y-x].append((x+y,0))
        DL[x+y].append((x-y,1))
    elif u == "R":   
        RL[y].append((x,1))
        DR[y-x].append((x+y,1))
        UR[x+y].append((x-y,1))
        
    else:
        RL[y].append((x,0))
        UL[y-x].append((x+y,0))
        DL[x+y].append((x-y,0))

ans = float("INF")

for x in [UD, UL, UR, DR, DL, RL]:
    for v in x.values():
        V = sorted(v)

        for i in range(len(V)-1):
            if V[i][1] and not V[i+1][1]:
                ans = min(ans,V[i+1][0]-V[i][0])


if ans == float("INF"):
    print("SAFE")
else:
    print(ans*5)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
from collections import defaultdict
n = int(input())
UR = defaultdict(list)
UD = defaultdict(list)
UL = defaultdict(list)
DR = defaultdict(list)
DL = defaultdict(list)
RL = defaultdict(list)

for i in range(n):
    x,y,u = input().split()
    x,y = int(x), int(y)
    if u == "U":    
        UD[x].append((y,1))
        UR[x+y].append((x-y,0))
        UL[y-x].append((x+y,1))
    elif u == "D":    
        UD[x].append((y,0))
        DR[y-x].append((x+y,0))
        DL[x+y].append((x-y,1))
    elif u == "R":   
        RL[y].append((x,1))
        DR[y-x].append((x+y,1))
        UR[x+y].append((x-y,1))
        
    else:
        RL[y].append((x,0))
        UL[y-x].append((x+y,0))
        DL[x+y].append((x-y,0))

ans = float("INF")

for x in [UD, UL, UR, DR, DL, RL]:
    for v in x.values():
        V = sorted(v)

        for i in range(len(V)-1):
            if V[i][1] and not V[i+1][1]:
                print(V[i+1],V[i],x)
                ans = min(ans,V[i+1][0]-V[i][0])


if ans == float("INF"):
    print("SAFE")
else:
    print(ans*5)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline


n = int(input())
Rp = []
Rm = []
Lp = []
Lm = []
Up = []
Um = []
Dp = []
Dm = []
U = []
D = []
R = []
L = []

for i in range(n):
    x,y,u = input().split()
    x,y = int(x), int(y)
    if u == "U":
        U.append([x,y])
        Up.append((x+y,x,y))
        Um.append((x-y,x,y))
    elif u == "D":
        D.append([x,y])
        Dp.append((x+y,x,y))
        Dm.append((x-y,x,y))
    elif u == "R":
        R.append([y,x])    
        Rp.append((x+y,x,y))
        Rm.append((x-y,x,y))
    else:
        L.append([y,x])
        Lp.append((x+y,x,y))
        Lm.append((x-y,x,y))

Rp.sort()
Rm.sort()
Up.sort()
Um.sort()
Lp.sort()
Lm.sort()
Dp.sort()
Dm.sort()
U.sort()
D.sort()
R.sort()
L.sort()
ans = float("INF")

def syaku(A,B):
    if A == [] or B == []:
        return float("INF")
    la = len(A)
    lb = len(B)
    count = float("INF")
    b = 0
    for i in range(la):
        xy,x,y = A[i]
        if i > 0 and xy == A[i-1][0]:
            if len(l) > 0:
                for nxy,nx,ny in l:
                    if ny >= y:
                        count = min(count, abs(x-nx)*10)
        else:
            if i < la-1 and A[i+1][0] == xy:
                l = []
                while b < lb and B[b][0] <= xy:
                    if B[b][0] == xy:
                        if B[b][2] >= y:
                            count = min(count,abs(x-B[b][1])*10)
                        l.append(B[b])
                    b += 1
            else:
                while b < lb and B[b][0] <= xy:
                    if B[b][0] == xy:
                        if B[b][2] >= y:
                            count = min(count,abs(x-B[b][1])*10)
                        
                    b += 1
    return count

def syaku2(A,B):
    if A == [] or B == []:
        return float("INF")
    la = len(A)
    lb = len(B)
    count = float("INF")
    b = 0
    for i in range(la):
        xy,x,y = A[i]
        if i > 0 and xy == A[i-1][0]:
            if len(l) > 0:
                for nxy,nx,ny in l:
                    if y >= ny:
                        count = min(count, abs(x-nx)*10)
        else:
            if i < la-1 and A[i+1][0] == xy:
                l = []
                while b < lb and B[b][0] <= xy:
                    if B[b][0] == xy:
                        if y >= B[b][2]:
                            count = min(count,abs(x-B[b][1])*10)
                        l.append(B[b])
                    b += 1
            else:
                while b < lb and B[b][0] <= xy:
                    if B[b][0] == xy:
                        if y >= B[b][2]:
                            count = min(count,abs(x-B[b][1])*10)
                        
                    b += 1
    return count

def syaku3(A,B):
    if A == [] or B == []:
        return float("INF")
    la = len(A)
    lb = len(B)
    count = float("INF")
    b = 0
    for i in range(la):
        x,y = A[i]
        if i > 0 and x == A[i-1][0]:
            if len(l) > 0:
                for nx,ny in l:
                    if y >= ny:
                        count = min(count, abs(y-ny)*5)
        else:
            if i < la-1 and A[i+1][1] == x:
                l = []
                while b < lb and B[b][0] <= x:
                    if B[b][0] == x:
                        if y >= B[b][1]:
                            count = min(count,abs(y-B[b][1])*5)
                        l.append(B[b])
                    b += 1
            else:
                while b < lb and B[b][0] <= x:
                    if B[b][0] == x:
                        if y >= B[b][1]:
                            count = min(count,abs(y-B[b][1])*5)
                    b += 1
    return count

ans = min(ans,syaku(Up,Rp))
ans = min(ans,syaku(Um,Lm))
ans = min(ans,syaku2(Dp,Lp))
ans = min(ans,syaku2(Dm,Rm))
ans = min(ans,syaku3(D,U))
ans = min(ans,syaku3(L,R))

if ans == float("INF"):
    print("SAFE")
else:
    print(ans)