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:
import sys
input = sys.stdin.readline

N = int(input())
X = [0] * N
Y = [0] * N
U = [''] * N
for i in range(N):
    x, y, u = input().split()
    X[i] = int(x)
    Y[i] = int(y)
    U[i] = u

def calc(X, Y, U):
    mx = min(X)
    for i in range(N):
        X[i] -= mx
    my = min(Y)
    for i in range(N):
        Y[i] -= my
    Mx = max(X)
    My = max(Y)
    up = [[] for _ in range(Mx+1)]
    down = [[] for _ in range(Mx+1)]
    left = [[] for _ in range(My+1)]
    right = [[] for _ in range(My+1)]
    for x, y, u in zip(X, Y, U):
        if u == 'U':
            up[x].append(y)
        if u == 'D':
            down[x].append(y)
        if u == 'L':
            left[y].append(x)
        if u == 'R':
            right[y].append(x)
    ret = float('inf')
    for x in range(Mx+1):
        up[x].sort()
        down[x].sort()
        s = t = 0
        while s < len(up[x]):
            while t < len(down[x]) and up[x][s] > down[x][t]:
                t += 1
            if t == len(down[x]):
                break
            ret = min(ret, (down[x][t] - up[x][s]) * 5)
            s += 1
    for y in range(My+1):
        right[y].sort()
        left[y].sort()
        s = t = 0
        while s < len(right[y]):
            while t < len(left[y]) and right[y][s] > left[y][t]:
                t += 1
            if t == len(left[y]):
                break
            ret = min(ret, (left[y][t] - right[y][s]) * 5)
            s += 1
    return ret

nX = [X[i] - Y[i] for i in range(N)]
nY = [X[i] + Y[i] for i in range(N)]
U1 = []
for u in U:
    if u == 'R':
        U1.append('U')
    elif u == 'L':
        U1.append('D')
    else:
        U1.append(u)
U2 = []
for u in U:
    if u == 'U':
        U2.append('L')
    elif u == 'D':
        U2.append('R')
    else:
        U2.append(u)
ans = min(calc(X, Y, U), calc(nX, nY, U1), calc(nX, nY, U2))
print(ans if ans < float('inf') else 'SAFE')

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter

ANS=1<<60

N=int(input())
L=[]
R=[]
U=[]
D=[]

for i in range(N):
    x,y,c=input().split()
    if c=="L":
        L.append((int(x),int(y)))

    if c=="R":
        R.append((int(x),int(y)))

    if c=="U":
        U.append((int(x),int(y)))

    if c=="D":
        D.append((int(x),int(y)))

L.sort(key=itemgetter(1))
R.sort(key=itemgetter(1))

indr=0
for indl in range(len(L)):
    while indr<len(R) and R[indr][1]<L[indl][1]:
        indr+=1
    if indr<len(R) and L[indl][1]==R[indr][1] and R[indr][0]<L[indl][0]:
        ANS=min(ANS,(L[indl][0]-R[indr][0])/2)

U.sort(key=itemgetter(0))
D.sort(key=itemgetter(0))

indd=0
for indu in range(len(U)):
    while indd<len(D) and D[indd][0]<U[indu][0]:
        indd+=1
    if indd<len(D) and U[indu][0]==D[indd][0] and U[indu][1]<D[indd][1]:
        ANS=min(ANS,(D[indd][1]-U[indu][1])/2)

U.sort(key=lambda x:x[0]+x[1])
R.sort(key=lambda x:x[0]+x[1])

indr=0
for indu in range(len(U)):
    while indr<len(R) and R[indr][0]+R[indr][1]<U[indu][0]+U[indu][1]:
        indr+=1
    if indr<len(R) and R[indr][0]+R[indr][1]==U[indu][0]+U[indu][1] and R[indr][0]<U[indu][0]:
        ANS=min(ANS,U[indu][0]-R[indr][0])

U.sort(key=lambda x:x[0]-x[1])
L.sort(key=lambda x:x[0]-x[1])

indl=0
for indu in range(len(U)):
    while indl<len(L) and L[indl][0]-L[indl][1]<U[indu][0]-U[indu][1]:
        indl+=1
    if indl<len(L) and L[indl][0]-L[indl][1]==U[indu][0]-U[indu][1] and L[indl][0]>U[indu][0]:
        ANS=min(ANS,L[indl][0]-U[indu][0])

D.sort(key=lambda x:x[0]-x[1])
R.sort(key=lambda x:x[0]-x[1])

indr=0
for indd in range(len(D)):
    while indr<len(R) and R[indr][0]-R[indr][1]<D[indd][0]-D[indd][1]:
        indr+=1
    if indr<len(R) and R[indr][0]-R[indr][1]==D[indd][0]-D[indd][1] and R[indr][0]<D[indd][0]:
        ANS=min(ANS,D[indd][0]-R[indr][0])

D.sort(key=lambda x:x[0]+x[1])
L.sort(key=lambda x:x[0]+x[1])

indl=0
for indd in range(len(D)):
    while indl<len(L) and L[indl][0]+L[indl][1]<D[indd][0]+D[indd][1]:
        indl+=1
    if indl<len(L) and L[indl][0]+L[indl][1]==D[indd][0]+D[indd][1] and L[indl][0]>D[indd][0]:
        ANS=min(ANS,L[indl][0]-D[indd][0])


if ANS==1<<60:
    print("SAFE")
else:
    print(int(ANS*10))

Submission status: Wrong Answer

Solution code:
def get_collision(A,ans):
    A.sort()
    nowkey = -10**10
    prevcoor = -10**10
    ret = ans[0]
    for key, coor, a in A:
        if key != nowkey:
            if a == 1:
                nowkey = key
                prevcoor = coor
                continue
            else:
                continue
        if a == 1:
            prevcoor = coor
            continue
        if (coor - prevcoor)*5 < ret:
            ret = (coor - prevcoor)*5
    ans[0] = ret

def main():
    N = int( input())
    XYU = [ tuple( input().split()) for _ in range(N)]
    U = []
    R = []
    D = []
    L = []
    for x, y, u in XYU:
        x, y = int(x), int(y)
        if u == "U":
            U.append((x,y))
        elif u == "D":
            D.append((x,y))
        elif u == "L":
            L.append((x,y))
        else:
            R.append((x,y))
    ans = [10**10]

    UD = [(x,y,1) for x, y in U] + [(x,y,-1) for x, y in D]
    get_collision(UD, ans)
    LR = [(x,y,1) for x, y in R] + [(x,y,-1) for x, y in L]
    get_collision(LR, ans)
    RU = [(x+y,x-y,1) for x, y in R] + [(x+y,x-y,-1) for x,y in U]
    get_collision(RU, ans)
    LD = [(x+y,x-y,1) for x, y in D] + [(x+y,x-y,-1) for x,y in L]
    get_collision(LD,ans)
    LU = [(x-y,x+y,1) for x,y in U] + [(x-y,x+y,-1) for x,y in L]
    get_collision(LU,ans)
    RD = [(x-y,x+y,1) for x,y in R] + [(x-y,x+y,-1) for x,y in D]
    get_collision(RD,ans)
    if ans[0] < 10**10:
        print(ans[0])
    else:
        print("SAFE")
        
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter

ANS=1<<60

N=int(input())
L=[]
R=[]
U=[]
D=[]

for i in range(N):
    x,y,c=input().split()
    if c=="L":
        L.append((int(x),int(y)))

    if c=="R":
        R.append((int(x),int(y)))

    if c=="U":
        U.append((int(x),int(y)))

    if c=="D":
        D.append((int(x),int(y)))

L.sort(key=itemgetter(1))
R.sort(key=itemgetter(1))

indr=0
for indl in range(len(L)):
    while indr<len(R) and R[indr][1]<L[indl][1]:
        indr+=1
    if indr<len(R) and L[indl][1]==R[indr][1] and R[indr][0]<L[indl][0]:
        ANS=min(ANS,(L[indl][0]-R[indr][0])/2)

U.sort(key=itemgetter(0))
D.sort(key=itemgetter(0))

indd=0
for indu in range(len(U)):
    while indd<len(D) and D[indd][0]<U[indu][0]:
        indd+=1
    if indd<len(D) and U[indu][0]==D[indd][0] and U[indu][1]<D[indd][1]:
        ANS=min(ANS,(D[indd][1]-U[indu][1])/2)

U.sort(key=lambda x:x[0]+x[1])
R.sort(key=lambda x:x[0]+x[1])

indr=0
for indu in range(len(U)):
    while indr<len(R) and R[indr][0]+R[indr][1]<U[indu][0]+U[indu][1]:
        indr+=1
    if indr<len(R) and R[indr][0]+R[indr][1]==U[indu][0]+U[indu][1] and R[indr][0]<U[indu][0]:
        ANS=min(ANS,U[indu][0]-R[indr][0])

U.sort(key=lambda x:x[0]-x[1])
L.sort(key=lambda x:x[0]-x[1])

indl=0
for indu in range(len(U)):
    while indl<len(L) and L[indl][0]-L[indl][1]<U[indu][0]-U[indu][1]:
        indl+=1
    if indl<len(L) and L[indl][0]-L[indl][1]==U[indu][0]-U[indu][1] and L[indl][0]>U[indu][0]:
        ANS=min(ANS,L[indl][0]-U[indu][0])

D.sort(key=lambda x:x[0]+x[1])
R.sort(key=lambda x:x[0]+x[1])

indr=0
for indd in range(len(D)):
    while indr<len(R) and R[indr][0]-R[indr][1]<D[indd][0]-D[indd][1]:
        indr+=1
    if indr<len(R) and R[indr][0]-R[indr][1]==D[indd][0]-D[indd][1] and R[indr][0]<D[indd][0]:
        ANS=min(ANS,D[indd][0]-R[indr][0])

D.sort(key=lambda x:x[0]+x[1])
L.sort(key=lambda x:x[0]+x[1])

indl=0
for indd in range(len(D)):
    while indl<len(L) and L[indl][0]+L[indl][1]<D[indd][0]+D[indd][1]:
        indl+=1
    if indl<len(L) and L[indl][0]+L[indl][1]==D[indd][0]+D[indd][1] and L[indl][0]>D[indd][0]:
        ANS=min(ANS,L[indl][0]-D[indd][0])


if ANS==1<<60:
    print("SAFE")
else:
    print(int(ANS*10))

Submission status: Wrong Answer

Solution code:
def get_collision(A,ans):
    A.sort()
    nowkey = -10**10
    prevcoor = -10**10
    ret = ans[0]
    for key, coor, a in A:
        if key != nowkey:
            if a == 1:
                nowkey = key
                prevcoor = coor
                continue
            else:
                continue
        if a == 1:
            prevcoor = coor
            continue
        if (coor - prevcoor)*5 < ret:
            ret = (coor - prevcoor)*5
    ans[0] = ret

def main():
    N = int( input())
    XYU = [ tuple( input().split()) for _ in range(N)]
    U = []
    R = []
    D = []
    L = []
    for x, y, u in XYU:
        x, y = int(x), int(y)
        if u == "U":
            U.append((x,y))
        elif u == "D":
            D.append((x,y))
        elif u == "L":
            L.append((x,y))
        else:
            R.append((x,y))
    ans = [10**10]

    UD = [(x,y,1) for x, y in U] + [(x,y,-1) for x, y in D]
    get_collision(UD, ans)
    LR = [(x,y,1) for x, y in R] + [(x,y,-1) for x, y in L]
    get_collision(LR, ans)
    RU = [(x+y,x-y,1) for x, y in R] + [(x+y,y-x,-1) for x,y in U]
    get_collision(RU, ans)
    LD = [(x+y,x-y,1) for x, y in D] + [(x+y,x-y,-1) for x,y in L]
    get_collision(LD,ans)
    LU = [(x-y,x+y,1) for x,y in U] + [(x-y,x+y,-1) for x,y in L]
    get_collision(LU,ans)
    RD = [(x-y,x+y,1) for x,y in R] + [(x-y,x+y,-1) for x,y in D]
    get_collision(RD,ans)
    if ans[0] < 10**10:
        print(ans[0])
    else:
        print("SAFE")
        
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def get_collision(A,ans):
    A.sort()
    nowkey = -10**10
    prevcoor = -10**10
    ret = ans[0]
    for key, coor, a in A:
        if key != nowkey:
            if a == 1:
                nowkey = key
                prevcoor = coor
                continue
            else:
                continue
        if a == 1:
            prevcoor = coor
            continue
        if (coor - prevcoor)*10 < ret:
            ret = (coor - prevcoor)*10
    ans[0] = ret

def main():
    N = int( input())
    XYU = [ tuple( input().split()) for _ in range(N)]
    U = []
    R = []
    D = []
    L = []
    for x, y, u in XYU:
        x, y = int(x), int(y)
        if u == "U":
            U.append((x,y))
        elif u == "D":
            D.append((x,y))
        elif u == "L":
            L.append((x,y))
        else:
            R.append((x,y))
    ans = [10**10]

    UD = [(x,y,1) for x, y in U] + [(x,y,-1) for x, y in D]
    get_collision(UD, ans)
    LR = [(x,y,1) for x, y in R] + [(x,y,-1) for x, y in L]
    get_collision(LR, ans)
    RU = [(x+y,x-y,1) for x, y in R] + [(x+y,y-x,-1) for x,y in U]
    get_collision(RU, ans)
    LD = [(x+y,x-y,1) for x, y in D] + [(x+y,x-y,-1) for x,y in L]
    get_collision(LD,ans)
    LU = [(x-y,x+y,1) for x,y in U] + [(x-y,x+y,-1) for x,y in L]
    get_collision(LU,ans)
    RD = [(x-y,x+y,1) for x,y in R] + [(x-y,x+y,-1) for x,y in D]
    get_collision(RD,ans)
    if ans[0] < 10**10:
        print(ans[0])
    else:
        print("SAFE")
    
    
        
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
X = [0] * N
Y = [0] * N
U = [''] * N
for i in range(N):
    x, y, u = input().split()
    X[i] = int(x)
    Y[i] = int(y)
    U[i] = u

def calc(X, Y, U):
    mx = min(X)
    for i in range(N):
        X[i] -= mx
    my = min(Y)
    for i in range(N):
        Y[i] -= my
    Mx = max(X)
    My = max(Y)
    up = [[] for _ in range(Mx+1)]
    down = [[] for _ in range(Mx+1)]
    left = [[] for _ in range(My+1)]
    right = [[] for _ in range(My+1)]
    for x, y, u in zip(X, Y, U):
        if u == 'U':
            up[x].append(y)
        if u == 'D':
            down[x].append(y)
        if u == 'L':
            left[y].append(x)
        if u == 'R':
            right[y].append(x)
    ret = float('inf')
    for x in range(Mx+1):
        up[x].sort()
        down[x].sort()
        s = t = 0
        while s < len(up[x]):
            while t < len(down[x]) and up[x][s] > down[x][t]:
                t += 1
            if t == len(down[x]):
                break
            ret = min(ret, (down[x][t] - up[x][s]) * 5)
            s += 1
    for y in range(My+1):
        right[y].sort()
        left[y].sort()
        s = t = 0
        while s < len(right[y]):
            while t < len(left[y]) and right[y][s] > left[y][t]:
                t += 1
            if t == len(left[y]):
                break
            ret = min(ret, (left[y][t] - right[y][s]) * 5)
            s += 1
    return ret


ans = calc(X, Y, U)
nX = [X[i] - Y[i] for i in range(N)]
nY = [X[i] + Y[i] for i in range(N)]
U1 = []
for u in U:
    if u == 'R':
        U1.append('U')
    elif u == 'L':
        U1.append('D')
    else:
        U1.append(u)
U2 = []
for u in U:
    if u == 'U':
        U2.append('L')
    elif u == 'D':
        U2.append('R')
    else:
        U2.append(u)
ans = min(calc(X, Y, U), calc(nX, nY, U1), calc(nX, nY, U2))
print(ans if ans < float('inf') else 'SAFE')

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
INF = float("inf")

N = int(input())
U = [];  D = [];  R = [];  L = [];
for i in range(N):
    x, y, d = input().split()
    x = int(x)
    y = int(y)
    if d == 'U':
        U.append((x, y, x + y, x - y))
    elif d == 'D':
        D.append((x, y, x + y, x - y))
    elif d == 'R':
        R.append((x, y, x + y, x - y))
    else:
        L.append((x, y, x + y, x - y))

ans = INF

# UとD
DD = [(D[k][0], D[k][1]) for k in range(len(D))]
DD.sort()
for (x, y, xpy, xmy) in U:
    i = bisect_left(DD, (x, y))
    if i < len(DD) and DD[i][0] == x:
        ans = min(ans, (DD[i][1] - y) * 5)

# RとL
LL = [(L[k][1], L[k][0]) for k in range(len(L))]
LL.sort()
for (x, y, xpy, xmy) in R:
    i = bisect_left(LL, (y, x))
    if i < len(LL) and LL[i][0] == y:
        ans = min(ans, (LL[i][1] - x) * 5)

# UとR
RR = [(R[k][2], R[k][1]) for k in range(len(R))]  # (x + y, y)
RR.sort()
for (x, y, xpy, xmy) in U:
    i = bisect_left(RR, (x + y, y))
    if i < len(RR) and RR[i][0] == xpy:
        ans = min(ans, (RR[i][1] - y) * 10)

# DとL
LL = [(L[k][2], L[k][0]) for k in range(len(L))]  # (x + y, x)
LL.sort()
for (x, y, xpy, xmy) in D:
    i = bisect_left(LL, (x + y, x))
    if i < len(LL) and LL[i][0] == xpy:
        ans = min(ans, (LL[i][1] - x) * 10)

# UとL
LL = [(L[k][3], L[k][0]) for k in range(len(L))]  # (x - y, x)
LL.sort()
for (x, y, xpy, xmy) in U:
    i = bisect_left(LL, (x - y, x))
    if i < len(LL) and LL[i][0] == xmy:
        ans = min(ans, (LL[i][1] - x) * 10)

# DとR
RR = [(R[k][3], R[k][1]) for k in range(len(R))]  # (x - y, y)
for (x, y, xpy, xmy) in D:
    i = bisect_left(RR, (x - y, y))
    if 0 < i and RR[i - 1][0] == xmy:
        ans = min(ans, (y - RR[i - 1][1]) * 10)

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

Submission status: Wrong Answer

Solution code:
def input(): return next(input_sample)


input_sample = iter("""
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
""".split("\n")[1:-1])


def main():
    def calc(v, w, i, j, ans):
        if v > 0:
            i[0] += 1
        elif v < 0:
            j[0] += 1
        else:
            if w > 0:
                ans[0] = min(ans[0], w)
                i[0] += 1
            else:
                j[0] += 1

    n = int(input())
    inf = 10**10
    ans = [inf]
    u_list, d_list, l_list, r_list = [], [], [], []
    for _ in range(n):
        x, y, d = input().split()
        x, y = int(x), int(y)
        if d == "D":
            d_list.append((x, y))
        elif d == "R":
            r_list.append((x, y))
        elif d == "L":
            l_list.append((x, y))
        else:
            u_list.append((x, y))
    ld, lr, ll, lu = len(d_list), len(r_list), len(l_list), len(u_list)
    # 1→u,d
    u_list.sort()
    d_list.sort()
    i, j = [0], [0]
    while(i[0] < lu and j[0] < ld):
        x, y = u_list[i[0]]
        p, q = d_list[j[0]]
        v = p-x
        w = (q-y)*5
        calc(v, w, i, j, ans)
    # 2→r,l
    r_list.sort(key=lambda x: x[1])
    l_list.sort(key=lambda x: x[1])
    i, j = [0], [0]
    while(i[0] < lr and j[0] < ll):
        x, y = r_list[i[0]]
        p, q = l_list[j[0]]
        v = q-y
        w = (p-x)*5
        calc(v, w, i, j, ans)
    # 3→u,l
    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[0] < lu and j[0] < ll):
        x, y = u_list[i[0]]
        p, q = l_list[j[0]]
        v = (p-q)-(x-y)
        w = (p-x)*10
        calc(v, w, i, j, ans)
    # 4→d,l
    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[0] < ld and j[0] < ll):
        x, y = d_list[i[0]]
        p, q = l_list[j[0]]
        v = (p+q)-(x+y)
        w = (p-x)*10
        calc(v, w, i, j, ans)
    # 5→u,r
    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[0] < lu and j[0] < lr):
        x, y = u_list[i[0]]
        p, q = r_list[j[0]]
        v = (p+q)-(x+y)
        w = (x-p)*10
        calc(v, w, i, j, ans)
    # 6→d,r
    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[0] < ld and j[0] < lr):
        x, y = d_list[i[0]]
        p, q = r_list[j[0]]
        v = (p-q)-(x-y)
        w = (x-p)*10
        calc(v, w, i, j, ans)
    if ans == [inf]:
        print(1/0)
    else:
        print(ans[0])


main()

Submission status: Wrong Answer

Solution code:
print("SAFE")

Submission status: Wrong Answer

Solution code:
def main():
    def calc(v, w, i, j, ans):
        if v > 0:
            i[0] += 1
        elif v < 0:
            j[0] += 1
        else:
            if w > 0:
                ans[0] = min(ans[0], w)
                i[0] += 1
            else:
                j[0] += 1

    n = int(input())
    inf = 10**10
    ans = [inf]
    u_list, d_list, l_list, r_list = [], [], [], []
    for _ in range(n):
        x, y, d = input().split()
        x, y = int(x), int(y)
        if d == "D":
            d_list.append((x, y))
        elif d == "R":
            r_list.append((x, y))
        elif d == "L":
            l_list.append((x, y))
        else:
            u_list.append((x, y))
    ld, lr, ll, lu = len(d_list), len(r_list), len(l_list), len(u_list)
    # 1→u,d
    u_list.sort()
    d_list.sort()
    i, j = [0], [0]
    while(i[0] < lu and j[0] < ld):
        x, y = u_list[i[0]]
        p, q = d_list[j[0]]
        v = p-x
        w = (q-y)*5
        calc(v, w, i, j, ans)
    # 2→r,l
    r_list.sort(key=lambda x: x[1])
    l_list.sort(key=lambda x: x[1])
    i, j = [0], [0]
    while(i[0] < lr and j[0] < ll):
        x, y = r_list[i[0]]
        p, q = l_list[j[0]]
        v = q-y
        w = (p-x)*5
        calc(v, w, i, j, ans)
    # 3→u,l
    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[0] < lu and j[0] < ll):
        x, y = u_list[i[0]]
        p, q = l_list[j[0]]
        v = (p-q)-(x-y)
        w = (p-x)*10
        calc(v, w, i, j, ans)
    # 4→d,l
    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[0] < ld and j[0] < ll):
        x, y = d_list[i[0]]
        p, q = l_list[j[0]]
        v = (p+q)-(x+y)
        w = (p-x)*10
        calc(v, w, i, j, ans)
    # 5→u,r
    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[0] < lu and j[0] < lr):
        x, y = u_list[i[0]]
        p, q = r_list[j[0]]
        v = (p+q)-(x+y)
        w = (x-p)*10
        calc(v, w, i, j, ans)
    # 6→d,r
    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[0] < ld and j[0] < lr):
        x, y = d_list[i[0]]
        p, q = r_list[j[0]]
        v = (p-q)-(x-y)
        w = (x-p)*10
        calc(v, w, i, j, ans)
    if ans == [inf]:
        print("SAFE")
    else:
        print(ans[0])


main()

Submission status: Wrong Answer

Solution code:
# import io
# import sys

# _INPUT = """\
# 2
# 2 1 U
# 3 2 L

# 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


# """
# sys.stdin = io.StringIO(_INPUT)
####################################################################
import sys
def _p(*_a):
  return
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
####################################################################
def _pa(_aa):
  return
  for _a in _aa: _p(_a)
####################################################################
N = int(input())

X=[]
Y=[]
U=[]

from collections import defaultdict
dAU = defaultdict(list)
dAD = defaultdict(list)
dAL = defaultdict(list)
dAR = defaultdict(list)

dBU = defaultdict(list)
dBD = defaultdict(list)
dBL = defaultdict(list)
dBR = defaultdict(list)

dXU = defaultdict(list)
dXD = defaultdict(list)
# dXR = defaultdict(list)
# dXL = defaultdict(list)

# dYU = defaultdict(list)
# dYD = defaultdict(list)
dYR = defaultdict(list)
dYL = defaultdict(list)

# for k, v in s:
#   d[k].append(v)

# print(sorted(d.items()))	#=> [('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]



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

  X.append(x)
  Y.append(y)
  U.append(u)

  a = y-x
  b = x+y
  if u=="U":
    dAU[a].append((x,y,u,i))
    dBU[b].append((x,y,u,i))
    dXU[x].append((x,y,u,i))
  if u=="D":
    dAD[a].append((x,y,u,i))
    dBD[b].append((x,y,u,i))
    dXD[x].append((x,y,u,i))
  if u=="L":
    dAL[a].append((x,y,u,i))
    dBL[b].append((x,y,u,i))
    dYL[y].append((x,y,u,i))
  if u=="R":
    dAR[a].append((x,y,u,i))
    dBR[b].append((x,y,u,i))
    dYR[y].append((x,y,u,i))

ans = []
L = [ [dXU, dXD], [dYL,dYR]]
for d1, d2 in L:
  for k,v in d1.items():
    _p("d1, k,v=",k,v)
    for v1 in v:
      _p("d1, k,v1=",k,v1)
      for v2 in d2[k]:
        _p("d2, k,v2=",k,v2)
        x1,y1,u1,i1 = v1
        x2,y2,u2,i2 = v2
        if u1=="U" and u2=="D":
          if y1 > y2: continue
          a = abs(y1 - y2)//2
        if u1=="L" and u2=="R":
          if x1 < x2: continue
          a = abs(x1 - x2)//2
        _p("a=",a)
        ans.append( a )

_p("ans=", ans)

L = [ [dAU,dAL], [dAD,dAR], [dBU,dBR], [dBD,dBL] ]

for d1, d2 in L:
  _p("---")
  for k,v in d1.items():
    _p("d1, k,v=",k,v)
    for v1 in v:
      _p("d1, k,v1=",k,v1)
      for v2 in d2[k]:
        _p("d2, k,v2=",k,v2)
        x1,y1,u1,i1 = v1
        x2,y2,u2,i2 = v2
        if u1=="D" and u2=="L":
          if x1 > x2: continue
        if u1=="D" and u2=="R":
          if x1 < x2: continue
        if u1=="U" and u2=="L":
          if x1 > x2: continue
        if u1=="U" and u2=="R":
          if x1 < x2: continue
        a = abs(x1 - x2)
        _p("a=",a)
        ans.append( a )

        _p("   ---")

_p("ans=",ans)

if len(ans)==0:
  print("SAFE")
else:
  ans.sort()
  for a in ans:
    print(a*10)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
XYU = [list(map(str,input().split())) for _ in range(N)]
from collections import deque
INF = float('inf')

UD = [deque() for _ in range(200001)]
LR = [deque() for _ in range(200001)]
UR = [deque() for _ in range(400001)]
DL = [deque() for _ in range(400001)]


for X,Y,u in XYU:
  X = int(X)
  Y = int(Y)
  if u=='R':
    LR[Y].append((X,u))
    UR[X+Y].append((Y,u))
  elif u=='L':
    LR[Y].append((X,u))
    DL[X+Y].append((Y,u))
  elif u=='U':
    UD[X].append((Y,u))
    UR[X+Y].append((Y,u))
  else:
    UD[X].append((Y,u))
    DL[X+Y].append((Y,u))
    
safe = True
ans = INF
for l in UD:
  length = len(l)
  if length>=2:
    l = sorted(l)
    for i in range(length - 1):
      a = l[i]
      b = l[i+1]
      if (a[1]=='U')and(b[1]=='D'):
        safe = False
        ans = min(ans,(b[0] - a[0])*5)
        
for l in LR:
  length = len(l)
  if length>=2:
    l = sorted(l)
    for i in range(length - 1):
      a = l[i]
      b = l[i+1]
      if (a[1]=='R')and(b[1]=='L'):
        safe = False
        ans = min(ans,(b[0] - a[0])*5)
        
        
for l in UR:
  length = len(l)
  if length>=2:
    l = sorted(l)
    for i in range(length - 1):
      a = l[i]
      b = l[i+1]
      if (a[1]=='U')and(b[1]=='R'):
        safe = False
        ans = min(ans,(b[0] - a[0]) * 10)
        
for l in DL:
  length = len(l)
  if length>=2:
    l = sorted(l)
    for i in range(length - 1):
      a = l[i]
      b = l[i+1]
      if (a[1]=='L')and(b[1]=='D'):
        safe = False
        ans = min(ans,(b[0] - a[0]) * 10)
          
if safe:
  print('SAFE')
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
XYU = [list(map(str,input().split())) for _ in range(N)]
from collections import deque
INF = float('inf')

UD = [deque() for _ in range(200001)]
LR = [deque() for _ in range(200001)]
UR = [deque() for _ in range(400001)]
DL = [deque() for _ in range(400001)]


for X,Y,u in XYU:
  X = int(X)
  Y = int(Y)
  if u=='R':
    LR[Y].append((X,u))
    UR[X+Y].append((Y,u))
  elif u=='L':
    LR[Y].append((X,u))
    DL[X+Y].append((Y,u))
  elif u=='U':
    UD[X].append((Y,u))
    UR[X+Y].append((Y,u))
  else:
    UD[X].append((Y,u))
    DL[X+Y].append((Y,u))
    
safe = True
ans = INF
for l in UD:
  length = len(l)
  if length>=2:
    l = sorted(l)
    for i in range(length - 1):
      a = l[i]
      b = l[i+1]
      if (a[1]=='U')and(b[1]=='D'):
        safe = False
        ans = min(ans,(b[0] - a[0])*5)
        
for l in LR:
  length = len(l)
  if length>=2:
    l = sorted(l)
    for i in range(length - 1):
      a = l[i]
      b = l[i+1]
      if (a[1]=='R')and(b[1]=='L'):
        safe = False
        ans = min(ans,(b[0] - a[0])*5)
        
        
for l in UR:
  length = len(l)
  if length>=2:
    l = sorted(l)
    for i in range(length - 1):
      a = l[i]
      b = l[i+1]
      if (a[1]=='U')and(b[1]=='R'):
        safe = False
        ans = min(ans,(b[0] - a[0]) * 10)
        
for l in DL:
  length = len(l)
  if length>=2:
    l = sorted(l)
    for i in range(length - 1):
      a = l[i]
      b = l[i+1]
      if (a[1]=='L')and(b[1]=='D'):
        safe = False
        ans = min(ans,(b[0] - a[0]) * 10)
          
if safe:
  print('SAFE')
else:
  print(ans)