Problem p02605 (python)

Fixed submission:
n = int(input())
b = 2000001
u = []; r = []; d = []; l = []
for i in range(n):
    A = list(input().split())
    a = [int(A[0]), int(A[1])]
    if A[2] == "U":
        u.append(a)
    elif A[2] == "R":
        r.append(a)
    elif A[2] == "D":
        d.append(a)
    else:
        l.append(a)
#ud
u1 = {}; d1 = {}
for i in range(len(u)):
    if u[i][0] not in u1:
        u1[u[i][0]] = []
    u1[u[i][0]].append(u[i][1])
for i in range(len(d)):
    if d[i][0] not in d1:
        d1[d[i][0]] = []
    d1[d[i][0]].append(d[i][1])
for i in range(200001):
    if i in u1 and i in d1:
        u1[i].sort(reverse=True)
        d1[i].sort(reverse=True)
        for j in range(len(u1[i])):
            for k in range(len(d1[i])):
                if u1[i][-1] < d1[i][-1]:
                    b = min(b, (d1[i][-1]-u1[i][-1])*5)
                    break
                else:
                    d1[i].pop()
            u1[i].pop()

#rl
r1 = {}; l1 = {}
for i in range(len(r)):
    if r[i][1] not in r1:
        r1[r[i][1]] = []
    r1[r[i][1]].append(r[i][0])
for i in range(len(l)):
    if l[i][1] not in l1:
        l1[l[i][1]] = []
    l1[l[i][1]].append(l[i][0])
for i in range(200001):
    if i in r1 and i in l1:
        r1[i].sort(reverse=True)
        l1[i].sort(reverse=True)
        for j in range(len(r1[i])):
            for k in range(len(l1[i])):
                if r1[i][-1] < l1[i][-1]:
                    b = min(b, (l1[i][-1]-r1[i][-1])*5)
                    break
                else:
                    l1[i].pop()
            r1[i].pop()
u2 = {}; r2 = {}
for i in range(len(u)):
    if u[i][0]+u[i][1] not in u2:
        u2[u[i][0]+u[i][1]] = []
    u2[u[i][0]+u[i][1]].append(u[i][0])
for i in range(len(r)):
    if r[i][0]+r[i][1] not in r2:
        r2[r[i][0]+r[i][1]] = []
    r2[r[i][0]+r[i][1]].append(r[i][0])
for i in range(400001):
    if i in u2 and i in r2:
        u2[i].sort(reverse=True)
        r2[i].sort(reverse=True)
        for j in range(len(r2[i])):
            for k in range(len(u2[i])):
                if r2[i][-1] < u2[i][-1]:
                    b = min(b, (u2[i][-1]-r2[i][-1])*10)
                    break
                else:
                    u2[i].pop()
            r2[i].pop()
l2 = {}; d2 = {}
for i in range(len(l)):
    if l[i][0]+l[i][1] not in l2:
        l2[l[i][0]+l[i][1]] = []
    l2[l[i][0]+l[i][1]].append(l[i][0])
for i in range(len(d)):
    if d[i][0]+d[i][1] not in d2:
        d2[d[i][0]+d[i][1]] = []
    d2[d[i][0]+d[i][1]].append(d[i][0])
for i in range(400001):
    if i in l2 and i in d2:
        l2[i].sort(reverse=True)
        d2[i].sort(reverse=True)
        for j in range(len(d2[i])):
            for k in range(len(l2[i])):
                if d2[i][-1] < l2[i][-1]:
                    b = min(b, (l2[i][-1]-d2[i][-1])*10)
                    break
                else:
                    l2[i].pop()
            d2[i].pop()
l3 = {}; u3 = {}
for i in range(len(l)):
    if l[i][0]-l[i][1] not in l3:
        l3[l[i][0]-l[i][1]] = []
    l3[l[i][0]-l[i][1]].append(l[i][0])
for i in range(len(u)):
    if u[i][0]-u[i][1] not in u3:
        u3[u[i][0]-u[i][1]] = []
    u3[u[i][0]-u[i][1]].append(u[i][0])
for i in range(-200000, 200001):
    if i in l3 and i in u3:
        l3[i].sort(reverse=True)
        u3[i].sort(reverse=True)
        for j in range(len(u3[i])):
            for k in range(len(l3[i])):
                if u3[i][-1] < l3[i][-1]:
                    b = min(b, (l3[i][-1]-u3[i][-1])*10)
                    break
                else:
                    l3[i].pop()
            u3[i].pop()
d3 = {}; r3 = {}
for i in range(len(d)):
    if d[i][0]-d[i][1] not in d3:
        d3[d[i][0]-d[i][1]] = []
    d3[d[i][0]-d[i][1]].append(d[i][0])
for i in range(len(r)):
    if r[i][0]-r[i][1] not in r3:
        r3[r[i][0]-r[i][1]] = []
    r3[r[i][0]-r[i][1]].append(r[i][0])
for i in range(-200000, 200001):
    if i in d3 and i in r3:
        d3[i].sort(reverse=True)
        r3[i].sort(reverse=True)
        for j in range(len(r3[i])):
            for k in range(len(d3[i])):
                if r3[i][-1] < d3[i][-1]:
                    b = min(b, (d3[i][-1]-r3[i][-1])*10)
                    break
                else:
                    d3[i].pop()
            r3[i].pop()
print("SAFE") if b == 2000001 else print(b)

Buggy submission (verdict: Runtime Error):
n = int(input())
b = 2000001
x = []
u = []
r = []
d = []
l = []
for i in range(n):
    A = list(input().split())
    a = [int(A[0]), int(A[1])]
    if A[2] == "U":
        u.append(a)
    elif A[2] == "R":
        r.append(a)
    elif A[2] == "D":
        d.append(a)
    else:
        l.append(a)
#ud
u1 = {}
d1 = {}
for i in range(len(u)):
    u1[u[i][0]] = u[i][1]
for i in range(len(d)):
    d1[d[i][0]] = d[i][1]
for i in range(200001):
    if i in u1 and i in d1:
        if u1[i] < d1[i]:
            b = min(b, (d1[i]-u1[i])*5)
#rl
r1 = {}
l1 = {}
for i in range(len(r)):
    r1[d[i][1]] = d[i][0]
for i in range(len(l)):
    l1[u[i][1]] = u[i][0]
for i in range(200001):
    if i in r1 and i in l1:
        if r1[i] < l1[i]:
            b = min(b, (l1[i]-r1[i])*5)
#ur
u2 = {}
r2 = {}
for i in range(len(u)):
    u2[u[i][0]+u[i][1]] = u[i][0]
for i in range(len(r)):
    r2[r[i][0]+r[i][1]] = r[i][0]
for i in range(400001):
    if i in u2 and i in r2:
        if r2[i] < u2[i]:
            b = min(b, (u2[i]-r2[i])*10)
#ld
l2 = {}
d2 = {}
for i in range(len(l)):
    l2[l[i][0]+l[i][1]] = l[i][0]
for i in range(len(d)):
    d2[d[i][0]+d[i][1]] = d[i][0]
for i in range(400001):
    if i in l2 and i in d2:
        if d2[i] < l2[i]:
            b = min(b, (l2[i]-d2[i])*10)
#lu
l3 = {}
u3 = {}
for i in range(len(l)):
    l3[l[i][0]-l[i][1]] = l[i][0]
for i in range(len(u)):
    l3[u[i][0]-u[i][1]] = u[i][0]
for i in range(-200000, 2000001):
    if i in l3 and i in u3:
        if u3[i] < l3[i]:
            b = min(b, (l3[i]-u3[i])*10)
#dr
d3 = {}
r3 = {}
for i in range(len(d)):
    d3[d[i][0]-d[i][1]] = d[i][0]
for i in range(len(r)):
    r3[r[i][0]-r[i][1]] = r[i][0]
for i in range(-200000, 2000001):
    if i in d3 and i in r3:
        if r3[i] < d3[i]:
            b = min(b, (d3[i]-r3[i])*10)
if b == 2000001:
    print("SAFE")
else:
    print(b)

Buggy submission (verdict: Runtime Error):
n = int(input())
b = 2000001
x = []
u = []
r = []
d = []
l = []
for i in range(n):
    A = list(input().split())
    a = [int(A[0]), int(A[1])]
    if A[2] == "U":
        u.append(a)
    elif A[2] == "R":
        r.append(a)
    elif A[2] == "D":
        d.append(a)
    else:
        l.append(a)
#ud
u1 = {}
d1 = {}
for i in range(len(u)):
    u1[u[i][0]] = u[i][1]
for i in range(len(d)):
    d1[d[i][0]] = d[i][1]
for i in range(200001):
    if i in u1 and i in d1:
        if u1[i] < d1[i]:
            b = min(b, (d1[i]-u1[i])*5)
#rl
r1 = {}
l1 = {}
for i in range(len(r)):
    r1[d[i][1]] = d[i][0]
for i in range(len(l)):
    l1[u[i][1]] = u[i][0]
for i in range(200001):
    if i in r1 and i in l1:
        if r1[i] < l1[i]:
            b = min(b, (l1[i]-r1[i])*5)
#ur
u2 = {}
r2 = {}
for i in range(len(u)):
    u2[u[i][0]+u[i][1]] = u[i][0]
for i in range(len(r)):
    r2[r[i][0]+r[i][1]] = r[i][0]
for i in range(400001):
    if i in u2 and i in r2:
        if r2[i] < u2[i]:
            b = min(b, (u2[i]-r2[i])*10)
#ld
l2 = {}
d2 = {}
for i in range(len(l)):
    l2[l[i][0]+l[i][1]] = l[i][0]
for i in range(len(d)):
    d2[d[i][0]+d[i][1]] = d[i][0]
for i in range(400001):
    if i in l2 and i in d2:
        if d2[i] < l2[i]:
            b = min(b, (l2[i]-d2[i])*10)
#lu
l3 = {}
u3 = {}
for i in range(len(l)):
    l3[l[i][0]-l[i][1]] = l[i][0]
for i in range(len(u)):
    l3[u[i][0]-u[i][1]] = u[i][0]
for i in range(400001):
    if i in l3 and i in u3:
        if u3[i] < l3[i]:
            b = min(b, (l3[i]-u3[i])*10)
#dr
d3 = {}
r3 = {}
for i in range(len(d)):
    d3[d[i][0]-d[i][1]] = d[i][0]
for i in range(len(r)):
    r3[r[i][0]-r[i][1]] = r[i][0]
for i in range(400001):
    if i in d3 and i in r3:
        if r3[i] < d3[i]:
            b = min(b, (d3[i]-r3[i])*10)
if b == 2000001:
    print("SAFE")
else:
    print(b)