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:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [list(map(str, input().split())) for _ in range(row)]
        return map(list, zip(*read_all))

#################

N = I()
X,Y,U = LIR(N,3)
X = [int(x) for x in X]
Y = [int(y) for y in Y]

d1 = defaultdict(lambda:defaultdict(list))
d2 = defaultdict(lambda:defaultdict(list))

u = defaultdict(list)
r = defaultdict(list)
d = defaultdict(list)
l = defaultdict(list)

for i in range(N):
    if U[i] == 'U':
        u[X[i]].append(Y[i])
    elif U[i] == 'D':
        d[X[i]].append(Y[i])
    elif U[i] == 'R':
        r[Y[i]].append(X[i])
    else:
        l[Y[i]].append(X[i])

for k in u.keys():
    u[k].sort()

for k in d.keys():
    d[k].sort()

for k in r.keys():
    r[k].sort()

for k in l.keys():
    l[k].sort()

for i in range(N):
    d1[U[i]][Y[i]-X[i]].append(X[i])
    d2[U[i]][Y[i]+X[i]].append(X[i])

ans = float('inf')
for i in range(N):
    if U[i] == 'U':
        p = bisect_left(d[X[i]],Y[i])
        if p <= len(d[X[i]])-1 and len(d[X[i]]):
            ans = min(ans,(d[X[i]][p]-Y[i])*5)

        p = bisect_left(d1['L'][Y[i]-X[i]],X[i])
        if p <= len(d1['L'][Y[i]-X[i]])-1 and len(d1['L'][Y[i]-X[i]]):
            ans = min(ans,(d1['L'][Y[i]-X[i]][p]-X[i])*10)  

        p = bisect_left(d2['R'][Y[i]+X[i]],X[i])
        if 0 <= p-1 <= len(d2['R'][Y[i]+X[i]])-1 and len(d2['R'][Y[i]+X[i]]):
            ans = min(ans,(X[i]-d2['R'][Y[i]+X[i]][p-1])*10)

    elif U[i] == 'D':
        p = bisect_left(u[X[i]],Y[i])
        if 0 <= p-1 <= len(u[X[i]])-1 and len(u[X[i]]):
            ans = min(ans,(Y[i]-u[X[i]][p-1])*5)

        p = bisect_left(d1['R'][Y[i]-X[i]],X[i])
        if 0 <= p-1 <= len(d1['R'][Y[i]-X[i]])-1 and len(d1['R'][Y[i]-X[i]]):
            ans = min(ans,(X[i]-d1['R'][Y[i]-X[i]][p])*10)       

        p = bisect_left(d2['L'][Y[i]+X[i]],X[i])
        if p <= len(d2['L'][Y[i]+X[i]])-1 and len(d2['L'][Y[i]+X[i]]):
            ans = min(ans,(d2['L'][Y[i]+X[i]][p]-X[i])*10)

    elif U[i] == 'R':
        p = bisect_left(l[Y[i]],X[i])
        if p <= len(l[Y[i]])-1 and len(l[Y[i]]):
            ans = min(ans,(l[Y[i]][p]-X[i])*5)

        p = bisect_left(d1['D'][Y[i]-X[i]],X[i])
        if p <= len(d1['D'][Y[i]-X[i]])-1 and len(d1['D'][Y[i]-X[i]]):
            ans = min(ans,(d1['D'][Y[i]-X[i]][p]-X[i])*10)       

        p = bisect_left(d2['U'][Y[i]+X[i]],X[i])
        if p <= len(d2['U'][Y[i]+X[i]])-1 and len(d2['U'][Y[i]+X[i]]):
            ans = min(ans,(d2['U'][Y[i]+X[i]][p]-X[i])*10)
    else:
        p = bisect_left(r[Y[i]],X[i])
        if 0 <=p-1 <= len(r[Y[i]])-1 and len(r[Y[i]]):
            ans = min(ans,(X[i]-l[Y[i]][p-1])*5)

        p = bisect_left(d2['D'][Y[i]-X[i]],X[i])
        if 0 <= p-1 <= len(d2['D'][Y[i]-X[i]])-1 and len(d2['D'][Y[i]-X[i]]):
            ans = min(ans,(X[i]-d2['D'][Y[i]-X[i]][p-1])*10)       

        p = bisect_left(d1['U'][Y[i]+X[i]],X[i])
        if 0 <= p-1 <= len(d1['U'][Y[i]+X[i]])-1 and len(d1['U'][Y[i]+X[i]]):
            ans = min(ans,(X[i]-d1['U'][Y[i]+X[i]][p-1])*10)


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

Submission status: Runtime Error

Solution code:
def LI():
  return list(map(int,input().split()))
# from itertools import product
# from scipy.optimize import minimize
# n=int(input())

# xxx=[]
# yyy=[]
# ppp=[]
# for i in range(n):
#     x,y,p=LI()
#     xxx+=x,
#     yyy+=y,
#     ppp+=p,

# def solve(xxx=xxx,yyy=yyy,ppp=ppp,k):
#     ret=0
#     for xk in range(k+1):
#         yk=k-xk
        
#         for xx in range(xk)
    
    
#     return ret

# for k in range(n+1):
#     print(minimize(solve,)

from collections import defaultdict

n=int(input())

d=defaultdict(list)
MA=10**6
for i in range(n):
    x,y,u=map(str,input().split())
    d[u].append((int(x),int(y)))

ans=10**15

for rr in d["R"]:
    x,y=rr
    for ll in d["L"]:
        i,j=ll
        if y==j and j-y>0:
            ans=min(ans,j-y)
    for uu in d["U"]:
        i,j=uu
        if x-i==j-y:
            ans=min(ans,x-i)
    for dd in d["D"]:
        i,j=dd
        if i-x==j-y:
            ans=min(ans,i-x)

?????

print("SAFE" if ans==10**15 else ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)

INF = 10**10
M = 4*10**5 + 10
M = 100

N = int(input())
PUs = [[] for _ in range(M)]
PRs = [[] for _ in range(M)]
PDs = [[] for _ in range(M)]
PLs = [[] for _ in range(M)]
nUs = [[] for _ in range(M)]
nRs = [[] for _ in range(M)]
nDs = [[] for _ in range(M)]
nLs = [[] for _ in range(M)]
xUs = [[] for _ in range(M)]
xDs = [[] for _ in range(M)]
yLs = [[] for _ in range(M)]
yRs = [[] for _ in range(M)]

for _ in range(N):
    X, Y, U = input().split()
    X, Y = int(X), int(Y)
    if U == 'L':
        PLs[X+Y].append((X, U))
        nLs[X-Y].append((X, U))
        yLs[Y].append((X, U))
    elif U == 'R':
        PRs[X+Y].append((X, U))
        nRs[X-Y].append((X, U))
        yRs[Y].append((X, U))
    elif U == 'U':
        PUs[X+Y].append((X, U))
        nUs[X-Y].append((X, U))
        xUs[X].append((Y, U))
    else:  # U == 'D'
        PDs[X+Y].append((X, U))
        nDs[X-Y].append((X, U))
        xDs[X].append((Y, U))

def func(P1s, P2s, dir1, dir2):
    dist = INF
    for z in range(M):
        Ps = sorted(P1s[z] + P2s[z])
        for i in range(len(Ps)-1):
            if Ps[i][1] == dir1 and Ps[i+1][1] == dir2:
                d = Ps[i+1][0] - Ps[i][0]
                if d < dist:
                    dist = d
    return dist

ans = INF
dists = []
dists += [func(PRs, PUs, 'R', 'U') * 2]
dists += [func(PDs, PLs, 'D', 'L') * 2]
dists += [func(nRs, nDs, 'R', 'D') * 2]
dists += [func(nUs, nLs, 'U', 'L') * 2]
dists += [func(yRs, yLs, 'R', 'L')]
dists += [func(xUs, xDs, 'U', 'D')]

ans = min(dists)
if ans == INF:
    print('SAFE')
else:
    print(ans*5)

Submission status: Runtime Error

Solution code:
N = int(input())
U = []
D = []
R = []
L = []
U_co = 0
D_co = 0
R_co = 0
L_co = 0
ans = 10 ** 10
for i in range(N):
    P = input().rstrip().split(" ")
    if P[2] == "U":U.append(list(map(int,P[0:2])))
    elif P[2] == "D":D.append(list(map(int,P[0:2])))
    elif P[2] == "R":R.append(list(map(int,P[0:2])))
    elif P[2] == "L":L.append(list(map(int,P[0:2])))
U.sort()
D.sort()
while U_co < len(U) and D_co < len(D):
    if U[U_co][0] > D[D_co][0]:D_co += 1
    elif U[U_co][0] < D[D_co][0]:U_co += 1
    else:
        if U[U_co][1] < D[D_co][1]:
            ans = min(ans,(D[D_co][1] - U[U_co][1])*5)
        if U_co + 1 < len(U) and U[U_co + 1][1] == D[D_co][0]:U_co += 1
        else: D_co += 1
R.sort(key = lambda x : x[1])
L.sort(key = lambda x : x[1])
while R_co < len(R) and  L_co < len(L):
    if R[R_co][0] > L[L_co][0]:L_co += 1
    elif R[R_co][0] < L[L_co][0]:R_co += 1
    else:
        if R[R_co][1] < L[L_co][1]:
            ans = min(ans,(L[L_co][1] - R[R_co][1])*5)
        if R_co + 1 < len(R) and R[R_co + 1][1] == L[L_co][0]:R_co += 1
        else: L_co += 1
U_co = 0
R_co = 0
U.sort(key = lambda x : x[0] + x[1])
R.sort(key = lambda x : x[0] + x[1])
while R_co < len(R) and  L_co < len(L):
    if R[R_co][0] + R[R_co][1] > U[U_co][0] + U[U_co][1]:U_co += 1
    elif R[R_co][0] + R[R_co][1] < U[U_co][0] + U[U_co][1]:R_co += 1
    else:
        if R[R_co][1] > U[U_co][1]:
            ans = min(ans,(U[U_co][1] - R[R_co][1])*10)
        if R_co + 1 < len(R) and R[R_co + 1][0] + R[R_co + 1][1] == U[U_co][0] + U[U_co][1]:R_co += 1
        else: U_co += 1

D_co = 0
L_co = 0
D.sort(key = lambda x : x[0] + x[1])
L.sort(key = lambda x : x[0] + x[1])
while D_co < len(D) and  L_co < len(L):
    if D[D_co][0] + D[D_co][1] > L[L_co][0] + L[L_co][1]:L_co += 1
    elif D[D_co][0] + D[D_co][1] < L[L_co][0] + L[L_co][1]:D_co += 1
    else:
        if D[D_co][1] > L[L_co][1]:
            ans = min(ans,(D[D_co][1] - L[L_co][1])*10)
        if D_co + 1 < len(D) and D[D_co + 1][0] + D[D_co + 1][1] == L[L_co][0] + L[L_co][1]:D_co += 1
        else: L_co += 1

U_co = 0
L_co = 0
U.sort(key = lambda x : x[0] - x[1])
L.sort(key = lambda x : x[0] - x[1])
while U_co < len(U) and  L_co < len(L):
    if U[U_co][0] - U[U_co][1] > L[L_co][0] - L[L_co][1]:L_co += 1
    elif U[U_co][0] - U[U_co][1] < L[L_co][0] - L[L_co][1]:U_co += 1
    else:
        if U[U_co][1] < L[L_co][1]:
            ans = min(ans,(L[L_co][1] - U[U_co][1])*10)
        if U_co + 1 < len(U) and U[U_co - 1][0] + U[U_co - 1][1] == L[L_co][0] - L[L_co][1]:U_co += 1
        else: L_co += 1

D_co = 0
R_co = 0
D.sort(key = lambda x : x[0] - x[1])
R.sort(key = lambda x : x[0] - x[1])
while D_co < len(D) and R_co < len(R):
    if D[D_co][0] - D[D_co][1] > R[R_co][0] - R[R_co][1]:R_co += 1
    elif D[D_co][0] - D[D_co][1] < R[R_co][0] - R[R_co][1]:D_co += 1
    else:
        if D[D_co][1] > R[R_co][1]:
            ans = min(ans,(D[D_co][1] - R[R_co][1])*10)
        if D_co + 1 < len(D) and D[D_co - 1][0] + D[D_co - 1][1] == R[R_co][0] - R[R_co][1]:D_co += 1
        else: R_co += 1
        
if (ans == 10 ** 10):
    print("SAFE")
else:
    print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
f = [[], [], [], []]
for _ in range(N):
    X, Y, U = map(int, input().split())
    if U == "U":
        f[0].append((X, Y))
    elif U == "R":
        f[1].append((X, Y))
    elif U == "D":
        f[2].append((X, Y))
    else:
        f[3].append((X, Y))

earliest = 2000000
ud = {}
rl = {}
ur_dl = {}
ul_dr = {}

for i in f[0]:
    if i[0] not in ud:
        ud[i[0]] = [[i[1]], []]
    else:
        ud[i[0]][0] += i[1]
    if i[0] + i[1] not in ur_dl:
        ur_dl[i[0] + i[1]] = [i[0]]
    else:
        ur_dl[i[0] + i[1]] += i[0]

for i in f[2]:
    if i[0] in ud:
        ud[i[0]][1] += i[1]
    if i[0] + i[1] not in ur_dl:
        ur_dl[i[0] + i[1]] = [i[0]]
    else:
        ur_dl[i[0] + i[1]] += i[0]

for i in ud.values():
    for j in i[0]:
        for k in i[1]:
            dis = abs(j - k)
            if j < k and dis < earliest:
                earliest = dis

for i in f[1]:
    if i[0] not in rl:
        rl[i[1]] = [[i[0]], []]
    else:
        rl[i[1]][0] += i[0]
    if i[0] + i[1] not in ur_dl:
        ur_dl[i[0] + i[1]] = [i[0]]
    else:
        ur_dl[i[0] + i[1]] += i[0]

for i in f[3]:
    if i[1] in rl:
        rl[i[1]][1] += i[0]
    if i[0] + i[1] in ur_dl:
        ur_dl[i[0] + i[1]] += i[0]

for i in rl.values():
    for j in i[0]:
        for k in i[1]:
            dis = abs(j - k)
            if j < k and dis < earliest:
                earliest = dis

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)

INF = 10**10
M = 4*10**5 + 10
M = 100

N = int(input())
PUs = [[] for _ in range(M)]
PRs = [[] for _ in range(M)]
PDs = [[] for _ in range(M)]
PLs = [[] for _ in range(M)]
nUs = [[] for _ in range(M)]
nRs = [[] for _ in range(M)]
nDs = [[] for _ in range(M)]
nLs = [[] for _ in range(M)]
xUs = [[] for _ in range(M)]
xDs = [[] for _ in range(M)]
yLs = [[] for _ in range(M)]
yRs = [[] for _ in range(M)]

for _ in range(N):
    X, Y, U = input().split()
    X, Y = int(X), int(Y)
    if U == 'L':
        PLs[X+Y].append((X, U))
        nLs[X-Y].append((X, U))
        yLs[Y].append((X, U))
    elif U == 'R':
        PRs[X+Y].append((X, U))
        nRs[X-Y].append((X, U))
        yRs[Y].append((X, U))
    elif U == 'U':
        PUs[X+Y].append((X, U))
        nUs[X-Y].append((X, U))
        xUs[X].append((Y, U))
    else:  # U == 'D'
        PDs[X+Y].append((X, U))
        nDs[X-Y].append((X, U))
        xDs[X].append((Y, U))

def func(P1s, P2s, dir1, dir2):
    dist = INF
    for z in range(M):
        Ps = sorted(P1s[z] + P2s[z])
        for i in range(len(Ps)-1):
            if Ps[i][1] == dir1 and Ps[i+1][1] == dir2:
                d = Ps[i+1][0] - Ps[i][0]
                if d < dist:
                    dist = d
        if Ps:
    return dist

ans = INF
dists = []
dists += [func(PRs, PUs, 'R', 'U') * 2]
dists += [func(PDs, PLs, 'D', 'L') * 2]
dists += [func(nRs, nDs, 'R', 'D') * 2]
dists += [func(nUs, nLs, 'U', 'L') * 2]
dists += [func(yRs, yLs, 'R', 'L')]
dists += [func(xUs, xDs, 'U', 'D')]

ans = min(dists)
if ans == INF:
    print('SAFE')
else:
    print(ans*5)

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict
from bisect import bisect_left, bisect_right
input = sys.stdin.readline

n = int(input())
xyd = []
x_const_down = defaultdict(list)
x_const_up = defaultdict(list)

y_const_left = defaultdict(list)
y_const_right = defaultdict(list)

xmy_const_left = defaultdict(list)
xmy_const_down = defaultdict(list)

xpy_const_up = defaultdict(list)
xpy_const_left = defaultdict(list)

for _ in range(n):
    x, y, d = input().split()
    xyd.append((int(x), int(y), d))

    if d == 'U':
        x_const_up[x].append(y)
        xpy_const_up[x + y].append(x - y)
    elif d == 'D':
        x_const_down[x].append(y)
        xmy_const_down[x - y].append(x + y)
    elif d == 'L':
        y_const_left[y].append(x)
        xmy_const_left[x - y].append(x + y)
        xpy_const_left[x + y].append(x - y)
    else:  # d == 'R'
        y_const_right[y].append(x)

x_const_down = dict(x_const_down)
for i in x_const_down:
    x_const_down[i].sort()
x_const_up = dict(x_const_up)
for i in x_const_up:
    x_const_up[i].sort()
y_const_left = dict(y_const_left)
for i in y_const_left:
    y_const_left[i].sort()
y_const_right = dict(y_const_right)
for i in y_const_right:
    y_const_right[i].sort()

xmy_const_left = dict(xmy_const_left)
for i in xmy_const_left:
    xmy_const_left[i].sort()

xmy_const_down = dict(xmy_const_down)
for i in xmy_const_down:
    xmy_const_down[i].sort()

xpy_const_right = dict(xpy_const_right)
for i in xpy_const_right:
    xpy_const_right[i].sort()
xpy_const_down = dict(xpy_const_down)
for i in xpy_const_down:
    xpy_const_down[i].sort()

print(*xyd, sep='\n')

ans = float('inf')

# in x_const
for x, y, d in xyd:
    if d == 'U':
        if x in x_const_down:
            idx = bisect_left(x_const_down[x], y)
            ans = min(x_const_down[x][idx] - y) * 10
        if x - y in xmy_const_left:
            idx = bisect_left(xmy_const_left[x - y], x + y)
            ans = min(xmy_const_left[x - y][idx] - (x + y)) // 2 * 10
    elif d == 'D':
        if x + y in xpy_const_left:
            idx = bisect_left(xpy_const_left[x + y], x - y)
            ans = min(xpy_const_left[x + y][idx] - (x - y)) // 2 * 10
    elif d == 'L':
        if y in y_const_right:
            idx = bisect_left(y_const_right[y], x)
            ans = min(y_const_right[y][idx] - x) * 10
    else:  # d == 'R'
        if x - y in xmy_const_down:
            idx = bisect_left(xmy_const_down[x - y], x + y)
            ans = min(xmy_const_down[x - y][idx] - (x + y)) // 2 * 10
        if x + y in xpy_const_right:
            idx = bisect_left(xpy_const_right[x + y], x - y)
            ans = min(xpy_const_right[x + y][idx] - (x - y)) // 2 * 10

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

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import bisect_right
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
N = int(input())
M = 4*10**5
num1 = [[] for _ in range(M+1)]
num2 = [[] for _ in range(M+1)]
num3 = [[] for _ in range(2*10**5+1)]
num4 = [[] for _ in range(2*10**5+1)]
for i in range(N):
    x,y,sss = input().split()
    x = int(x)
    y = int(y)
    num1[x+y].append([x,sss])
    num2[x-y+2*10**5].append([x,sss])
    num3[x].append([x,sss])
    num3[y].append([y,sss])
flag = 0
MIN = 10**25
for i in range(4*10**5):
    n = len(num1[i])
    if n >= 2:
        li = num1[i]
        ue = []
        migi = []
        for j in range(n):
            xx,ar = li[j]
            if ar == "U" or ar == "D":
                ue.append(xx)
            else:
                migi.append(xx)
        un = len(ue)
        mn = len(migi)
        if un != 0 and mn != 0:
            flag = 1
            vMIN = 10**26
            for uu in range(un):
                for mm in range(mn):
                    vMIN = min(vMIN,abs(ue[uu]-migi[mm]))
            MIN = min(MIN,vMIN)
    n2 = len(num2[i])
    if n2 >= 2:
        li2 = num2[i]
        ue2 = []
        migi2 = []
        for j2 in range(n2):
            xx2,ar2 = li[j2]
            if ar2 == "U" or ar2 == "D":
                ue2.append(xx2)
            else:
                migi2.append(xx2)
        un2 = len(ue2)
        mn2 = len(migi2)
        if un2 != 0 and mn2 != 0:
            flag = 1
            vMIN2 = 10**26
            for uu2 in range(un2):
                for mm2 in range(mn2):
                    vMIN2 = min(vMIN2,abs(ue2[uu2]-migi2[mm2]))
            MIN = min(MIN,vMIN2)
    n3 = len(num3[i])
    if n3 >= 2:
        li3 = num3[i]
        uemax = -1
        shitamin = 10**10
        for j3 in range(n3):
            xx3,ar3 = li[j3]

            if ar3 == "U" :
                uemax = max(uemax,xx3)
            elif ar3 == "D":
                shitamin = max(shitamin,xx3)
        if uemax != -1 and shitamin != 10**10:
            flag = 1
            vmin3 = shitamin-uemax
            MIN = min(MIN,vmin3)
    n4 = len(num4[i])
    if n4 >= 2:
        li4 = num4[i]
        uemax = -1
        shitamin = 10**10
        for j4 in range(n4):
            xx4,ar4 = li[j4]

            if ar4 == "R" :
                uemax = max(uemax,xx4)
            elif ar3 == "L":
                shitamin = max(shitamin,xx4)
        if uemax != -1 and shitamin != 10**10:
            flag = 1
            vmin4 = shitamin-uemax
            MIN = min(MIN,vmin4)
    
if flag == 0:
    print("SAFE")
else:
    print(MIN*10)

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 = []
from collections import defaultdict
UR = defaultdict(list)
UL = defaultdict(list)
DR = defaultdict(list)
DL = defaultdict(list)
LR = defaultdict(list)
UD = defaultdict(list)
for _ in range(N):
    x, y, u = input().split()
    XYU.append((x, y, u))
XYU.sort()
for x, y, u in XYU:
    x, y = int(x), int(y)
    if u=='U':
        UR[(x+y)].append((x, y))
        UL[(x-y)].append((x, y))
        UD[x].append((x, y))
    if u=='R':
        LR[y].append((x, y))
    if u=='D':
        DL[(x+y)].append((x, y))
        DR[(x-y)].append((x, y))

ans = 10**18
def check(x, y, u):
    global ans
    if u=='R':
        for px, py in UR[(x+y)]:
            if x<px and y>py:
                ans = min(ans, px-x)
                
        for px, py in DR[(x-y)]:
            if x<px and py>y:
                ans = min(ans, px-x)
                
    if u=='L':
        for px, py in UL[(x-y)]:
            if x<px: break
            if x>px and y>py:
                ans = min(ans, x-px)
                
        for px, py in DL[(x+y)]:
            if x>px and py>y:
                ans = min(ans, x-px) 
                
        for px, py in LR[y]:
            if x<px: break
            if x>px:
                ans = min(ans, (x-px)/2)
                
    if u=='D':
        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:
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

from operator import itemgetter

n = readint()

ans = 10**10

u = []
d = []
l = []
r = []
uu = [[]for i in range(2*10**5+1)]
dd = [[]for i in range(2*10**5+1)]
ll = [[]for i in range(2*10**5+1)]
rr = [[]for i in range(2*10**5+1)]
for i in range(n):
    x,y,a = readstrs()
    x = int(x)
    y = int(y)
    if a=='U':
        u.append([x,y,x+y,x-y])
        uu[x].append(y)
    elif a == 'D':
        d.append([x,y,x+y,x-y])
        dd[x].append(y)
    elif a == 'L':
        l.append([x,y,x+y,x-y])
        ll[y].append(x)
    elif a == 'R':
        r.append([x,y,x+y,x-y])
        rr[y].append(x)

for i in range(2*10**5+1):
    if uu[i] and dd[i]:
        uu[i].sort()
        dd[i].sort()
        lenu = len(uu[i])
        lend = len(dd[i])
        k = 0
        j = 0
        while k<lenu and j<lend:
            if dd[i][j]>uu[i][k]:
                ans = min(ans,(dd[i][j]-uu[i][k])/2)
                k += 1
            else:
                j += 1
    if ll[i] and rr[i]:
        ll[i].sort()
        rr[i].sort()
        lenl = len(ll[i])
        lenr = len(rr[i])
        k = 0
        j = 0
        while k<lenl and j<lenr:
            if ll[i][j]>rr[i][k]:
                ans = min(ans,(ll[i][j]-rr[i][k])/2)
                k += 1
            else:
                j += 1

uu = [[]for i in range(4*10**5+100)]
dd = [[]for i in range(4*10**5+100)]
ll = [[]for i in range(4*10**5+100)]
rr = [[]for i in range(4*10**5+100)]

for a in u:
    uu[a[3]].append(a[0])
for a in d:
    dd[a[3]].append(a[0])    
for a in l:
    ll[a[3]].append(a[0])
for a in r:
    rr[a[3]].append(a[0])

for i in range(4*10**5+1):
    if uu[i] and ll[i]:
        uu[i].sort()
        ll[i].sort()
        lenu = len(uu[i])
        lenl = len(ll[i])
        k = 0
        j = 0
        while k<lenu and j<lenl:
            if ll[i][j]>uu[i][k]:
                ans = min(ans,ll[i][j]-uu[i][k])
                k += 1
            else:
                j += 1
    if dd[i] and rr[i]:
        dd[i].sort()
        rr[i].sort()
        lend = len(dd[i])
        lenr = len(rr[i])
        k = 0
        j = 0
        while j<lend and k<lenr:
            if dd[i][j]>rr[i][k]:
                ans = min(ans,dd[i][j]-rr[i][k])
                k += 1
            else:
                j += 1

        
uu = [[]for i in range(4*10**5+100)]
dd = [[]for i in range(4*10**5+100)]
ll = [[]for i in range(4*10**5+100)]
rr = [[]for i in range(4*10**5+100)]

for a in u:
    uu[a[2]].append(a[0])
for a in d:
    dd[a[2]].append(a[0])    
for a in l:
    ll[a[2]].append(a[0])
for a in r:
    rr[a[2]].append(a[0])

for i in range(4*10**5+1):
    if uu[i] and rr[i]:
        uu[i].sort()
        rr[i].sort()
        lenu = len(uu[i])
        lenr = len(rr[i])
        k = 0
        j = 0
        while k<lenu and j<lenr:
            if rr[i][j]<uu[i][k]:
                ans = min(ans,-rr[i][j]+uu[i][k])
                j += 1
            else:
                k += 1
    if dd[i] and ll[i]:
        dd[i].sort()
        ll[i].sort()
        lend = len(dd[i])
        lenl = len(ll[i])
        k = 0
        j = 0
        while j<lend and k<lenl:
            if dd[i][j]<ll[i][k]:
                ans = min(ans,-dd[i][j]+ll[i][k])
                j += 1
            else:
                k += 1

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

Submission status: Runtime Error

Solution code:
f

Submission status: Runtime Error

Solution code:
N = int(input())
U = []
D = []
R = []
L = []
U_co = 0
D_co = 0
R_co = 0
L_co = 0
ans = 10 ** 10
for i in range(N):
    P = input().rstrip().split(" ")
    if P[2] == "U":U.append(list(map(int,P[0:2])))
    elif P[2] == "D":D.append(list(map(int,P[0:2])))
    elif P[2] == "R":R.append(list(map(int,P[0:2])))
    elif P[2] == "L":L.append(list(map(int,P[0:2])))
U.sort()
D.sort()
while U_co < len(U) and D_co < len(D):
    if U[U_co][0] > D[D_co][0]:D_co += 1
    elif U[U_co][0] < D[D_co][0]:U_co += 1
    else:
        if U[U_co][1] < D[D_co][1]:
            ans = min(ans,(D[D_co][1] - U[U_co][1])*5)
        if U_co + 1 < len(U) and U[U_co + 1][1] == D[D_co][0]:U_co += 1
        else: D_co += 1
R.sort(key = lambda x : x[1])
L.sort(key = lambda x : x[1])
while R_co < len(R) and  L_co < len(L):
    if R[R_co][0] > L[L_co][0]:L_co += 1
    elif R[R_co][0] < L[L_co][0]:R_co += 1
    else:
        if R[R_co][1] < L[L_co][1]:
            ans = min(ans,(L[L_co][1] - R[R_co][1])*5)
        if R_co + 1 < len(R) and R[R_co + 1][1] == L[L_co][0]:R_co += 1
        else: L_co += 1
U_co = 0
R_co = 0
U.sort(key = lambda x : x[0] + x[1])
R.sort(key = lambda x : x[0] + x[1])
while R_co < len(R) and  L_co < len(L):
    if R[R_co][0] + R[R_co][1] > U[U_co][0] + U[U_co][1]:U_co += 1
    elif R[R_co][0] + R[R_co][1] < U[U_co][0] + U[U_co][1]:R_co += 1
    else:
        if R[R_co][1] > U[U_co][1]:
            ans = min(ans,(U[U_co][1] - R[R_co][1])*10)
        if R_co + 1 < len(R) and R[R_co + 1][0] + R[R_co + 1][1] == U[U_co][0] + U[U_co][1]:R_co += 1
        else: U_co += 1

D_co = 0
L_co = 0
D.sort(key = lambda x : x[0] + x[1])
L.sort(key = lambda x : x[0] + x[1])
while D_co < len(D) and  L_co < len(L):
    if D[D_co][0] + D[D_co][1] > L[L_co][0] + L[L_co][1]:L_co += 1
    elif D[D_co][0] + D[D_co][1] < L[L_co][0] + L[L_co][1]:D_co += 1
    else:
        if D[D_co][1] > L[L_co][1]:
            ans = min(ans,(D[D_co][1] - L[L_co][1])*10)
        if D_co + 1 < len(D) and D[D_co + 1][0] + D[D_co + 1][1] == L[L_co][0] + L[L_co][1]:D_co += 1
        else: L_co += 1

U_co = 0
L_co = 0
U.sort(key = lambda x : x[0] - x[1])
L.sort(key = lambda x : x[0] - x[1])
while U_co < len(U) and  L_co < len(L):
    if U[U_co][0] - U[U_co][1] > L[L_co][0] - L[L_co][1]:L_co += 1
    elif U[U_co][0] - U[U_co][1] < L[L_co][0] - L[L_co][1]:U_co += 1
    else:
        if U[U_co][1] < L[L_co][1]:
            ans = min(ans,(L[L_co][1] - U[U_co][1])*10)
        if U_co + 1 < len(U) and U[U_co - 1][0] + U[U_co - 1][1] == L[L_co][0] - L[L_co][1]:U_co += 1
        else: L_co += 1

D_co = 0
R_co = 0
D.sort(key = lambda x : x[0] - x[1])
R.sort(key = lambda x : x[0] - x[1])
while D_co < len(D) and R_co < len(R):
    if D[D_co][0] - D[D_co][1] > R[R_co][0] - R[R_co][1]:R_co += 1
    elif D[D_co][0] - D[D_co][1] < R[R_co][0] - R[R_co][1]:D_co += 1
    else:
        if D[D_co][1] > R[R_co][1]:
            ans = min(ans,(D[D_co][1] - R[R_co][1])*10)
        if D_co + 1 < Ren(D) and D[D_co - 1][0] + D[D_co - 1][1] == R[R_co][0] - R[R_co][1]:D_co += 1
        else: R_co += 1
        
if (ans == 10 ** 10):
    print("SAFE")
else:
    print(ans)