Problem:


Score: 600 points

Problem Statement

M-kun is a brilliant air traffic controller.

On the display of his radar, there are N airplanes numbered 1, 2, ..., N, all flying at the same altitude.

Each of the airplanes flies at a constant speed of 0.1 per second in a constant direction. The current coordinates of the airplane numbered i are (X_i, Y_i), and the direction of the airplane is as follows:

if U_i is U, it flies in the positive y direction;

if U_i is R, it flies in the positive x direction;

if U_i is D, it flies in the negative y direction;

if U_i is L, it flies in the negative x direction.

To help M-kun in his work, determine whether there is a pair of airplanes that will collide with each other if they keep flying as they are now.

If there is such a pair, find the number of seconds after which the first collision will happen.

We assume that the airplanes are negligibly small so that two airplanes only collide when they reach the same coordinates simultaneously.

Constraints

1 \leq N \leq 200000

0 \leq X_i, Y_i \leq 200000

U_i is U, R, D, or L.

The current positions of the N airplanes, (X_i, Y_i), are all distinct.

N, X_i, and Y_i are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1 U_1
X_2 Y_2 U_2
X_3 Y_3 U_3
:
X_N Y_N U_N

Output

If there is a pair of airplanes that will collide with each other if they keep flying as they are now, print an integer representing the number of seconds after which the first collision will happen.

If there is no such pair, print SAFE.

Sample Input 1

2
11 1 U
11 47 D

Sample Output 1

230

If the airplanes keep flying as they are now, two airplanes numbered 1 and 2 will reach the coordinates (11, 24) simultaneously and collide.  

Sample Input 2

4
20 30 U
30 20 R
20 10 D
10 20 L

Sample Output 2

SAFE

No pair of airplanes will collide.

Sample Input 3

8
168 224 U
130 175 R
111 198 D
121 188 L
201 116 U
112 121 R
145 239 D
185 107 L

Sample Output 3

100



Sample input:
2
11 1 U
11 47 D


Sample output:
230

Submission status: Runtime Error

Solution code:
N = int(input())

Xaxis = [[] for _ in range(200001)]
Yaxis = [[] for _ in range(200001)]
sum = [[] for _ in range(400001)]
dif = [[] for _ in range(400001)]
value = []
ans = 10**8

for i in range(N):
    X,Y,P = input().split()
    X,Y = int(X),int(Y)
    value.append([X,Y,P])
    Xaxis[X].append(i)
    Yaxis[Y].append(i)
    sum[X+Y].append(i)
    dif[X-Y].append(i)

for i in Xaxis:
    l = len(i)
    if l >= 2:
        for j in range(l-1):
            for k in range(j+1,l):
                if (value[i[j]][2] == 'D' and value[i[k]][2] == 'U'):
                    if value[i[j]][1] >= value[i[k]][1]:
                        ans = min((value[i[j]][1]-value[i[k]][1])*5)
                elif (value[i[j]][2] == 'U' and value[i[k]][2] == 'D'):
                    if value[i[j]][1] <= value[i[k]][1]:
                        ans = min((value[i[k]][1]-value[i[j]][1])*5)

for i in Yaxis:
    l = len(i)
    if l >= 2:
        for j in range(l-1):
            for k in range(j+1,l):
                if (value[i[j]][2] == 'L' and value[i[k]][2] == 'R'):
                    if value[i[j]][0] >= value[i[k]][0]:
                        ans = min((value[i[j]][0]-value[i[k]][0])*5)
                elif (value[i[j]][2] == 'R' and value[i[k]][2] == 'L'):
                    if value[i[j]][0] <= value[i[k]][0]:
                        ans = min((value[i[k]][0]-value[i[j]][0])*5)

for i in sum:
    l = len(i)
    if l >= 2:
        for j in range(l-1):
            for k in range(j+1,l):
                if  (value[i[j]][2] == 'L' and value[i[k]][2] == 'D') or (value[i[j]][2] == 'U' and value[i[k]][2] == 'R'):
                    if value[i[j]][0] >= value[i[k]][0]:
                        ans = min((value[i[j]][0]-value[i[k]][0])*10)
                elif (value[i[j]][2] == 'D' and value[i[k]][2] == 'L') or (value[i[j]][2] == 'R' and value[i[k]][2] == 'U'):
                    if value[i[j]][0] <= value[i[k]][0]:
                        ans = min((value[i[k]][0]-value[i[j]][0])*10)

for i in dif:
    l = len(i)
    if l >= 2:
        for j in range(l-1):
            for k in range(j+1,l):
                if (value[i[j]][2] == 'L' and value[i[k]][2] == 'U') or (value[i[j]][2] == 'D' and value[i[k]][2] == 'R'):
                    if value[i[j]][0] >= value[i[k]][0]:
                        ans = min((value[i[j]][0]-value[i[k]][0])*10)
                elif (value[i[j]][2] == 'U' and value[i[k]][2] == 'L') or (value[i[j]][2] == 'R' and value[i[k]][2] == 'D'):
                    if value[i[j]][0] <= value[i[k]][0]:
                        ans = min((value[i[k]][0]-value[i[j]][0])*10)

if ans == 10**8:
    print('SAFE')
else:
    print(ans)

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
XYU = []
UR = {}
URs = set()
UL = {}
ULs = set()
DR = {}
DRs = set()
DL = {}
DLs = set()
LR = {}
LRs = set()
UD = {}
UDs = set()
for _ in range(N):
    x, y, u = input().split()
    x, y = int(x), int(y)
    XYU.append((x, y, u))
XYU.sort()
for x, y, u in XYU:
    if u=='U':
        if x+y in URs:
            UR[(x+y)].append((x, y))
        else:
            UR[x+y] = [(x, y)]
            URs.add(x+y)
        if x-y in ULs:
            UL[(x-y)].append((x, y))
        else:
            UL[x-y] = [(x-y)]
            ULs.add(x-y)
        if x in UDs:
            UD[x].append((x, y))
        else:
            UD[x] = [(x, y)]
            UDs.add(x)
    if u=='R':
        if y in LRs:
            LR[y].append((x, y))
        else:
            LRs.add(y)
            LR[y] = [(x, y)]
    if u=='D':
        if x+y in DLs:
            DL[(x+y)].append((x, y))
        else:
            DL[x+y] = [(x, y)]
            DLs.add(x+y)
        if x-y in DRs:
            DR[(x-y)].append((x, y))
        else:
            DR[(x-y)] = [(x, y)]
            DRs.add(x-y)

ans = 10**18
def check(x, y, u):
    global ans
    if u=='R':
        if x+y in URs:
            for px, py in UR[(x+y)]:
                if x<px and y>py:
                    ans = min(ans, px-x)
        
        if x-y in DRs:      
            for px, py in DR[(x-y)]:
                if x<px and py>y:
                    ans = min(ans, px-x)
                
    if u=='L':
        if x-y in ULs:
            for px, py in UL[(x-y)]:
                if px>x: break
                if x>px and y>py:
                    ans = min(ans, x-px)
            
        if x+y in DLs:    
            for px, py in DL[(x+y)]:
                if px>x: break
                if x>px and py>y:
                    ans = min(ans, x-px) 
        
        if y in LRs:      
            for px, py in LR[y]:
                if px>x: break
                if x>px:
                    ans = min(ans, (x-px)/2)
                
    if u=='D':
        if x in UDs:
            for px, py in UD[x]:
                if py<y:
                    ans = min(ans, (y-py)/2)

for x, y, u in XYU:
    check(x, y, u)

if ans>=10**16:
    print('SAFE')
else:
    print(int(ans*10))

Submission status: Runtime Error

Solution code:
N=int(input())
C=[]
d=[]
for i in range(N):
  X,Y,U=input().split()
  for j in range(len(C)):
    if (int(X)==C[j][0] or int(Y)==C[j][1] or int(X)+int(Y)==C[j][0]+C[j][1])and ((U=="U" and C[j][2]=="D")or(U=="D" and C[j][2]=="U")or(U=="R" and C[j][2]=="L")or(U=="L"and C[j][2]=="R")):
      d.append(max(abs(int(X)-C[j][0]),abs(int(Y)-C[j][1])))
  C.append([int(X),int(Y),U])
t=min(d)
print(int(t*5))

Submission status: Runtime Error

Solution code:
N = int(input())
X = [0]*N
Y = [0]*N
U = [0]*N
const = 400010
const2 = 200010
S_U = [[] for i in range(const)]
S_R = [[] for i in range(const)]
S_D = [[] for i in range(const)]
S_L = [[] for i in range(const)]

D_U = [[] for i in range(const)]
D_R = [[] for i in range(const)]
D_D = [[] for i in range(const)]
D_L = [[] for i in range(const)]

UD_U = [[] for i in range(const2)]
UD_D = [[] for i in range(const2)]
LR_L = [[] for i in range(const2)]
LR_R = [[] for i in range(const2)]

for i in range(N):
    X[i],Y[i],U[i] = input().split()
    X[i] = int(X[i])
    Y[i] = int(Y[i])
    if U[i]=="U":
        S_U[X[i]+Y[i]].append(X[i])
        D_U[X[i]-Y[i]+const2].append(X[i])
        UD_U[X[i]].append(Y[i])
    elif U[i]=="R":
        S_R[X[i]+Y[i]].append(X[i])
        D_R[X[i]-Y[i]+const2].append(X[i])
        LR_R[Y[i]].append(X[i])
    elif U[i]=="D":
        S_D[X[i]+Y[i]].append(X[i])
        D_D[X[i]-Y[i]+const2].append(X[i])
        UD_D[X[i]].append(Y[i])
    else:
        S_L[X[i]+Y[i]].append(X[i])
        D_L[X[i]-Y[i]+const2].append(X[i])
        LR_L[Y[i]].append(X[i])
        
        
import itertools

safe = 0
time = 1000000
for i in range(const2):
    if UD_U[i] != [] and UD_D[i] != []:

        for temp in list(itertools.product(UD_U[i], UD_D[i])):
            if temp[1]-temp[0]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1])//2,time)

    if LR_L[i] != [] and LR_R[i] != []:

        for temp in list(itertools.product(LR_L[i], LR_R[i])):
            if temp[0]-temp[1]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1])//2,time)

for i in range(const):
    if S_U[i] != [] and S_R[i] != []:

        for temp in list(itertools.product(S_U[i], S_R[i])):
            if temp[0]-temp[1]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1]),time)
            
    if S_D[i] != [] and S_L[i] != []:

        for temp in list(itertools.product(S_D[i], S_L[i])):
            if temp[1]-temp[0]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1]),time)
            
    if D_U[i] != [] and D_L[i] != []:

        for temp in list(itertools.product(D_U[i], D_L[i])):
            if temp[1]-temp[0]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1]),time)
            
    if D_D[i] != [] and S_R[i] != []:

        for temp in list(itertools.product(D_D[i], S_R[i])):
            if temp[0]-temp[1]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1]),time)

if safe == 0:
    print("SAFE")
else:
    print(time*10)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
from bisect import bisect,bisect_left
N = int(input())
XYU = [input().split() for _ in range(N)]
for i in range(N):
    XYU[i][0] = int(XYU[i][0])
    XYU[i][1] = int(XYU[i][1])
dp = defaultdict(lambda :defaultdict(list))
dn = defaultdict(lambda :defaultdict(list))
d1 = defaultdict(lambda :defaultdict(list))
d2 = defaultdict(lambda :defaultdict(list))

for i in range(N):
    x,y,u = XYU[i]
    dp[u][x+y].append(x)
    dn[u][x-y].append(x)
    d1[u][x].append(y)
    d2[u][y].append(x)

for u in ['U','R','D','L']:
    for z in dp[u]:
        dp[u][z].sort()
    for z in dn[u]:
        dn[u][z].sort()
    for z in d1[u]:
        d1[u][z].sort()
    for z in d2[u]:
        d2[u][z].sort()

ans = float('inf')
for i in range(N):
    x,y,u = XYU[i]
    if u=='U':
        j = bisect_left(dp['R'][x+y],x)-1
        if j>=0:
            ans = min(ans,10*(x-dp['R'][x+y][j]))
        j = bisect(dn['L'][x-y],x)
        if j<len(dn['L'][x-y]):
            ans = min(ans,10*(dn['L'][x-y][j]-x))
        j = bisect(d1['D'][x],y)
        if j<len(d1['D'][x]):
            ans = min(ans,5*(d1['D'][x][j]-y))
    elif u=='D':
        j = bisect_left(dn['R'][x-y],x)-1
        if j>=0:
            ans = min(ans,10*(x-dn['R'][x-y][j]))
        j = bisect(dp['L'][x+y],x)
        if j<len(dp['L'][x+y]):
            ans = min(ans,10*(dp['L'][x+y][j]-x))
        j = bisect_left(d1['U'][x],y)-1
        if j>=0:
            ans = min(ans,5*(y-d1['U'][x][j]))
    elif u=='R':
        j = bisect(dp['U'][x+y],x)
        if j<len(dp['U'][x+y]):
            ans = min(ans,10*(dp['U'][x+y][j]-x))
        j = bisect(dn['D'][x-y],x)
        if j<len(dn['D'][x-y]):
            ans = min(ans,10*(dn['D'][x-y][j]-x))
        j = bisect(d2['L'][y],x)
        if j<len(d2['L'][y]):
            ans = min(ans,5*(d2['L'][y][j]-x))
    elif u=='L':
        j = bisect_left(dp['D'][x+y],x)-1
        if j>=0:
            ans = min(ans,10*(x-dp['D'][x+y][j]))
        j = bisect_left(dn['U'][x-y],x)-1
        if j>=0:
            ans = min(ans,10(x-dn['U'][x-y][j]))
        j = bisect_left(d2['R'][y],x)-1
        if j>=0:
            ans = min(ans,5*(x-d2['R'][y][j]))

if ans==float('inf'):
    ans = 'SAFE'
    print(ans)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n = k()
plane = deque([[] for i in range(2000001)])
num = []

for i in range(n):
    a,b,c=map(str,input().split())
    b = int(b)
    if c == "U" or c =- "R":
        b = -b
    plane[0][int(a)].append(b)

for i in range(2000001):
    x = plane[i].popleft()
    if len(x) > 1:

Submission status: Runtime Error

Solution code:
from collections import defaultdict
from bisect import bisect,bisect_left
N = int(input())
XYU = [input().split() for _ in range(N)]
for i in range(N):
    XYU[i][0] = int(XYU[i][0])
    XYU[i][1] = int(XYU[i][1])
dp = defaultdict(lambda :defaultdict(list))
dn = defaultdict(lambda :defaultdict(list))
d1 = defaultdict(lambda :defaultdict(list))
d2 = defaultdict(lambda :defaultdict(list))

for i in range(N):
    x,y,u = XYU[i]
    dp[u][x+y].append(x)
    dn[u][x-y].append(x)
    d1[u][x].append(y)
    d2[u][y].append(x)

for u in ['U','R','D','L']:
    for z in dp[u]:
        dp[u][z].sort()
    for z in dn[u]:
        dn[u][z].sort()
    for z in d1[u]:
        d1[u][z].sort()
    for z in d2[u]:
        d2[u][z].sort()

ans = float('inf')
for i in range(N):
    x,y,u = XYU[i]
    if u=='U':
        j = bisect_left(dp['R'][x+y],x)-1
        if j>=0:
            ans = min(ans,10*(x-dp['R'][x+y][j]))
        j = bisect(dn['L'][x-y],x)
        if j<len(dn['L'][x-y]):
            ans = min(ans,10*(dn['L'][x-y][j]-x))
        j = bisect(d1['D'][x],y)
        if j<len(d1['D'][x]):
            ans = min(ans,5*(d1['D'][x][j]-y))
    elif u=='D':
        j = bisect_left(dn['R'][x-y],x)-1
        if j>=0:
            ans = min(ans,10*(x-dn['R'][x-y][j]))
        j = bisect(dp['L'][x+y],x)
        if j<len(dp['L'][x+y]):
            ans = min(ans,10*(dp['L'][x+y][j]-x))
        j = bisect_left(d1['U'][x],y)-1
        if j>=0:
            ans = min(ans,5*(y-d1['U'][x][j]))
    elif u=='R':
        j = bisect(dp['U'][x+y],x)
        if j<len(dp['U'][x+y]):
            ans = min(ans,10*(dp['U'][x+y][j]-x))
        j = bisect(dn['D'][x-y],x)
        if j<len(dn['D'][x-y]):
            ans = min(ans,10*(dn['D'][x-y][j]-x))
        j = bisect(d2['L'][y],x)
        if j<len(d2['L'][y]):
            ans = min(ans,5*(d2['L'][y][j]-x))
    elif u=='L':
        j = bisect_left(dp['D'][x+y],x)-1
        if j>=0:
            ans = min(ans,10*(x-dp['D'][x+y][j]))
        j = bisect_left(dn['U'][x-y],x)-1
        if j>=0:
            ans = min(ans,10(x-dn['U'][x-y][j]))
        j = bisect_left(d2['R'][y],x)-1
        if j>=0:
            ans = min(ans,5*(x-d2['R'][y][j]))

if ans==float('inf'):
    ans = 'SAFE'
    print(ans)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
from bisect import bisect,bisect_left
N = int(input())
XYU = [input().split() for _ in range(N)]
for i in range(N):
    XYU[i][0] = int(XYU[i][0])
    XYU[i][1] = int(XYU[i][1])
dp = defaultdict(lambda :defaultdict(list))
dn = defaultdict(lambda :defaultdict(list))
d1 = defaultdict(lambda :defaultdict(list))
d2 = defaultdict(lambda :defaultdict(list))

for i in range(N):
    x,y,u = XYU[i]
    dp[u][x+y].append(x)
    dn[u][x-y].append(x)
    d1[u][x].append(y)
    d2[u][y].append(x)

for u in ['U','R','D','L']:
    for z in dp[u]:
        dp[u][z].sort()
    for z in dn[u]:
        dn[u][z].sort()
    for z in d1[u]:
        d1[u][z].sort()
    for z in d2[u]:
        d2[u][z].sort()

ans = float('inf')
for i in range(N):
    x,y,u = XYU[i]
    if u=='U':
        j = bisect_left(dp['R'][x+y],x)-1
        if j>=0:
            ans = min(ans,10*(x-dp['R'][x+y][j]))
        j = bisect(dn['L'][x-y],x)
        if j<len(dn['L'][x-y]):
            ans = min(ans,10*(dn['L'][x-y][j]-x))
        j = bisect(d1['D'][x],y)
        if j<len(d1['D'][x]):
            ans = min(ans,5*(d1['D'][x][j]-y))
    elif u=='D':
        j = bisect_left(dn['R'][x-y],x)-1
        if j>=0:
            ans = min(ans,10*(x-dn['R'][x-y][j]))
        j = bisect(dp['L'][x+y],x)
        if j<len(dp['L'][x+y]):
            ans = min(ans,10*(dp['L'][x+y][j]-x))
        j = bisect_left(d1['U'][x],y)-1
        if j>=0:
            ans = min(ans,5*(y-d1['U'][x][j]))
    elif u=='R':
        j = bisect(dp['U'][x+y],x)
        if j<len(dp['U'][x+y]):
            ans = min(ans,10*(dp['U'][x+y][j]-x))
        j = bisect(dn['D'][x-y],x)
        if j<len(dn['D'][x-y]):
            ans = min(ans,10*(dn['D'][x-y][j]-x))
        j = bisect(d2['L'][y],x)
        if j<len(d2['L'][y]):
            ans = min(ans,5*(d2['L'][y][j]-x))
    elif u=='L':
        j = bisect_left(dp['D'][x+y],x)-1
        if j>=0:
            ans = min(ans,10*(x-dp['D'][x+y][j]))
        j = bisect_left(dn['U'][x-y],x)-1
        if j>=0:
            ans = min(ans,10(x-dn['U'][x-y][j]))
        j = bisect_left(d2[y]['R'],x)-1
        if j>=0:
            ans = min(ans,5*(x-d2[y]['R'][j]))

if ans==float('inf'):
    ans = 'SAFE'
    print(ans)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
Lu = []
Lr = []
Ld = []
Ll = []

a = 400000

for i in range(n):
    t = list(map(str,input().split()))
    if t[2] == 'U':
        Lu.append(t)
    elif t[2] == 'R':
        Lr.append(t)
    elif t[2] == 'D':
        Ld.append(t)
    elif t[2] == 'L':
        Ll.append(t)
#print(Lu,Lr,Ld,Ll)

def check(L1,L2,a):
    if len(L1) > 0 and len(L2) > 0: 
        if (L1[0][2] == 'U' or L1[0][2] == 'D') and (L2[0][2] == 'U' or L2[0][2] == 'D'):
            for i in range(len(L1)):
                for j in range(len(L2)):
                    if L1[i][0] == L2[i][0]:
                        if (L1[i][1] < L2[i][1] and L1[i][2] == 'U') or (L1[i][1] > L2[i][1] and L1[i][2] == 'D'):
                            t = abs(int(L1[i][1])-int(L2[i][1]))//2
                            if a > t:
                                a = t
        elif (L1[0][2] == 'L' or L1[0][2] == 'R') and (L2[0][2] == 'L' or L2[0][2] == 'R'):
            for i in range(len(L1)):
                for j in range(len(L2)):
                    if L1[i][1] == L2[i][1]:
                        if (L1[i][0] < L2[i][0] and L1[i][2] == 'R') or (L1[i][0] > L2[i][0] and L1[i][2] == 'L'):
                            t = abs(int(L1[i][0])-int(L2[i][0]))//2
                            if a > t:
                                a = t
        else:
            for i in range(len(L1)):
                for j in range(len(L2)):
                    if abs(int(L1[i][0])-int(L2[i][0])) == abs(int(L1[i][1])-int(L2[i][1])):
                        t = abs(int(L1[i][0])-int(L2[i][0]))
    
                        if L1[i][2] == 'U' and ((L2[i][2] == 'R' and L2[i][0] < L1[i][0]) or (L2[i][2] == 'L' and L2[i][0] > L1[i][0])):
                            if a > t:
                                a = t
                        elif L1[i][2] == 'D' and ((L2[i][2] == 'R' and L2[i][0] < L1[i][0]) or (L2[i][2] == 'L' and L2[i][0] > L1[i][0])):
                            if a > t:
                                a = t
                        elif L1[i][2] == 'R' and ((L2[i][2] == 'U' and L2[i][1] < L1[i][1]) or (L2[i][2] == 'D' and L2[i][1] > L1[i][1])):
                            if a > t:
                                a = t
                        elif L1[i][2] == 'L' and ((L2[i][2] == 'U' and L2[i][1] > L1[i][1]) or (L2[i][2] == 'D' and L2[i][1] < L1[i][1])):
                            if a > t:
                                a = t
    return a


a = check(Lu,Lr,a)
a = check(Lu,Ld,a)
a = check(Lu,Ll,a)
a = check(Lr,Ld,a)
a = check(Lr,Ll,a)
a = check(Ld,Ll,a)

if a < 400000:
    print(a*10)
else:
    print('SAFE')

Submission status: Runtime Error

Solution code:
N = int(input())
X = [0]*N
Y = [0]*N
U = [0]*N
const = 400001
const2 = 200001
S_U = [[] for i in range(const)]
S_R = [[] for i in range(const)]
S_D = [[] for i in range(const)]
S_L = [[] for i in range(const)]

D_U = [[] for i in range(const)]
D_R = [[] for i in range(const)]
D_D = [[] for i in range(const)]
D_L = [[] for i in range(const)]

UD_U = [[] for i in range(const2)]
UD_D = [[] for i in range(const2)]
LR_L = [[] for i in range(const2)]
LR_R = [[] for i in range(const2)]

for i in range(N):
    X[i],Y[i],U[i] = input().split()
    X[i] = int(X[i])
    Y[i] = int(Y[i])
    if U[i]=="U":
        S_U[X[i]+Y[i]].append(X[i])
        D_U[X[i]-Y[i]+const2].append(X[i])
        UD_U[X[i]].append(Y[i])
    elif U[i]=="R":
        S_R[X[i]+Y[i]].append(X[i])
        D_R[X[i]-Y[i]+const2].append(X[i])
        LR_R[Y[i]].append(X[i])
    elif U[i]=="D":
        S_D[X[i]+Y[i]].append(X[i])
        D_D[X[i]-Y[i]+const2].append(X[i])
        UD_D[X[i]].append(Y[i])
    else:
        S_L[X[i]+Y[i]].append(X[i])
        D_L[X[i]-Y[i]+const2].append(X[i])
        LR_L[Y[i]].append(X[i])
        
        
import itertools

safe = 0
time = 1000000
for i in range(const2):
    if UD_U[i] != [] and UD_D[i] != []:

        for temp in list(itertools.product(UD_U[i], UD_D[i])):
            if temp[1]-temp[0]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1])//2,time)

    if LR_L[i] != [] and LR_R[i] != []:

        for temp in list(itertools.product(LR_L[i], LR_R[i])):
            if temp[0]-temp[1]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1])//2,time)

for i in range(const):
    if S_U[i] != [] and S_R[i] != []:

        for temp in list(itertools.product(S_U[i], S_R[i])):
            if temp[0]-temp[1]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1]),time)
            
    if S_D[i] != [] and S_L[i] != []:

        for temp in list(itertools.product(S_D[i], S_L[i])):
            if temp[1]-temp[0]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1]),time)
            
    if D_U[i] != [] and D_L[i] != []:

        for temp in list(itertools.product(D_U[i], D_L[i])):
            if temp[1]-temp[0]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1]),time)
            
    if D_D[i] != [] and S_R[i] != []:

        for temp in list(itertools.product(D_D[i], S_R[i])):
            if temp[0]-temp[1]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1]),time)

if safe == 0:
    print("SAFE")
else:
    print(time*10)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
from bisect import bisect,bisect_left
N = int(input())
XYU = [input().split() for _ in range(N)]
for i in range(N):
    XYU[i][0] = int(XYU[i][0])
    XYU[i][1] = int(XYU[i][1])
dp = defaultdict(lambda :defaultdict(list))
dn = defaultdict(lambda :defaultdict(list))
d1 = defaultdict(lambda :defaultdict(list))
d2 = defaultdict(lambda :defaultdict(list))

for i in range(N):
    x,y,u = XYU[i]
    dp[u][x+y].append(x)
    dn[u][x-y].append(x)
    d1[u][x].append(y)
    d2[u][y].append(x)

for u in ['U','R','D','L']:
    for z in dp[u]:
        dp[u][z].sort()
    for z in dn[u]:
        dn[u][z].sort()
    for z in d1[u]:
        d1[u][z].sort()
    for z in d2[u]:
        d2[u][z].sort()

ans = float('inf')
for i in range(N):
    x,y,u = XYU[i]
    if u=='U':
        j = bisect_left(dp['R'][x+y],x)-1
        if j>=0:
            ans = min(ans,10*(x-dp['R'][x+y][j]))
        j = bisect(dn['L'][x-y],x)
        if j<len(dn['L'][x-y]):
            ans = min(ans,10*(dn['L'][x-y][j]-x))
        j = bisect(d1['D'][x],y)
        if j<len(d1['D'][x]):
            ans = min(ans,5*(d1['D'][x][j]-y))

    elif u=='D':
        j = bisect_left(dn['R'][x-y],x)-1
        if j>=0:
            ans = min(ans,10*(x-dn['R'][x-y][j]))
        j = bisect(dp['L'][x+y],x)
        if j<len(dp['L'][x+y]):
            ans = min(ans,10*(dp['L'][x+y][j]-x))
        j = bisect_left(d1['U'][x],y)-1
        if j>=0:
            ans = min(ans,5*(y-d1['U'][x][j]))
    elif u=='R':
        j = bisect(dp['U'][x+y],x)
        if j<len(dp['U'][x+y]):
            ans = min(ans,10*(dp['U'][x+y][j]-x))
        j = bisect(dn['D'][x-y],x)
        if j<len(dn['D'][x-y]):
            ans = min(ans,10*(dn['D'][x-y][j]-x))
        j = bisect(d2['L'][y],x)
        if j<len(d2['L'][y]):
            ans = min(ans,5*(d2['L'][y][j]-x))
            
    elif u=='L':
        j = bisect_left(dp['D'][x+y],x)-1
        if j>=0:
            ans = min(ans,10*(x-dp['D'][x+y][j]))
        j = bisect_left(dn['U'][x-y],x)-1
        if j>=0:
            ans = min(ans,10(x-dp['U'][x-y][j]))
        j = bisect_left(d2[y]['R'],x)-1
        if j>=0:
            ans = min(ans,5*(x-d2[y]['R'][j]))

if ans==float('inf'):
    ans = 'SAFE'
    print(ans)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
X = [0]*N
Y = [0]*N
U = [0]*N
const = 400001
const2 = 200000
S_U = [[] for i in range(const)]
S_R = [[] for i in range(const)]
S_D = [[] for i in range(const)]
S_L = [[] for i in range(const)]

D_U = [[] for i in range(const)]
D_R = [[] for i in range(const)]
D_D = [[] for i in range(const)]
D_L = [[] for i in range(const)]

UD_U = [[] for i in range(const2)]
UD_D = [[] for i in range(const2)]
LR_L = [[] for i in range(const2)]
LR_R = [[] for i in range(const2)]

for i in range(N):
    X[i],Y[i],U[i] = input().split()
    X[i] = int(X[i])
    Y[i] = int(Y[i])
    if U[i]=="U":
        S_U[X[i]+Y[i]].append(X[i])
        D_U[X[i]-Y[i]+const2].append(X[i])
        UD_U[X[i]].append(Y[i])
    elif U[i]=="R":
        S_R[X[i]+Y[i]].append(X[i])
        D_R[X[i]-Y[i]+const2].append(X[i])
        LR_R[Y[i]].append(X[i])
    elif U[i]=="D":
        S_D[X[i]+Y[i]].append(X[i])
        D_D[X[i]-Y[i]+const2].append(X[i])
        UD_D[X[i]].append(Y[i])
    else:
        S_L[X[i]+Y[i]].append(X[i])
        D_L[X[i]-Y[i]+const2].append(X[i])
        LR_L[Y[i]].append(X[i])
        
        
import itertools

safe = 0
time = 1000000
for i in range(const2):
    if UD_U[i] != [] and UD_D[i] != []:

        for temp in list(itertools.product(UD_U[i], UD_D[i])):
            if temp[1]-temp[0]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1])//2,time)

    if LR_L[i] != [] and LR_R[i] != []:

        for temp in list(itertools.product(LR_L[i], LR_R[i])):
            if temp[0]-temp[1]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1])//2,time)

for i in range(const):
    if S_U[i] != [] and S_R[i] != []:

        for temp in list(itertools.product(S_U[i], S_R[i])):
            if temp[0]-temp[1]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1]),time)
            
    if S_D[i] != [] and S_L[i] != []:

        for temp in list(itertools.product(S_D[i], S_L[i])):
            if temp[1]-temp[0]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1]),time)
            
    if D_U[i] != [] and D_L[i] != []:

        for temp in list(itertools.product(D_U[i], D_L[i])):
            if temp[1]-temp[0]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1]),time)
            
    if D_D[i] != [] and S_R[i] != []:

        for temp in list(itertools.product(D_D[i], S_R[i])):
            if temp[0]-temp[1]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1]),time)

if safe == 0:
    print("SAFE")
else:
    print(time*10)

Submission status: Runtime Error

Solution code:
N = int(input())
X = []
Y = []
U = []
time = []
for i in range(N):
  xappend, yappend, uappend = input().split()
  xappend = int(xappend)
  yappend = int(yappend)
  X.append(xappend)
  Y.append(yappend)
  U.append(uappend)
  if i > 0:
    for j in range(i):
      if U[i] == "U" and U[j] == "D":
        if Y[i] < Y[j] and X[i] == X[j]:
          time.append((Y[j]-Y[i])*5)

      elif U[i] == "D" and U[j] == "U":
          if Y[i] > Y[j] and X[i] == X[j]:
            time.append((Y[i]-Y[j])*5)

      elif U[i] == "R" and U[j] == "L":
        if X[i] < X[j] and Y[i] == Y[j]:
          time.append((X[j]-X[i])*5)

      elif U[i] == "L" and U[j] == "R":
        if X[i] > X[j] and Y[i] == Y[j]:
          time.append((X[i]-X[j])*5)

      elif U[i] == "U" and U[j] == "R":
        if X[i] > X[j] and Y[i] < Y[j] and X[i]-X[j] == Y[j]-Y[i]:
          time.append((X[i]-X[j])*10)

      elif U[i] == "U" and U[j] == "L":
        if X[i] < X[j] and Y[i] < Y[j] and X[j]-X[i] == Y[j]-Y[i]:
          time.append((X[j]-X[i])*10)

      elif U[i] == "D" and U[j] == "R":
        if X[i] > X[j] and Y[i] > Y[j] and X[i]-X[j] == Y[i]-Y[j]:
          time.append((X[i]-X[j])*10)

      elif U[i] == "D" and U[j] == "L":
        if X[i] < X[j] and Y[i] > Y[j] and X[j]-X[i] == Y[j]-Y[i]:
          time.append((X[j]-X[i])*10)

      elif U[i] == "R" and U[j] == "U":
        if X[i] < X[j] and Y[i] > Y[j] and X[j]-X[i] == Y[i]-Y[j]:
          time.append((X[j]-X[i])*10)

      elif U[i] == "R" and U[j] == "D":
        if X[i] < X[j] and Y[i] < Y[j] and X[j]-X[i] == Y[j]-Y[i]:
          time.append((X[i]-X[j])*10)

      elif U[i] == "L" and U[j] == "U":
        if X[i] < X[j] and Y[i] < Y[j] and X[i]-X[j] == Y[i]-Y[j]:
          time = min((X[j]-X[i])*10, time)

      elif U[i] == "L" and U[j] == "D":
        if X[i] > X[j] and Y[i] < Y[j] and X[i]-X[j] == Y[j]-Y[i]:
          time = min((X[i]-X[j])*10, time)

if len(time) > 0:
  time.sort()
  print(time[0])
else:
  print("SAFE")