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())
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])

for i in range(n):
    x = int(A[i][0])
    y = int(A[i][1])
    nn = [x,y,A[i][2]]
    D[0][x].append(nn)
    D[1][y].append(nn)
    D[2][x+y].append(nn)
    D[3][x-y+l].append(nn)

# print(D)
import bisect
ans = float("inf")
for i in range(l):
    # print(D[1][i])
    LR = [[], []]
    for j in range(len(D[1][i])):
        
        if D[1][i][j][2] == "L":
            LR[0].append(D[1][i][j][0])
        if D[1][i][j][2] == "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(l):
    # print(D[0][i])
    LR = [[], []]
    for j in range(len(D[0][i])):
        
        if D[0][i][j][2] == "U":
            LR[0].append(D[0][i][j][1])
        if D[0][i][j][2] == "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][2] == "L":
            LR[0].append(D[2][i][j][0])
        if D[2][i][j][2] == "D":
            LR[1].append(D[2][i][j][0])

        if D[2][i][j][2] == "U":
            LR2[0].append(D[2][i][j][0])
        if D[2][i][j][2] == "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][2] == "D":
            LR[0].append(D[3][i][j][1])
        if D[3][i][j][2] == "R":
            LR[1].append(D[3][i][j][1])

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

        # 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:
import sys
from collections import defaultdict


def input():
    return sys.stdin.readline().rstrip()


def main():
    N = int(input())
    UD = defaultdict(list)
    RL = defaultdict(list)
    RD = defaultdict(list)
    UL = defaultdict(list)
    LD = defaultdict(list)
    RU = defaultdict(list)
    U = []
    D = []
    R = []
    L = []

    for i in range(N):
        x, y, d = input().split()
        x = int(x)
        y = int(y)
        if d == "U":
            UD[x].append(y)
            UL[x - y].append(y)
            U.append((x,y))
        elif d == "D":
            D.append((x,y))
        elif d == "R":
            RL[y].append(x)
            RD[x - y].append(x)
            RU[x + y].append(x)
            R.append((x, y))
        elif d == "L":
            LD[x + y].append(x)
            L.append((x, y))
    ans = 10 ** 9


    while L:
        x, y = L.pop()
        rx = RL[y]
        while rx:
            xxx = rx.pop()
            if xxx <= x:
                ans = min(ans,(x - xxx) / 0.2)

        ul = UL[x - y]
        while ul:
            yyy = ul.pop()
            if yyy <= y:
                ans = min(ans,(y - yyy) / 0.1)
    while D:
        x, y = D.pop()
        ud = UD[x]
        while ud:
            yyy = ud.pop()
            if y >= yyy:
                ans = min(ans,(y - yyy) / 0.2)

        rd = RD[x - y]
        while rd:
            xxx = rd.pop()
            if xxx >= x:
                ans = min(ans,(xxx - x) / 0.1)

        ld = LD[x + y]
        while ld:
            xxx = ld.pop()
            if xxx >=x:
                ans =min(ans,(xxx-x)/0.1)

    while U:
        x,y =U.pop()
        ru = RU[x+y]
        while ru:
            xxx =ru.pop()
            if x >=xxx:
                ans =min(ans,(x-xxx)/0.1)

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
sky_mapping = {}
plane_l = []
min_x, min_y = 10**6, 10**6
max_x, max_y = 0, 0
dir_dic = {'U':(0, 1), 'D':(0, -1), 'L':(-1, 0), 'R':(1, 0)}
for i in range(N):
    x, y, u = input().split()
    plane_l.append(((int(x), int(y)), u))
    max_x = max(max_x, int(x))
    min_x = min(min_x, int(x))
    max_y = max(max_y, int(y))
    min_y = min(min_y, int(y))
result = []
for plane in plane_l:
    pos, dr = plane
    x, y = pos
    if dr == 'U':
        for i in range(max_y - y + 1):
            sky_mapping.setdefault((x, y+i), set())
            if i not in sky_mapping[(x, y+i)]:
                sky_mapping[(x, y+i)].add(i)
            else:
                result.append(i)
    if dr == 'D':
        for i in range(y - min_y + 1):
            sky_mapping.setdefault((x, y-i), set())
            if i not in sky_mapping[(x, y-i)]:
                sky_mapping[(x, y-i)].add(i)
            else:
                result.append(i)
    if dr == 'R':
        for i in range(max_x - x + 1):
            sky_mapping.setdefault((x+i, y), set())
            if i not in sky_mapping[(x+i, y)]:
                sky_mapping[(x+i, y)].add(i)
            else:
                result.append(i)
    if dr == 'L':
        for i in range(x - min_x + 1):
            sky_mapping.setdefault((x-i, y), set())
            if i not in sky_mapping[(x-i, y)]:
                sky_mapping[(x-i, y)].add(i)
            else:
                result.append(i)
if result == []:
    print('SAFE')
else:
    print(min(result)*10)

Submission status: Wrong Answer

Solution code:
n = int(input())
n1, n2 = 200001, 400001
s1, s2 = [[] for _ in range(n1)], [[] for _ in range(n1)]
s3, s4 = [[] for _ in range(n2)], [[] for _ in range(n2)]
for _ in range(n):
    x, y, u = input().split()
    x, y = int(x), int(y)
    a, b = x + y, x - y + 200000
    s1[x].append([y, u])
    s2[y].append([x, u])
    s3[a].append([b, u])
    s4[b].append([a, u])
ans = 100000000
for t in s1:
    if not t:
        continue
    a, b = -1, -1
    t.sort()
    for tt in t:
        if tt[1] == "U":
            a = tt[0]
        elif tt[1] == "D":
            b = tt[0]
        if not min(a, b) == -1 and a < b:
            ans = min(ans, 5 * (b - a))
for t in s2:
    if not t:
        continue
    a, b = -1, -1
    t.sort()
    for tt in t:
        if tt[1] == "R":
            a = tt[0]
        elif tt[1] == "L":
            b = tt[0]
        if not min(a, b) == -1 and a < b:
            ans = min(ans, 5 * (b - a))
for t in s3:
    if not t:
        continue
    a, b, c, d = -1, -1, -1, -1
    t.sort()
    for tt in t:
        if tt[1] == "U":
            a = tt[0]
        elif tt[1] == "D":
            b = tt[0]
        elif tt[1] == "R":
            c = tt[0]
        elif tt[1] == "L":
            d = tt[0]
        if not min(a, c) == -1 and a > c:
            ans = min(ans, 5 * (a - c))
        if not min(b, d) == -1 and d > b:
            ans = min(ans, 5 * (d - b))
for t in s4:
    if not t:
        continue
    a, b, c, d = -1, -1, -1, -1
    t.sort()
    for tt in t:
        if tt[1] == "U":
            a = tt[0]
        elif tt[1] == "D":
            b = tt[0]
        elif tt[1] == "R":
            c = tt[0]
        elif tt[1] == "L":
            d = tt[0]
        if not min(b, c) == -1 and c < b:
            ans = min(ans, 5 * (c - b))
        if not min(a, d) == -1 and a < d:
            ans = min(ans, 5 * (d - a))
print(ans if not ans == 100000000 else "SAFE")

Submission status: Wrong Answer

Solution code:
n=int(input())
f=list()
m=10**9
x=input()
x0=x.split()
f+=x0
for i in range(n-1):
    x=input()
    x0=x.split()
    if x0[2]=="U":
        for l in range(i+1):
            if f[3*l+2]=="D" and x0[0]==f[3*l] and int(x0[1])<int(f[3*l+1]):
                m1=(int(f[3*l+1])-int(x0[1]))*5
                if m1<m:
                    m=m1
            if f[3*l+2]=="R" and int(x0[0])-int(f[3*l])==int(f[3*l+1])-int(x0[1]) and int(x0[0])-int(f[3*l])>0:
                m1=(int(x0[0])-int(f[3*l]))*10
                if m1<m:
                    m=m1
            if f[3*l+2]=="L" and int(f[3*l])-int(x0[0])==int(f[3*l+1])-int(x0[1]) and int(x0[0])-int(f[3*l])<0:
                m1=(int(f[3*l])-int(x0[0]))*10
                if m1<m:
                    m=m1
    if x0[2]=="D":
        for l in range(i+1):
            if f[3*l+2]=="U" and x0[0]==f[3*l] and int(x0[1])>int(f[3*l+1]):
                m1=(int(f[3*l+1])-int(x0[1]))*5*(-1)
                if m1<m:
                    m=m1
            if f[3*l+2]=="R" and int(x0[0])-int(f[3*l])==(int(f[3*l+1])-int(x0[1]))*(-1) and int(x0[0])-int(f[3*l])>0:
                m1=(int(x0[0])-int(f[3*l]))*10
                if m1<m:
                    m=m1
            if f[3*l+2]=="L" and int(f[3*l])-int(x0[0])==(int(f[3*l+1])-int(x0[1]))*(-1) and int(x0[0])-int(f[3*l])<0:
                m1=(int(f[3*l])-int(x0[0]))*10
                if m1<m:
                    m=m1
    if x0[2]=="R":
        for l in range(i+1):
            if f[3*l+2]=="L" and int(x0[0])<int(f[3*l]) and int(x0[1])==int(f[3*l+1]):
                m1=(int(f[3*l])-int(x0[0]))*5
                if m1<m:
                    m=m1
            if f[3*l+2]=="U" and (int(x0[0])-int(f[3*l]))*(-1)==(int(f[3*l+1])-int(x0[1])) and int(x0[0])-int(f[3*l])<0:
                m1=(int(x0[0])-int(f[3*l]))*(-1)*10
                if m1<m:
                    m=m1
            if f[3*l+2]=="D" and int(f[3*l])-int(x0[0])==int(f[3*l+1])-int(x0[1]) and int(x0[0])-int(f[3*l])<0:
                m1=(int(f[3*l])-int(x0[0]))*10
                if m1<m:
                    m=m1
    if x0[2]=="L":
        for l in range(i+1):
            if f[3*l+2]=="R" and int(x0[0])>int(f[3*l]) and int(x0[1])==int(f[3*l+1]):
                m1=(int(f[3*l])-int(x0[0]))*5*(-1)
                if m1<m:
                    m=m1
            if f[3*l+2]=="U" and (int(x0[0])-int(f[3*l]))==(int(f[3*l+1])-int(x0[1]))*(-1) and int(x0[0])-int(f[3*l])>0:
                m1=(int(x0[0])-int(f[3*l]))*10
                if m1<m:
                    m=m1
            if f[3*l+2]=="D" and (int(f[3*l])-int(x0[0]))*(-1)==int(f[3*l+1])-int(x0[1]) and int(x0[0])-int(f[3*l])>0:
                m1=(int(f[3*l])-int(x0[0]))*(-1)*10
                if m1<m:
                    m=m1
    f+=x0
if m==10**9:
    print("SAFE")
else:    
    print(m)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 10 ** 9
upanddown = [[] for i in range(200001)]
leftandright = [[] for i in range(200001)]
lis = []
for i in range(n):
    x,y,r = map(str,input().split())
    x,y = int(x),int(y)
    lis.append([x+y,x-y,x,y,r])
    if r == "U":
        upanddown[x].append([y,0])
    elif r == "D":
        upanddown[x].append([y,1])
    elif r == "R":
        leftandright[y].append([x,0])
    else:
        leftandright[y].append([x,1])
  # print(upanddown[11])
for li in upanddown:
    if len(li) >= 2:
        li.sort(key=lambda z:z[1])
        li.sort(key=lambda z:z[0])
        key = 0
        keynum = -1
        for num,r in li:
            if r == 0:
                keynum = num
            else:
                if keynum != -1:
                    ans = min(ans,(num-keynum)*5)
for li in leftandright:
    if len(li) >= 2:
        li.sort(key=lambda z:z[1])
        li.sort(key=lambda z:z[0])
        key = 0
        keynum = -1
        for num,r in li:
            if r == 0:
                keynum = num
            else:
                if keynum != -1:
                    ans = min(ans,(num-keynum)*5)
  # print(ans)
lis.sort(key = lambda z:z[2])
lis.sort(key = lambda z:z[0])
for i in range(n-1):
    if lis[i][0] == lis[i+1][0]:
        if (lis[i][4] == "R" and lis[i+1][4] == "U") or (lis[i][4] == "D" or lis[i+1][4] == "L"):
            ans = min(ans,(lis[i+1][2]-lis[i][2])*10)
lis.sort(key = lambda z:z[2])
lis.sort(key = lambda z:z[1])
for i in range(n-1):
    if lis[i][1] == lis[i+1][1]:
        if (lis[i][4] == "U" and lis[i+1][4] == "L") or (lis[i][4] == "R" and lis[i+1][4] == "D"):
            ans = min(ans,(lis[i+1][2]-lis[i][2])*10)
if ans == 10 ** 9:
    print("SAFE")
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
sky_mapping = {}
plane_l = []
for i in range(N):
    x, y, u = input().split()
    plane_l.append(((int(x), int(y)), u))
result = []
for plane in plane_l:
    pos, dr = plane
    x, y = pos
    if dr == 'U':
        for i in range(100000 - y + 1):
            sky_mapping.setdefault((x, y+i), set())
            if i not in sky_mapping[(x, y+i)]:
                sky_mapping[(x, y+i)].add(i)
            else:
                result.append(i)
    if dr == 'D':
        for i in range(y + 1):
            sky_mapping.setdefault((x, y-i), set())
            if i not in sky_mapping[(x, y-i)]:
                sky_mapping[(x, y-i)].add(i)
            else:
                result.append(i)
    if dr == 'R':
        for i in range(100000 - x + 1):
            sky_mapping.setdefault((x+i, y), set())
            if i not in sky_mapping[(x+i, y)]:
                sky_mapping[(x+i, y)].add(i)
            else:
                result.append(i)
    if dr == 'L':
        for i in range(x + 1):
            sky_mapping.setdefault((x-i, y), set())
            if i not in sky_mapping[(x-i, y)]:
                sky_mapping[(x-i, y)].add(i)
            else:
                result.append(i)
if result == []:
    print('SAFE')
else:
    print(min(result)*10)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 10 ** 9
upanddown = [[] for i in range(200001)]
leftandright = [[] for i in range(200001)]
lis = []
for i in range(n):
    x,y,r = map(str,input().split())
    x,y = int(x),int(y)
    lis.append([x+y,x-y,x,y,r])
    if r == "U":
        upanddown[x].append([y,0])
    elif r == "D":
        upanddown[x].append([y,1])
    elif r == "R":
        leftandright[y].append([x,0])
    else:
        leftandright[y].append([x,1])
  # print(upanddown[11])
for li in upanddown:
    if len(li) >= 2:
        li.sort(key=lambda z:z[1])
        li.sort(key=lambda z:z[0])
        key = 0
        keynum = -1
        for num,r in li:
            if r == 0:
                keynum = num
            else:
                if keynum != -1:
                    ans = min(ans,num-keynum)
for li in leftandright:
    if len(li) >= 2:
        li.sort(key=lambda z:z[1])
        li.sort(key=lambda z:z[0])
        key = 0
        keynum = -1
        for num,r in li:
            if r == 0:
                keynum = num
            else:
                if keynum != -1:
                    ans = min(ans,(num-keynum)*5)
  # print(ans)
lis.sort(key = lambda z:z[2])
lis.sort(key = lambda z:z[0])
for i in range(n-1):
    if lis[i][0] == lis[i+1][0]:
        if (lis[i][4] == "R" and lis[i+1][4] == "U") or (lis[i][4] == "D" or lis[i+1][4] == "L"):
            ans = min(ans,(lis[i+1][2]-lis[i][2])*10)
lis.sort(key = lambda z:z[2])
lis.sort(key = lambda z:z[1])
for i in range(n-1):
    if lis[i][1] == lis[i+1][1]:
        if (lis[i][4] == "U" and lis[i+1][4] == "L") or (lis[i][4] == "R" and lis[i+1][4] == "D"):
            ans = min(ans,(lis[i+1][2]-lis[i][2])*10)
if ans == 10 ** 9:
    print("SAFE")
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
f=list()
m=10**9
x=input()
x0=x.split()
f+=x0
for i in range(n-1):
    x=input()
    x0=x.split()
    if x0[2]=="U":
        for l in range(i+1):
            if f[3*l+2]=="D" and x0[0]==f[3*l] and int(x0[1])<int(f[3*l+1]):
                m1=(int(f[3*l+1])-int(x0[1]))*5
                if m1<m:
                    m=m1
            if f[3*l+2]=="R" and int(x0[0])-int(f[3*l])==int(f[3*l+1])-int(x0[1]) and int(x0[0])-int(f[3*l])>0:
                m1=(int(x0[0])-int(f[3*l]))*10
                if m1<m:
                    m=m1
            if f[3*l+2]=="L" and int(f[3*l])-int(x0[0])==int(f[3*l+1])-int(x0[1]) and int(x0[0])-int(f[3*l])<0:
                m1=(int(f[3*l])-int(x0[0]))*10
                if m1<m:
                    m=m1
    if x0[2]=="D":
        for l in range(i+1):
            if f[3*l+2]=="U" and x0[0]==f[3*l] and int(x0[1])>int(f[3*l+1]):
                m1=(int(f[3*l+1])-int(x0[1]))*5*(-1)
                if m1<m:
                    m=m1
            if f[3*l+2]=="R" and int(x0[0])-int(f[3*l])==(int(f[3*l+1])-int(x0[1]))*(-1) and int(x0[0])-int(f[3*l])>0:
                m1=(int(x0[0])-int(f[3*l]))*10
                if m1<m:
                    m=m1
            if f[3*l+2]=="L" and int(f[3*l])-int(x0[0])==(int(f[3*l+1])-int(x0[1]))*(-1) and int(x0[0])-int(f[3*l])<0:
                m1=(int(f[3*l])-int(x0[0]))*10
                if m1<m:
                    m=m1
    if x0[2]=="R":
        for l in range(i+1):
            if f[3*l+2]=="L" and int(x0[0])<int(f[3*l]) and int(x0[1])==int(f[3*l+1]):
                m1=(int(f[3*l])-int(x0[0]))*5
                if m1<m:
                    m=m1
            if f[3*l+2]=="U" and (int(x0[0])-int(f[3*l]))*(-1)==(int(f[3*l+1])-int(x0[1])) and int(x0[0])-int(f[3*l])<0:
                m1=(int(x0[0])-int(f[3*l]))*(-1)*10
                if m1<m:
                    m=m1
            if f[3*l+2]=="D" and int(f[3*l])-int(x0[0])==int(f[3*l+1])-int(x0[1]) and int(x0[0])-int(f[3*l])<0:
                m1=(int(f[3*l])-int(x0[0]))*10
                if m1<m:
                    m=m1
    if x0[2]=="L":
        for l in range(i+1):
            if f[3*l+2]=="R" and int(x0[0])>int(f[3*l]) and int(x0[1])==int(f[3*l+1]):
                m1=(int(f[3*l])-int(x0[0]))*5*(-1)
                if m1<m:
                    m=m1
            if f[3*l+2]=="U" and (int(x0[0])-int(f[3*l]))*(-1)==(int(f[3*l+1])-int(x0[1]))*(-1) and int(x0[0])-int(f[3*l])<0:
                m1=(int(x0[0])-int(f[3*l]))*(-1)*10
                if m1<m:
                    m=m1
            if f[3*l+2]=="D" and (int(f[3*l])-int(x0[0]))*(-1)==int(f[3*l+1])-int(x0[1]) and int(x0[0])-int(f[3*l])>0:
                m1=(int(f[3*l])-int(x0[0]))*(-1)*10
                if m1<m:
                    m=m1
    f+=x0
if m==10**9:
    print("SAFE")
else:    
    print(m)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict


def input():
    return sys.stdin.readline().rstrip()


def main():
    N = int(input())
    UD = defaultdict(list)
    RL = defaultdict(list)
    RD = defaultdict(list)
    UL = defaultdict(list)
    LD = defaultdict(list)
    RU = defaultdict(list)
    U = []
    D = []
    R = []
    L = []

    for i in range(N):
        x, y, d = input().split()
        x = int(x)
        y = int(y)
        if d == "U":
            UD[x].append(y)
            UL[x - y].append(y)
            U.append((x,y))
        elif d == "D":
            D.append((x,y))
        elif d == "R":
            RL[y].append(x)
            RD[x - y].append(x)
            RU[x + y].append(x)
            R.append((x, y))
        elif d == "L":
            LD[x + y].append(x)
            L.append((x, y))
    ans = 10 ** 9


    while L:
        x, y = L.pop()
        rx = RL[y]
        while rx:
            xxx = rx.pop()
            if xxx <= y:
                ans = min(ans,(y - xxx) / 0.2)

        ul = UL[x - y]
        while ul:
            yyy = ul.pop()
            if yyy <= y:
                ans = min(ans,(y - yyy) / 0.1)
    while D:
        x, y = D.pop()
        ud = UD[x]
        while ud:
            yyy = ud.pop()
            if y >= yyy:
                ans = min(ans,(y - yyy) / 0.2)

        rd = RD[x - y]
        while rd:
            xxx = rd.pop()
            if xxx >= x:
                ans = min(ans,(xxx - x) / 0.1)

        ld = LD[x + y]
        while ld:
            xxx = ld.pop()
            if xxx >=x:
                ans =min(ans,(xxx-x)/0.1)

    while U:
        x,y =U.pop()
        ru = RU[x+y]
        while ru:
            xxx =ru.pop()
            if x >=xxx:
                ans =min(ans,(x-xxx)/0.1)

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


if __name__ == "__main__":
    main()

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(lambda x: x[1])
    l_list.sort(lambda x: x[1])
    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:
import sys
input = sys.stdin.readline

from collections import defaultdict

M = 200001
N = int(input())

X = [[] for _ in range(M)] #上向きのy座標, 下向きのy座標
Y = [[] for _ in range(M)]
S1 = defaultdict(list) #x+yが一致
S2 = defaultdict(list)
T1 = defaultdict(list)
T2 = defaultdict(list)

for _ in range(N):
    x, y, c = input().split()
    x = int(x)
    y = int(y)
    if c == 'U':
        c = 0
        S1[x + y].append((x - y, c))
        T1[x - y].append((x + y, c))
    elif c == 'R':
        c = 1
        S1[x + y].append((x - y, c))
        T2[x - y].append((x + y, c))
    elif c == 'D':
        c = 2
        S2[x + y].append((x - y, c))
        T2[x - y].append((x + y, c))
    else:
        c = 3
        S2[x + y].append((x - y, c))
        T1[x - y].append((x + y, c))
    if c % 2 == 0:
        X[x].append((y, c))
    else:
        Y[y].append((x, c))

res = 10**18

tmp = None
for i in range(M):
    X[i].sort()
    for j in range(len(X[i])):
        y, c = X[i][j]
        if c == 0:
            tmp = y
        if c == 2:
            if tmp is not None:
                res = min(res, (y - tmp) * 5)


for i in range(M):
    Y[i].sort()
    tmp = None
    for j in range(len(Y[i])):
        x, c = Y[i][j]
        if c == 1:
            tmp = x
        if c == 3:
            if tmp is not None:
                res = min(res, (x - tmp) * 5)


for k, v in S1.items():
    v.sort()
    tmp = None
    for j in range(len(v)):
        z, c = v[j]
        if c == 1:
            tmp = z
        if c == 0:
            if tmp is not None:
                res = min(res, (z - tmp) * 5)


for k, v in S2.items():
    v.sort()
    tmp = None
    for j in range(len(v)):
        z, c = v[j]
        if c == 2:
            tmp = z
        if c == 3:
            if tmp is not None:
                res = min(res, (z - tmp) * 5)


for k, v in T1.items():
    v.sort()
    tmp = None
    for j in range(len(v)):
        z, c = v[j]
        if c == 0:
            tmp = z
        if c == 3:
            if tmp is not None:
                res = min(res, (z - tmp) * 5)



for k, v in T2.items():
    v.sort()
    tmp = None
    for j in range(len(v)):
        z, c = v[j]
        if c == 1:
            tmp = z
        if c == 2:
            if tmp is not None:
                res = min(res, (z - tmp) * 5)

print(res if res != 10**18 else 'SAFE')

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]) * 2)
			if u[2] == 'R' and v[2] == 'D':
			 	r = min(r, f(u[0],v[0]) * 2) 

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'