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 = []
min_crash = 2e5
for _ in range(n):
    x, y, u = list(input().split())
    x = int(x)
    y = int(y)
    a_list.append([x, y, u])

min_crash = 2e5
x_sort = sorted(a_list, key=lambda a: a[0])
y_sort = sorted(a_list, key=lambda a: a[1])
for i in range(n):
    if i < n-1 and x_sort[i][2] == "R":
        for x, y, u in x_sort[i+1:]:
            if (y < x_sort[i][1] and u == "U") or (y > x_sort[i][1] and u == "D"):
                if abs(x_sort[i][0]-x) == abs(x_sort[i][1] - y):
                    crash = x_sort[i][0]-x
                    min_crash = min(min_crash, crash)
            elif y == x_sort[i][1] and u == "L":
                crash = (x - x_sort[i][0])/2
                min_crash = min(min_crash, crash)
    elif i != 0 and x_sort[i][2] == "L":
        for x, y, u in x_sort[:i]:
            if (y < x_sort[i][1] and u == "U") or (y > x_sort[i][1] and u == "D"):
                if abs(x_sort[i][0]-x) == abs(x_sort[i][1] - y):
                    crash = x_sort[i][0]-x
                    min_crash = min(min_crash, crash)
            elif y == x_sort[i][1] and u == "R":
                crash = (x_sort[i][0] - x)/2
                min_crash = min(min_crash, crash)
    
    if i < n-1 and y_sort[i][2] == "U":
        for x, y, u in y_sort[i+1:]:
            if (x < y_sort[i][0] and u == "R") or (x > y_sort[i][0] and u == "L"):
                if abs(y_sort[i][1]-y) == abs(y_sort[i][0] - x):
                    crash = y_sort[i][1]-y
                    min_crash = min(min_crash, crash)
            elif x == y_sort[i][0] and u == "D":
                crash = (y - y_sort[i][1])/2
                min_crash = min(min_crash, crash)
    elif i != 0 and y_sort[i][2] == "D":
        for x, y, u in y_sort[:i]:
            if (x < y_sort[i][0] and u == "R") or (x > y_sort[i][0] and u == "L"):
                if abs(y_sort[i][1]-y) == abs(y_sort[i][0] - x):
                    crash = y_sort[i][1]-y
                    min_crash = min(min_crash, crash)
            elif x == y_sort[i][0] and u == "U":
                crash = (y_sort[i][1] - y)/2
                min_crash = min(min_crash, crash)


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

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+2
Uy = [[] for _ in range(M)]
Dy = [[] for _ in range(M)]

Rx = [[] for _ in range(M)]
Lx = [[] 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]

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))
    return v*5

def naname(d1,d2):
    v = INF
    for i in range(-2*M,2*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

INF = 1<<30
ans = INF

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)

"""
print(d1R,d1U)
print(d2U,d2L)
print(d3D,d3L)
print(d4R,d4D)
"""

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
A = []
buf = {
  "U":{
  	"H":{},
    "V":{},
    "P":{},
    "N":{}
  },
  "R":{
  	"H":{},
    "V":{},
    "P":{},
    "N":{}},
  "D":{
  	"H":{},
    "V":{},
    "P":{},
    "N":{}},
  "L":{
  	"H":{},
    "V":{},
    "P":{},
    "N":{}},
}
for _ in range(N):
  X, Y, U = input().split()
  X = int(X)
  Y = int(Y)
  A.append([X, Y, U])
  if X in buf[U]["H"]:
    buf[U]["H"][X].append(Y)
  else:
    buf[U]["H"][X] = [Y]
  if Y in buf[U]["V"]:
    buf[U]["V"][Y].append(X)
  else:
    buf[U]["V"][Y] = [X]
  if Y-X in buf[U]["P"]:
    buf[U]["P"][Y-X].append(X)
  else:
    buf[U]["P"][Y-X] = [X]
  if X + Y in buf[U]["N"]:
    buf[U]["N"][X + Y].append(X)
  else:
    buf[U]["N"][X + Y] = [X]

for U in "URDL":
  for H in "HVPN":
    for X in buf[U][H]:
      buf[U][H][X].sort()
      buf[U][H][X] = np.array(buf[U][H][X])
#print(buf)
ans = 1e10
for i in range(N):
  X, Y, U = A[i]
  bufans = 1e10
  if U=="U":
    if X in buf["D"]["H"]:
      bufans = min(bufans, np.abs(buf["D"]["H"][X]-Y).min()*5)
    if Y-X in buf["L"]["P"]:
      bufans = min(bufans, np.abs(buf["L"]["P"][Y-X]-X).min()*10)
    if Y+X in buf["R"]["N"]:
      bufans = min(bufans, np.abs(buf["R"]["N"][Y+X]-X).min()*10)
  elif U=="D":
    if X in buf["U"]["H"]:
      bufans = min(bufans, np.abs(buf["U"]["H"][X]-Y).min()*5)
    if Y-X in buf["R"]["P"]:
      bufans = min(bufans, np.abs(buf["R"]["P"][Y-X]-X).min()*10)
    if Y+X in buf["L"]["N"]:
      bufans = min(bufans, np.abs(buf["L"]["N"][Y+X]-X).min()*10)
  elif U=="R":
    if Y in buf["L"]["V"]:
      bufans = min(bufans, np.abs(buf["L"]["V"][Y]-X).min()*5)
    if Y-X in buf["D"]["P"]:
      bufans = min(bufans, np.abs(buf["D"]["P"][Y-X]-X).min()*10)
    if Y+X in buf["U"]["N"]:
      bufans = min(bufans, np.abs(buf["U"]["N"][Y+X]-X).min()*10)
  else:
    if Y in buf["R"]["V"]:
      bufans = min(bufans, np.abs(buf["R"]["V"][Y]-X).min()*5)
    if Y-X in buf["U"]["P"]:
      bufans = min(bufans, np.abs(buf["U"]["P"][Y-X]-X).min()*10)
    if Y+X in buf["D"]["N"]:
      bufans = min(bufans, np.abs(buf["D"]["N"][Y+X]-X).min()*10)
  ans = min(bufans, ans)
if ans == 1e10:
  print("SAFE")
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
x = [[] for _ in range(200001)]
y = [[] for _ in range(200001)]
z = [[] for _ in range(400001)]
t = float("inf")
for _ in range(n):
  xi,yi,ui = map(str,input().split())
  xi = int(xi)
  yi = int(yi)
  if ui == "U":
    x[xi].append((yi,1))
    z[xi+yi].append((xi,-1))
  elif ui == "D":
    x[xi].append((yi,-1))
    z[xi+yi].append((xi,1))
  elif ui == "R":
    y[yi].append((xi,1))
    z[xi+yi].append((xi,1))
  else:
    y[yi].append((xi,-1))
    z[xi+yi].append((xi,-1))
for xx in x:
  if len(xx) >= 2:
    xx.sort(key=lambda a:a[0])
    n = -1
    for i in range(len(xx)):
      if xx[i][1] == 1:
        n = xx[i][0]
      else:
        if n != -1:
          if (xx[i][0] - n)*5 < t:
            t = (xx[i][0] - n)*5
          n = -1 
for xx in y:
  if len(xx) >= 2:
    xx.sort(key=lambda a:a[0])
    n = -1
    for i in range(len(xx)):
      if xx[i][1] == 1:
        n = xx[i][0]
      else:
        if n != -1:
          if (xx[i][0] - n)*5 < t:
            t = (xx[i][0] - n)*5
          n = -1 
for xx in z:
  if len(xx) >= 2:
    xx.sort(key=lambda a:a[0])
    n = -1
    for i in range(len(xx)):
      if xx[i][1] == 1:
        n = xx[i][0]
      else:
        if n != -1:
          if (xx[i][0] - n)*10 < t:
            t = (xx[i][0] - n)*10
          n = -1                
if t == float("inf"):
  print("SAFE")
else:
  print(t)

Submission status: Wrong Answer

Solution code:
import collections
import heapq
def main():

    N = int(input())

    horizontal = dict()
    horizontal = collections.defaultdict(list)
    vertical = collections.defaultdict(list)
    UL = collections.defaultdict(list)
    UR = collections.defaultdict(list)
    DL = collections.defaultdict(list)
    DR = collections.defaultdict(list)
    for _ in range(N):
        X, Y, U = input().split()
        X = int(X)
        Y = int(Y)
        if U == 'R':
            heapq.heappush(horizontal[Y], [X, 0])
            heapq.heappush(UR[X+Y], [Y, 1])
            heapq.heappush(DR[X-Y], [X, 0])
        elif U == 'L':
            heapq.heappush(horizontal[Y], [X, 1])
            heapq.heappush(UL[X-Y], [X, 1])
            heapq.heappush(DL[X+Y], [X, 1])
        elif U == 'U':
            heapq.heappush(vertical[X], [Y, 0])
            heapq.heappush(UL[X-Y], [X, 0])
            heapq.heappush(UR[X+Y], [Y, 0])
        else:
            heapq.heappush(vertical[X], [Y, 1])
            heapq.heappush(DL[X+Y], [X, 0])
            heapq.heappush(DR[X-Y], [X, 1])
    # print(horizontal)
    # print(vertical)

    ans = float('inf')
    for Y in horizontal:
        if len(horizontal[Y]) > 1:
            for j in range(len(horizontal[Y])-1):
                if horizontal[Y][j][1] == 0 and horizontal[Y][j+1][1] == 1:
                    ans = min(ans, 5 * (horizontal[Y][j+1][0]- horizontal[Y][j][0]))
    for X in vertical:
        if len(vertical[X]) > 1:
            for j in range(len(vertical[X])-1):
                if vertical[X][j][1] == 0 and vertical[X][j+1][1] == 1:
                    ans = min(ans, 5 * (vertical[X][j+1][0]- vertical[X][j][0]))
    for X in UL:
        if len(UL[X]) > 1:
            for j in range(len(UL[X])-1):
                if UL[X][j][1] == 0 and UL[X][j+1][1] == 1:
                    ans = min(ans, 10 * (UL[X][j+1][0]- UL[X][j][0]))
    for X in UR:
        if len(UR[X]) > 1:
            for j in range(len(UR[X])-1):
                if UR[X][j][1] == 0 and UR[X][j+1][1] == 1:
                    ans = min(ans, 10 * (UR[X][j+1][0]- UR[X][j][0]))

    for X in DL:
        if len(DL[X]) > 1:
            for j in range(len(DL[X])-1):
                if DL[X][j][1] == 0 and DL[X][j+1][1] == 1:
                    ans = min(ans, 10 * (DL[X][j+1][0]- DL[X][j][0]))

    for X in DR:
        if len(DR[X]) > 1:
            for j in range(len(DR[X])-1):
                if DR[X][j][1] == 0 and DR[X][j+1][1] == 1:
                    ans = min(ans, 10 * (DR[X][j+1][0]- DR[X][j][0]))

    if ans == float('inf'): return "SAFE"
    return ans

if __name__ == '__main__':
    print(main())

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,(x - xxx) / 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())
big = 800001
sml = 400000
pp = [[[] for _ in range(big)] for _ in range(8)]
mm = [[[] for _ in range(sml//2+1)] for _ in range(4)]
for _ in range(n):
  x,y,d = input().split()
  x = int(x)
  y = int(y)
  if d == 'R':
    pp[0][+x+y].append(+y)
    pp[2][+x-y].append(-y)
    mm[0][y].append(x)
  if d == 'L':
    pp[4][-x+y].append(+y)
    pp[6][-x-y].append(-y)
    mm[1][y].append(x)
  if d == 'U':
    pp[1][+x+y].append(+x)
    pp[5][-x+y].append(-x)
    mm[2][x].append(y)
  if d == 'D':
    pp[3][+x-y].append(+x)
    pp[7][-x-y].append(-x)
    mm[3][x].append(y)
import bisect
inf = 10**10
ans = inf
def ser(lx,ly,su):
  global ans
  lly = len(ly)
  for xx in lx:
    b = bisect.bisect_left(ly,su-xx)
    if b != lly:
      ans = min(ans,(xx+ly[b]-su)*10)
    else:
      pass
for d in range(4):
  xd = 2*d
  yd = 2*d+1
  for sun in range(-sml,sml+1):
    if pp[xd][sun] and pp[yd][sun]:
      #print(pp[xd][sun],pp[yd][sun],sun,xd,yd)
      ser(pp[xd][sun],pp[yd][sun],sun)
for sun in range(0,sml//2+1):
  if mm[0][sun] and mm[1][sun]:
    llm = len(mm[1][sun])
    for xx in mm[0][sun]:
      b = bisect.bisect_left(mm[1][sun],xx)
      if b != llm:
        ans = min(ans,(mm[1][sun][b]-xx)*5)
      else:
        pass
for sun in range(0,sml//2+1):
  if mm[2][sun] and mm[3][sun]:
    llm = len(mm[3][sun])
    for yy in mm[2][sun]:
      b = bisect.bisect_left(mm[3][sun],yy)
      if b != llm:
        ans = min(ans,(mm[3][sun][b]-yy)*5)
      else:
        pass
#print(ans)
if ans == inf:
  print('SAFE')
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
n = int(input())
point_dict = {}
time = 200000 * 100
for i in range(n):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    if u == "U":
        for i in range(1, 200001):
            if y + i > 200000:
                break
            key = (x, y + i, i * 10)
            if key in point_dict:
                if i * 10 < time:
                    time = i * 10
            else:
                point_dict[key] = 1
    if u == "D":
        for i in range(1, 200001):
            if y - i < 0:
                break
            key = (x, y - i, i * 10)
            if key in point_dict:
                if i * 10 < time:
                    time = i * 10
            else:
                point_dict[key] = 1
    if u == "R":
        for i in range(1, 200001):
            if x + i > 200000:
                break
            key = (x + i, y, i * 10)
            if key in point_dict:
                if i * 10 < time:
                    time = i * 10
            else:
                point_dict[key] = 1
    if u == "L":
        for i in range(1, 200001):
            if x - i < 0:
                break
            key = (x - i, y, i * 10)
            if key in point_dict:
                if i * 10 < time:
                    time = i * 10
            else:
                point_dict[key] = 1

if time == -1:
    print("SAFE")
else:
    print(time)

Submission status: Wrong Answer

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()
    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))
    XYU.append((x, y, u))

ans = 10**18
for x, y, u in XYU:
    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 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:
                ans = min(ans, (x-px)//2)
                
    if u=='D':
        for px, py in UD[x]:
            if py<y:
                ans = min(ans, (y-py)//2)

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
n = int(input())
infos = []
dic = {"L":0,"R":1,"D":0,"U":1}
lr_dic = [defaultdict(list)] * 2 # straight LR
ud_dic =  [defaultdict(list)] * 2 # straight UD
lr_dic_p = [defaultdict(list)] * 2 # x + y LR
ud_dic_p = [defaultdict(list)] * 2 # x + y UD
lr_dic_m = [defaultdict(list)] * 2 # -x + y LR
ud_dic_m = [defaultdict(list)] * 2 # -x + y UD


for i in range(n):
    x,y,u = input().split()
    x = int(x)
    y = int(y)
    if u == "R" or u == "L":
        lr_dic[dic[u]][y].append(x)
        lr_dic_p[dic[u]][x+y].append(y)
        lr_dic_m[dic[u]][-x+y].append(y)
    
    else:
        ud_dic[dic[u]][x].append(y)
        ud_dic_p[dic[u]][x+y].append(x)
        ud_dic_m[dic[u]][-x+y].append(x)
    infos.append((u,x,y))
flg = True
mn = 1 << 50
for u,x,y in infos:
    if u == "D":
        # Lの中で、yより小さくて切片がx+yのもの
        ys = lr_dic_p[dic["L"]][x+y]
        for y_ in ys:
            if y_ < y:
                flg = False
                mn = min(mn, y-y_)
        # Rの中で、yより小さくて切片が-x+yのもの
        ys = lr_dic_p[dic["R"]][x-y]
        for y_ in ys:
            if y_ < y:
                flg = False
                mn = min(mn, y-y_)
        # Uの中でxが一致するもの
        ys = ud_dic[dic["U"]][x]
        for y_ in ys:
            if y_ < y:
                flg = False
                mn = min(mn, abs(y-y_)/2)
    elif u == "U":
        # Lの中で、yより大きくて切片が-x+yのもの
        ys = lr_dic_p[dic["L"]][x-y]
        for y_ in ys:
            if y_ > y:
                flg = False
                mn = min(mn, abs(y-y_))
        # Rの中で、yより大きくて切片がx+yのもの
        ys = lr_dic_p[dic["R"]][x+y]
        for y_ in ys:
            if y_ > y:
                flg = False
                mn = min(mn, abs(y-y_))
        # Dのなかでxが一致するもの
        ys = ud_dic[dic["D"]][x]
        for y_ in ys:
            if y_ > y:
                flg = False
                mn = min(mn, abs(y-y_)/2)

    elif u == "R":
        # Dの中で、xより大きくて切片が-x+yのもの
        xs = ud_dic_m[dic["D"]][-x+y]
        for x_ in xs:
            if x_ > x:
                flg = False
                mn = min(mn, abs(x-x_))
        # Uの中で、xより大きくて切片がx+yのもの
        xs = ud_dic_m[dic["U"]][+x+y]
        for x_ in xs:
            if x_ > x:
                flg = False
                mn = min(mn, abs(x-x_))
        # Lのなかでyが一致するもの
        xs = lr_dic[dic["L"]][y]
        for x_ in xs:
            if x < x_:
                flg = False
                mn = min(mn, abs(x-x_)/2)

    elif u == "L":
        # Dの中で、xより小さくて切片がx+yのもの
        xs = ud_dic_m[dic["D"]][x+y]
        for x_ in xs:
            if x_ < x:
                flg = False
                mn = min(mn, abs(x-x_))
        # Uの中で、xより小さくて切片が-x+yのもの
        xs = ud_dic_m[dic["U"]][-x+y]
        for x_ in xs:
            if x_ < x:
                flg = False
                mn = min(mn, abs(x-x_))

        # Rのなかでyが一致するもの
        xs = lr_dic[dic["R"]][y]
        for x_ in xs:
            if x_ < x:
                flg = False
                mn = min(mn, abs(x-x_)/2)

if flg:
    print("SAFE")
if not flg:
    print(int(mn * 10))

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
    u_list, d_list, l_list, r_list = [], [], [], []
    for x, y, d in xyp:
        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))
    # 1→u,d
    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
    li, lj = len(r_list), len(l_list)
    r_list.sort(key=lambda x: x[1])
    l_list.sort(key=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
    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
    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
    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
    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:
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
    u_list, d_list, l_list, r_list = [], [], [], []
    for x, y, d in xyp:
        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))
    # 1→u,d
    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
    li, lj = len(r_list), len(l_list)
    r_list.sort(key=lambda x: x[1])
    l_list.sort(key=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
    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
    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
    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
    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)