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: Wrong Answer

Solution code:
n = int(input())
m = 200000
y_r = [[] for _ in range(m+1)]
y_l = [[] for _ in range(m+1)]
x_u = [[] for _ in range(m+1)]
x_d = [[] for _ in range(m+1)]
xy_r = [[] for _ in range(2*m+1)]
xy_l = [[] for _ in range(2*m+1)]
xy_u = [[] for _ in range(2*m+1)]
xy_d = [[] for _ in range(2*m+1)]
for _ in range(n):
    x, y, u = input().split()
    x, y = int(x), int(y)
    if u == 'U':
        x_u[x].append(y)
        xy_u[x+y].append(x)
    if u == 'D':
        x_d[x].append(y)
        xy_d[x+y].append(x)
    if u == 'R':
        y_r[y].append(x)
        xy_r[x+y].append(x)
    if u == 'L':
        y_l[y].append(x)
        xy_l[x+y].append(x)

    

import bisect
ans = m * 10
for i in range(m+1):
    x_u[i].sort()
    x_d[i].sort()
    y_l[i].sort()
    y_r[i].sort()

    for yu in x_u[i]:
        idx = bisect.bisect_left(x_d[i], yu)
        if idx < len(x_d[i]):
            ans = min(ans, (x_d[i][idx] - yu) * 5)

    for xr in y_r[i]:
        idx = bisect.bisect_left(y_l[i], xr)
        if idx < len(y_l[i]):
            ans = min(ans, (y_l[i][idx] - xr) * 5)

for i in range(2*m+1):
    xy_u[i].sort()
    xy_d[i].sort()
    xy_l[i].sort()
    xy_r[i].sort()

    for xr in xy_r[i]:
        idx = bisect.bisect_left(xy_u[i], xr)
        if idx < len(xy_u[i]):
            ans = min(ans, (xy_u[i][idx] - xr) * 10)
    
    for xd in xy_d[i]:
        idx = bisect.bisect_left(xy_l[i], xd)
        if idx < len(xy_l[i]):
            ans = min(ans, (xy_l[i][idx] - xd) * 10)


print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
import numpy as np

def idou(x, y ,p):
    if p == 'U':
        return (x, y+1)
    elif p == 'R':
        return (x+1, y)
    elif p == 'D':
        return (x, y-1)
    else:
        return (x-1, y)

N = int(input())
X = list()
Y = list()
P = list()
safe_flag = True
t = 100000

for _ in range(N):
    x, y, p = input().split()
    X.append(int(x))
    Y.append(int(y))
    P.append(p)

a = list(itertools.combinations(range(N), 2))
for i, j in a:
    if P[i] == P[j]:
        continue
    man_distance = np.abs(X[i] - X[j]) + np.abs(Y[i] - Y[j])
    if man_distance % 2 == 0:
        new_xi, new_yi = idou(X[i], Y[i], P[i])
        new_xj, new_yj = idou(X[j], Y[j], P[j])
        new_distance = np.abs(new_xi - new_xj) + np.abs(new_yi - new_yj)
        if man_distance - new_distance == 2:
            safe_flag = False
            new_t = (man_distance // 2) * 10
            if new_t < t:
                t = new_t
    else:
        continue
if safe_flag: print('SAFE')
else: print(t)

Submission status: Wrong Answer

Solution code:
import collections
n = int(raw_input())
ais = [raw_input().split() for _ in range(n)]
ais = [[int(u),int(v),d] for u,v,d in ais]
cx = collections.defaultdict(list)
cy = collections.defaultdict(list)
cd = collections.defaultdict(list)
cad = collections.defaultdict(list)
for x,y,d in ais:
	if d == 'U' or d == 'D':
		cx[x].append((y,d))
	if d == 'L' or d == 'R':
		cy[y].append((y,d))
	cd[y - x].append((x,y,d))
	cad[y + x].append((x,y,d))

def f(a,b):
	return ((b - a)/2.0 ) * 10
r = +float('inf')
for x in cx:
	l = cx[x]
	l.sort()
	for a,b in zip(l,l[1:]):
		if a[1] ==  'U' and b[1] == 'D':
			r = min(r, f(a[0], b[0]))
			#print (x,a[0]), (x,b[0]),  'collide'

for y in cy:
	l = cy[y]
	l.sort()
	for a,b in zip(l,l[1:]):
		if a[1] ==  'R' and b[1] == 'L':
			r = min(r, f(a[0], b[0]))
			#print (a[0],y), (b[0],y),  'collide'



for e in cd:
	l = cd[e]
	l.sort()
	for i in range(len(l)):
		u = l[i]
		for j in range(i + 1, len(l)):
			v=  l[j]
			if u[2] == 'U' and v[2] == 'L':
			 	r = min(r, f(u[0],v[0])) #- u[0]
			if u[2] == 'R' and v[2] == 'D':
			 	r = min(r, f(u[0],v[0])) #- u[0]


for e in cad:
	l = cad[e]
	l.sort()
	for i in range(len(l)):
		u = l[i]
		for j in range(i + 1, len(l)):
			v=  l[j]
			if u[2] == 'D' and v[2] == 'L':
			 	r = min(r, f(u[0],v[0]) * 2) #- u[0]
			if u[2] == 'R' and v[2] == 'U':
			 	r = min(r, f(u[0],v[0]) * 2) #- u[0]


print int(r) if r < float('inf') else 'SAFE'

Submission status: Wrong Answer

Solution code:
n = int(input())
x_line = {}
y_line = {}
min_crash = 2e5
for _ in range(n):
    x, y, u = list(input().split())
    x = int(x)
    y = int(y)
    if x_line.get(x):
        x_line[x].append([y, u])
    else:
        x_line[x] = [[y, u]]
    if y_line.get(y):
        y_line[y].append([x, u])
    else:
        y_line[y] = [[x, u]]

min_crash = 2e5
for y_list in x_line.values():
    if len(y_list) >= 2:
        y_list_s = sorted(y_list, key=lambda a: a[0])
        u_y = None
        for y, u in y_list_s:
            if u == "D" and u_y:
                crash = (y-u_y)//2
                min_crash = min(min_crash, crash)
            elif u == "U":
                u_y = y

for x_list in y_line.values():
    if len(x_list) >= 2:
        x_list_s = sorted(x_list, key=lambda a: a[0])
        r_x = None
        for x, u in x_list_s:
            if u == "L" and r_x:
                crash = x-r_x
                min_crash = min(min_crash, crash)
            elif u == "R":
                r_x = x

if min_crash == 2e5:
    print("SAFE")
else:
    print(min_crash*10)

Submission status: Wrong Answer

Solution code:
# Fast IO (only use in integer input)

import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

N = int(input())
plane = []
for _ in range(N):
    xb,yb,ub = input().split()
    x = int(xb)
    y = int(yb)
    if ub == b'U':
        u = 'U'
    elif ub == b'R':
        u = 'R'
    elif ub == b'D':
        u = 'D'
    else:
        u = 'L'
    plane.append((x,y,u))

# UD, LR collision
XR = []
XL = []
YU = []
YD = []
for i in range(200001):
    XR.append([])
    XL.append([])
    YU.append([])
    YD.append([])
for elem in plane:
    if elem[2] == 'U':
        YU[elem[0]].append(elem[1])
    if elem[2] == 'D':
        YD[elem[0]].append(elem[1])
    if elem[2] == 'L':
        XL[elem[1]].append(elem[0])
    if elem[2] == 'R':
        XR[elem[1]].append(elem[0])

        
for i in range(200001):
    XR[i].sort()
    XL[i].sort()
    YU[i].sort()
    YD[i].sort()

minTime = 4000000
for i in range(200001):
    indexR = 0
    indexL = 0
    while indexR < len(XR[i]) and indexL < len(XL[i]):
        if XR[i][indexR] > XL[i][indexL]:
            indexL += 1
        else:
            if (XL[i][indexL] - XR[i][indexR]) * 5 < minTime:
                minTime = (XL[i][indexL] - XR[i][indexR]) * 5
            indexR += 1
    
    indexU = 0
    indexD = 0
    while indexU < len(YU[i]) and indexD < len(YD[i]):
        if YU[i][indexU] > YD[i][indexD]:
            indexD += 1
        else:
            if (YD[i][indexD] - YU[i][indexU]) * 5 < minTime:
                minTime = (YD[i][indexD] - YU[i][indexU]) * 5
            indexU += 1

# Other collisions

XYSumR = []
XYSumU = []
XYSumD = []
XYSumL = []
# Diff is X - Y + 200000
XYDiffR = []
XYDiffU = []
XYDiffD = []
XYDiffL = []

for i in range(400001):
    XYSumR.append([])
    XYSumU.append([])
    XYSumD.append([])
    XYSumL.append([])
    XYDiffR.append([])
    XYDiffU.append([])
    XYDiffD.append([])
    XYDiffL.append([])
for elem in plane:
    if elem[2] == 'U':
        XYSumU[elem[0] + elem[1]].append(elem[0])
        XYDiffU[elem[0] - elem[1] + 200000].append(elem[0])
    if elem[2] == 'D':
        XYSumD[elem[0] + elem[1]].append(elem[0])
        XYDiffD[elem[0] - elem[1] + 200000].append(elem[0])
    if elem[2] == 'L':
        XYSumL[elem[0] + elem[1]].append(elem[0])
        XYDiffL[elem[0] - elem[1] + 200000].append(elem[0])
    if elem[2] == 'R':
        XYSumR[elem[0] + elem[1]].append(elem[0])
        XYDiffR[elem[0] - elem[1] + 200000].append(elem[0])

for i in range(200001):
    XYSumU[i].sort()
    XYDiffU[i].sort()
    XYSumD[i].sort()
    XYDiffD[i].sort()
    XYSumL[i].sort()
    XYDiffL[i].sort()
    XYSumR[i].sort()
    XYDiffR[i].sort()

for i in range(400001):
    indexR = 0
    indexU = 0
    while indexR < len(XYSumR[i]) and indexU < len(XYSumU[i]):
        if XYSumU[i][indexU] > XYSumR[i][indexR]:
            if (XYSumU[i][indexU] - XYSumR[i][indexR]) * 10 < minTime:
                minTime = (XYSumU[i][indexU] - XYSumR[i][indexR]) * 10
            indexR += 1
        else:
            indexU += 1
    indexD = 0
    indexL = 0
    while indexL < len(XYSumL[i]) and indexD < len(XYSumD[i]):
        if XYSumD[i][indexD] > XYSumL[i][indexL]:
            indexL += 1
        else:
            if (XYSumL[i][indexL] - XYSumD[i][indexD]) * 10 < minTime:
                minTime = (XYSumL[i][indexL] - XYSumD[i][indexD]) * 10
            indexD += 1    
    indexL = 0
    indexU = 0
    while indexL < len(XYDiffL[i]) and indexU < len(XYDiffU[i]):
        if XYDiffU[i][indexU] > XYDiffL[i][indexL]:
            indexL += 1
        else:
            if (XYDiffL[i][indexL] - XYDiffU[i][indexU]) * 10 < minTime:
                minTime = (XYDiffL[i][indexL] - XYDiffU[i][indexU]) * 10
            indexU += 1
    indexR = 0
    indexD = 0
    while indexR < len(XYDiffR[i]) and indexD < len(XYDiffD[i]):
        if XYDiffR[i][indexR] > XYDiffD[i][indexD]:
            indexD += 1
        else:
            if (XYDiffD[i][indexD] - XYDiffR[i][indexR]) * 10 < minTime:
                minTime = (XYDiffD[i][indexD] - XYDiffR[i][indexR]) * 10
            indexR += 1

if minTime == 4000000:
    print("SAFE")
else:
    print(minTime)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [list(input().split()) for i in range(n)]
l = 2*10**5 + 5
# l = 500
B = [[int(A[i][0]), int(A[i][1]), int(A[i][0])+int(A[i][1]), int(A[i][0])-int(A[i][1])+l, A[i][2]] for i in range(n)]

D = [[[] for j in range(2*l)] for i in range(4)]

for i in range(4):
    for j in range(n):
        D[i][B[j][i]].append(B[j])

# print(D)
import bisect
ans = float("inf")
for i in range(2*l):
    # print(D[1][i])
    LR = [[], []]
    for j in range(len(D[1][i])):
        
        if D[1][i][j][4] == "L":
            LR[0].append(D[1][i][j][0])
        if D[1][i][j][4] == "R":
            LR[1].append(D[1][i][j][0])

        # print(j,LR)
        for k in range(len(LR[1])):
            ind = bisect.bisect(LR[0], LR[1][k])
            if ind != len(LR[0]):
                ans = min(ans, LR[0][ind]-LR[1][k]) * 5

for i in range(2*l):
    # print(D[0][i])
    LR = [[], []]
    for j in range(len(D[0][i])):
        
        if D[0][i][j][4] == "U":
            LR[0].append(D[0][i][j][1])
        if D[0][i][j][4] == "D":
            LR[1].append(D[0][i][j][1])

        # print(j,LR)
        for k in range(len(LR[0])):
            ind = bisect.bisect(LR[1], LR[0][k])
            # print("ind",ind)
            if ind != len(LR[1]):
                ans = min(ans, LR[1][ind]-LR[0][k]) * 5

for i in range(2*l):
    # print(D[1][i])
    LR = [[], []]
    LR2 = [[], []]
    for j in range(len(D[2][i])):
        
        if D[2][i][j][4] == "L":
            LR[0].append(D[2][i][j][0])
        if D[2][i][j][4] == "D":
            LR[1].append(D[2][i][j][0])

        if D[2][i][j][4] == "U":
            LR2[0].append(D[2][i][j][0])
        if D[2][i][j][4] == "R":
            LR2[1].append(D[2][i][j][0])

        # print(j,LR)
        for k in range(len(LR[1])):
            ind = bisect.bisect(LR[0], LR[1][k])
            if ind != len(LR[0]):
                ans = min(ans, LR[0][ind]-LR[1][k]) * 10
        
        for k in range(len(LR2[1])):
            ind = bisect.bisect(LR2[0], LR2[1][k])
            if ind != len(LR2[0]):
                ans = min(ans, LR2[0][ind]-LR2[1][k]) * 10

for i in range(2*l):
    # print(D[1][i])
    LR = [[], []]
    LR2 = [[], []]
    for j in range(len(D[3][i])):
        
        if D[3][i][j][4] == "D":
            LR[0].append(D[3][i][j][0])
        if D[3][i][j][4] == "R":
            LR[1].append(D[3][i][j][0])

        if D[3][i][j][4] == "L":
            LR2[0].append(D[3][i][j][0])
        if D[3][i][j][4] == "U":
            LR2[1].append(D[3][i][j][0])

        # print(j,LR)
        for k in range(len(LR[1])):
            ind = bisect.bisect(LR[0], LR[1][k])
            if ind != len(LR[0]):
                ans = min(ans, LR[0][ind]-LR[1][k]) * 10
        
        for k in range(len(LR2[1])):
            ind = bisect.bisect(LR2[0], LR2[1][k])
            if ind != len(LR2[0]):
                ans = min(ans, LR2[0][ind]-LR2[1][k]) * 10


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

Submission status: Wrong Answer

Solution code:
# Fast IO (only use in integer input)

import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

N = int(input())
plane = []
for _ in range(N):
    xb,yb,ub = input().split()
    x = int(xb)
    y = int(yb)
    if ub == b'U':
        u = 'U'
    elif ub == b'R':
        u = 'R'
    elif ub == b'D':
        u = 'D'
    else:
        u = 'L'
    plane.append((x,y,u))

# UD, LR collision
XR = []
XL = []
YU = []
YD = []
for i in range(200001):
    XR.append([])
    XL.append([])
    YU.append([])
    YD.append([])
for elem in plane:
    if elem[2] == 'U':
        YU[elem[0]].append(elem[1])
    if elem[2] == 'D':
        YD[elem[0]].append(elem[1])
    if elem[2] == 'L':
        XL[elem[1]].append(elem[0])
    if elem[2] == 'R':
        XR[elem[1]].append(elem[0])

        
for i in range(200001):
    XR[i].sort()
    XL[i].sort()
    YU[i].sort()
    YD[i].sort()

minTime = 4000000
for i in range(200001):
    indexR = 0
    indexL = 0
    while indexR < len(XR[i]) and indexL < len(XL[i]):
        if XR[i][indexR] > XL[i][indexL]:
            indexL += 1
        else:
            if (XL[i][indexL] - XR[i][indexR]) * 5 < minTime:
                minTime = (XL[i][indexL] - XR[i][indexR]) * 5
            indexR += 1
    
    indexU = 0
    indexD = 0
    while indexU < len(YU[i]) and indexD < len(YD[i]):
        if YU[i][indexU] > YD[i][indexD]:
            indexD += 1
        else:
            if (YD[i][indexD] - YU[i][indexU]) * 5 < minTime:
                minTime = (YD[i][indexD] - YU[i][indexU]) * 5
            indexU += 1

# Other collisions

XYSumR = []
XYSumU = []
XYSumD = []
XYSumL = []
# Diff is X - Y + 200000
XYDiffR = []
XYDiffU = []
XYDiffD = []
XYDiffL = []

for i in range(400001):
    XYSumR.append([])
    XYSumU.append([])
    XYSumD.append([])
    XYSumL.append([])
    XYDiffR.append([])
    XYDiffU.append([])
    XYDiffD.append([])
    XYDiffL.append([])
for elem in plane:
    if elem[2] == 'U':
        XYSumU[elem[0] + elem[1]].append(elem[0])
        XYDiffU[elem[0] - elem[1] + 200000].append(elem[0])
    if elem[2] == 'D':
        XYSumD[elem[0] + elem[1]].append(elem[0])
        XYDiffD[elem[0] - elem[1] + 200000].append(elem[0])
    if elem[2] == 'L':
        XYSumL[elem[0] + elem[1]].append(elem[0])
        XYDiffL[elem[0] - elem[1] + 200000].append(elem[0])
    if elem[2] == 'R':
        XYSumR[elem[0] + elem[1]].append(elem[0])
        XYDiffR[elem[0] - elem[1] + 200000].append(elem[0])

for i in range(400001):
    indexR = 0
    indexU = 0
    while indexR < len(XYSumR[i]) and indexU < len(XYSumU[i]):
        if XYSumU[i][indexU] > XYSumR[i][indexR]:
            if (XYSumU[i][indexU] - XYSumR[i][indexR]) * 10 < minTime:
                minTime = (XYSumU[i][indexU] - XYSumR[i][indexR]) * 10
            indexR += 1
        else:
            indexU += 1
    indexD = 0
    indexL = 0
    while indexL < len(XYSumL[i]) and indexD < len(XYSumD[i]):
        if XYSumD[i][indexD] > XYSumL[i][indexL]:
            indexL += 1
        else:
            if (XYSumL[i][indexL] - XYSumD[i][indexD]) * 10 < minTime:
                minTime = (XYSumL[i][indexL] - XYSumD[i][indexD]) * 10
            indexD += 1    
    indexL = 0
    indexU = 0
    while indexL < len(XYDiffL[i]) and indexU < len(XYDiffU[i]):
        if XYDiffU[i][indexU] > XYDiffL[i][indexL]:
            indexL += 1
        else:
            if (XYDiffL[i][indexL] - XYDiffU[i][indexU]) * 10 < minTime:
                minTime = (XYDiffL[i][indexL] - XYDiffU[i][indexU]) * 10
            indexU += 1
    indexR = 0
    indexD = 0
    while indexR < len(XYDiffR[i]) and indexD < len(XYDiffD[i]):
        if XYDiffR[i][indexR] > XYDiffD[i][indexD]:
            indexD += 1
        else:
            if (XYDiffD[i][indexD] - XYDiffR[i][indexR]) * 10 < minTime:
                minTime = (XYDiffD[i][indexD] - XYDiffR[i][indexR]) * 10
            indexR += 1

if minTime == 4000000:
    print("SAFE")
else:
    print(minTime)

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    xyp = [input().split() for _ in [0]*n]
    for i in range(n):
        xyp[i][0] = int(xyp[i][0])
        xyp[i][1] = int(xyp[i][1])
    inf = 10**10
    ans = inf
    # 1→u,d
    u_list = []
    d_list = []
    for i in range(n):
        if xyp[i][2] == "U":
            u_list.append((xyp[i][0], xyp[i][1]))
        if xyp[i][2] == "D":
            d_list.append((xyp[i][0], xyp[i][1]))
    li, lj = len(u_list), len(d_list)
    u_list.sort()
    d_list.sort()
    i, j = 0, 0
    while(i < li and j < lj):
        if u_list[i][0] < d_list[j][0]:
            i += 1
        elif u_list[i][0] > d_list[j][0]:
            j += 1
        else:
            if u_list[i][1] < d_list[j][1]:
                ans = min(ans, (d_list[j][1] - u_list[i][1])*5)
                i += 1
            else:
                j += 1
    # 2→r,l
    r_list = []
    l_list = []
    for i in range(n):
        if xyp[i][2] == "R":
            r_list.append((xyp[i][0], xyp[i][1]))
        if xyp[i][2] == "L":
            l_list.append((xyp[i][0], xyp[i][1]))
    li, lj = len(r_list), len(l_list)
    r_list.sort()
    l_list.sort()
    i, j = 0, 0
    while(i < li and j < lj):
        if r_list[i][1] < l_list[j][1]:
            i += 1
        elif r_list[i][1] > l_list[j][1]:
            j += 1
        else:
            if r_list[i][0] < l_list[j][0]:
                ans = min(ans, (l_list[j][0] - r_list[i][0])*5)
                i += 1
            else:
                j += 1
    # 3→u,l
    u_list = []
    l_list = []
    for i in range(n):
        if xyp[i][2] == "U":
            u_list.append((xyp[i][0], xyp[i][1]))
        if xyp[i][2] == "L":
            l_list.append((xyp[i][0], xyp[i][1]))
    li, lj = len(u_list), len(l_list)
    u_list.sort(key=lambda x: x[0]-x[1])
    l_list.sort(key=lambda x: x[0]-x[1])
    i, j = 0, 0
    while(i < li and j < lj):
        if u_list[i][0]-u_list[i][1] < l_list[j][0]-l_list[j][1]:
            i += 1
        elif u_list[i][0]-u_list[i][1] > l_list[j][0]-l_list[j][1]:
            j += 1
        else:
            if u_list[i][0] < l_list[j][0]:
                ans = min(ans, (l_list[j][0] - u_list[i][0])*10)
                i += 1
            else:
                j += 1
    # 4→d,l
    d_list = []
    l_list = []
    for i in range(n):
        if xyp[i][2] == "D":
            d_list.append((xyp[i][0], xyp[i][1]))
        if xyp[i][2] == "L":
            l_list.append((xyp[i][0], xyp[i][1]))
    li, lj = len(d_list), len(l_list)
    d_list.sort(key=lambda x: x[0]+x[1])
    l_list.sort(key=lambda x: x[0]+x[1])
    i, j = 0, 0
    while(i < li and j < lj):
        if d_list[i][0]+d_list[i][1] < l_list[j][0]+l_list[j][1]:
            i += 1
        elif d_list[i][0]+d_list[i][1] > l_list[j][0]+l_list[j][1]:
            j += 1
        else:
            if d_list[i][0] < l_list[j][0]:
                ans = min(ans, (l_list[j][0] - d_list[i][0])*10)
                i += 1
            else:
                j += 1
    # 5→u,r
    u_list = []
    r_list = []
    for i in range(n):
        if xyp[i][2] == "U":
            u_list.append((xyp[i][0], xyp[i][1]))
        if xyp[i][2] == "R":
            r_list.append((xyp[i][0], xyp[i][1]))
    li, lj = len(u_list), len(r_list)
    u_list.sort(key=lambda x: (x[0]+x[1], -x[0]))
    r_list.sort(key=lambda x: (x[0]+x[1], -x[0]))
    i, j = 0, 0
    while(i < li and j < lj):
        if u_list[i][0]+u_list[i][1] < r_list[j][0]+r_list[j][1]:
            i += 1
        elif u_list[i][0]+u_list[i][1] > r_list[j][0]+r_list[j][1]:
            j += 1
        else:
            if u_list[i][0] > r_list[j][0]:
                ans = min(ans, (u_list[i][0] - r_list[j][0])*10)
                i += 1
            else:
                j += 1
    # 6→d,r
    d_list = []
    r_list = []
    for i in range(n):
        if xyp[i][2] == "D":
            d_list.append((xyp[i][0], xyp[i][1]))
        if xyp[i][2] == "R":
            r_list.append((xyp[i][0], xyp[i][1]))
    li, lj = len(d_list), len(r_list)
    d_list.sort(key=lambda x: (x[0]-x[1], -x[0]))
    r_list.sort(key=lambda x: (x[0]-x[1], -x[0]))
    i, j = 0, 0
    while(i < li and j < lj):
        if d_list[i][0]-d_list[i][1] < r_list[j][0]-r_list[j][1]:
            i += 1
        elif d_list[i][0]-d_list[i][1] > r_list[j][0]-r_list[j][1]:
            j += 1
        else:
            if d_list[i][0] > r_list[j][0]:
                ans = min(ans, (d_list[i][0] - r_list[j][0])*10)
                i += 1
            else:
                j += 1
    if ans == inf:
        print("SAFE")
    else:
        print(ans)


main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

n, = map(int, readline().split())

M = 2*10**5+1
Uy = [[] for _ in range(M)]
Dy = [[] for _ in range(M)]

Lx = [[] for _ in range(M)]
Rx = [[] for _ in range(M)]

d1U = {} # 右上、x+y UR
d1R = {} # 右上、x+y UR
d2U = {} # 左上、x-y UL
d2L = {} # 左上、x-y UL
d3L = {} # 左下、x+y LD
d3D = {} # 左下、x+y LD
d4D = {} # 右下、x-y DR
d4R = {} # 右下、x-y DR

for _ in range(n):
    x,y,z = readline().split()
    x = int(x); y = int(y)
    if z=="U":
        Uy[x].append(y)
        dp,dn = d1U,d2U
    elif z=="D":
        Dy[x].append(y)
        dp,dn = d3D,d4D
    elif z=="R":
        Rx[y].append(x)
        dp,dn = d1R,d4R
    elif z=="L":
        Lx[y].append(x)
        dp,dn = d3L,d2L
    
    v = x+y
    if v in dp: dp[v].append(x)
    else: dp[v] = [x]
    v = x-y
    if v in dn: dn[v].append(x)
    else: dn[v] = [x]

INF = 1<<30
ans = INF

def get(a,b):
    v = INF
    la = len(a)
    lb = len(b)
    if la:
        i = j = 0
        while i < la:
            while j < lb and b[j] < a[i]:
                j += 1
            if j < lb:
                v = min(v,b[j]-a[i])
            i += 1                
    return v

def syoumen(lst1,lst2):
    v = INF
    for i in range(M):
        a = lst1[i]
        b = lst2[i]
        v = min(v,get(a,b))
        """
        a.sort()
        b.sort()
        la = len(a)
        lb = len(b)
        if la:
            i = j = 0
            while i < la:
                while j < lb and b[j] < a[i]:
                    j += 1
                if j < lb:
                    v = min(v,b[j]-a[i])
                i += 1                
        """
    return v*5

def naname(d1,d2):
    v = INF
    for i in range(M):
        if i not in d1: continue
        if i not in d2: continue
        a = d1[i]
        b = d2[i]
        v = min(v,get(a,b))
    return v*10

ans = min(ans,syoumen(Uy,Dy))
ans = min(ans,syoumen(Rx,Lx))

ans = min(ans,naname(d1R,d1U))
ans = min(ans,naname(d2U,d2L))
ans = min(ans,naname(d3D,d3L))
ans = min(ans,naname(d4R,d4D))

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

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
M = 200001
n = int(input())
l = [None] * n
ans = 10 ** 18
for i in range(n):
    x, y, u = input().split()
    u = ('R', 'U', 'L', 'D').index(u)
    l[i] = (int(x), int(y), u)
for _ in range(4):
    xy = [[] for _ in range(M * 2 - 1)]
    yx = [[] for _ in range(M * 2 - 1)]
    xs = [[] for _ in range(M)]
    k = []
    for i, t in enumerate(l):
        x, y, u = t
        u %= 4
        if u == 1:
            xy[x + y].append(x)
        elif u == 3:
            yx[x - y + M].append(x)
        elif u == 2:
            xs[y].append(x)
        else:
            k.append((x, y))
        l[i] = (-y + M - 1, x, u + 1)
    for x, y in k:
        idx = bisect_left(xs[y], x)
        if idx < len(xs[y]):
            ans = min(ans, (xs[y][idx] - x) * 5)
        idx = bisect_left(xy[x + y], x)
        if idx < len(xy[x + y]):
            ans = min(ans, (xy[x + y][idx] - x) * 10)
        idx = bisect_left(yx[x - y + M], x)
        if idx < len(yx[x - y + M]):
            ans = min(ans, (yx[x - y + M][idx] - x) * 10)
if ans == 10 ** 18:
    print('SAFE')
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict
def smin(x,y):
    if x>y:
        return y
    else:
        return x

sys.setrecursionlimit(500000)
def input():
    return sys.stdin.readline()[:-1]

def main():
    N = int(input())

    dd = defaultdict(list)
    ans = 10**27

    for i in range(N):
        x,y,u = input().split()
        x,y = int(x),int(y)
        dd[u].append((x,y))

    dd_u = defaultdict(list)

    for x,y in dd["U"]:
        dd_u[x].append((y,"U"))

    for x,y in dd["D"]:
        if x in dd_u:
            dd_u[x].append((y,"D"))

    for x in dd_u:
        dd_u[x].sort()
        for i in range(len(dd_u[x])-1):
            if dd_u[x][i][1]=="U" and dd_u[x][i+1][1]=="D":
                ans = smin((dd_u[x][i+1][0]-dd_u[x][i][0])/2,ans)

    dd_l = defaultdict(list)

    for x,y in dd["L"]:
        dd_u[y].append((x,"L"))

    for x,y in dd["R"]:
        if y in dd_u:
            dd_u[y].append((x,"R"))

    for y in dd_l:
        dd_l[y].sort()
        for i in range(len(dd_l[y])-1):
            if dd_u[y][i][1]=="R" and dd_u[y][i+1][1]=="L":
                ans = smin((dd_l[y][i+1][0]-dd_l[y][i][0])/2,ans)

    dd_v = defaultdict(list)

    for x,y in dd["U"]:
        v = y-x
        dd_v[v].append((x+y,"U"))

    for x,y in dd["L"]:
        v = y-x
        if v in dd_v:
            dd_v[v].append((x+y,"L"))

    for v in dd_v:
        dd_v[v].sort()
        for i in range(len(dd_v[v])-1):
            if dd_v[v][i][1]=="U" and dd_v[v][i+1][1]=="L":
                ans = smin((dd_v[v][i+1][0]-dd_v[v][i][0])/2,ans)

    dd_v = defaultdict(list)

    for x,y in dd["U"]:
        v = y+x
        dd_v[v].append((x-y,"U"))

    for x,y in dd["R"]:
        v = y+x
        if v in dd_v:
            dd_v[v].append((x-y,"R"))

    for v in dd_v:
        dd_v[v].sort()
        for i in range(len(dd_v[v])-1):
            if dd_v[v][i][1]=="R" and dd_v[v][i+1][1]=="U":
                ans = smin((dd_v[v][i+1][0]-dd_v[v][i][0])/2,ans)


    dd_v = defaultdict(list)

    for x,y in dd["D"]:
        v = y-x
        dd_v[v].append((x+y,"D"))

    for x,y in dd["R"]:
        v = y-x
        if v in dd_v:
            dd_v[v].append((x+y,"R"))

    for v in dd_v:
        dd_v[v].sort()
        for i in range(len(dd_v[v])-1):
            if dd_v[v][i][1]=="R" and dd_v[v][i+1][1]=="D":
                ans = smin((dd_v[v][i+1][0]-dd_v[v][i][0])/2,ans)

    dd_v = defaultdict(list)

    for x,y in dd["D"]:
        v = y+x
        dd_v[v].append((x-y,"D"))

    for x,y in dd["L"]:
        v = y+x
        if v in dd_v:
            dd_v[v].append((x-y,"L"))

    for v in dd_v:
        dd_v[v].sort()
        for i in range(len(dd_v[v])-1):
            if dd_v[v][i][1]=="D" and dd_v[v][i+1][1]=="L":
                ans = smin((dd_v[v][i+1][0]-dd_v[v][i][0])/2,ans)

    if ans>10**24:
        print("SAFE")
    else:
        print(int(ans*10))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
N = int(input())
M = 200000
miU = [200001] * (M + 10)
maD = [-1] * (M + 10)
miR = [200001] * (M + 10)
maL = [-1] * (M + 10)
RU = [[] for _ in range(4 * M + 10)]
RD = [[] for _ in range(4 * M + 10)]
LU = [[] for _ in range(4 * M + 10)]
LD = [[] for _ in range(4 * M + 10)]

for _ in range(N):
    x, y, di = input().split()
    x, y = int(x), int(y)
    if di == "R":
        miR[y] = min(miR[y], x)
        RU[x+y].append(x * 2)
        RD[x-y+2*M].append(x * 2)
    elif di == "L":
        maL[y] = max(maL[y], x)
        LU[x-y+2*M].append(x * 2 + 1)
        LD[x+y].append(x * 2 + 1)
    elif di == "U":
        miU[x] = min(miU[x], y)
        RU[x+y].append(x * 2 + 1)
        LU[x-y+2*M].append(x * 2)
    elif di == "D":
        maD[x] = max(maD[x], y)
        RD[x-y+2*M].append(x * 2 + 1)
        LD[x+y].append(x * 2)

ans = 1 << 100
for miu, mad in zip(miU, maD):
    if miu < mad:
        ans = min(ans, (mad - miu) * 5)
for mir, mal in zip(miR, maL):
    if mir < mal:
        ans = min(ans, (mal - mir) * 5)

def chk(XX):
    def subchk(X):
        t = - (1 << 100)
        re = 1 << 100
        for x in sorted(X):
            a = x >> 1
            b = x & 1
            if b == 0:
                t = a
            else:
                re = min(re, a - t)
        return re * 10
    
    RE = 1 << 100
    for xx in XX:
        RE = min(RE, subchk(xx))
    return RE

ans = min(ans, chk(RU))
ans = min(ans, chk(RD))
ans = min(ans, chk(LU))
ans = min(ans, chk(LD))

print("SAFE" if ans > 1 << 80 else ans)