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:
#!/usr/bin/env python3
import sys
from enum import IntEnum
from itertools import groupby

rl = sys.stdin.buffer.readline
INF = sys.maxsize

class Direction(IntEnum):
  UP = 1
  RIGHT = 2
  DOWN = 3
  LEFT = 4

direction_of_str = {
  b'U': Direction.UP,
  b'R': Direction.RIGHT,
  b'D': Direction.DOWN,
  b'L': Direction.LEFT,
}

X = 0
Y = 1

def read_data(n):
  d = {
    Direction.UP: [],
    Direction.RIGHT: [],
    Direction.DOWN: [],
    Direction.LEFT: []
  }
  for _ in range(n):
    l = rl().split()
    d[direction_of_str[l[2]]].append((int(l[0]), int(l[1])))
  return d

def to_d(gl):
  return {k: list(v) for k, v in gl}

N = int(rl())
PS = read_data(N)

col_dist = INF

up_down_x_dic = to_d(groupby(PS[Direction.DOWN], key=lambda t: t[X]))
up_right_dic = to_d(groupby(PS[Direction.RIGHT], key=lambda t: t[X]+t[Y]))
up_left_dic = to_d(groupby(PS[Direction.LEFT], key=lambda t: t[Y]-t[X]))
# UP
for u in PS[Direction.UP]:
  # 正面衝突
  if u[X] in up_down_x_dic:
    for d in up_down_x_dic[u[X]]:
      if u[Y] < d[Y]:
        col_dist = min(col_dist, ((d[Y]-u[Y]) * 10) // 2)
  # 右
  if (u[X]+u[Y]) in up_right_dic:
    for r in up_right_dic[u[X]+u[Y]]:
      if u[Y] < r[Y]:
        col_dist = min(col_dist, (r[Y]-u[Y]) * 10)
  # 左
  if (u[Y]-u[X]) in up_left_dic:
    for l in up_left_dic[u[Y]-u[X]]:
      if u[Y] < l[Y]:
        col_dist = min(col_dist, (l[Y]-u[Y]) * 10)

right_left_y_dic = to_d(groupby(PS[Direction.LEFT], key=lambda t: t[Y]))
right_down_dic = to_d(groupby(PS[Direction.DOWN], key=lambda t: t[X]-t[Y]))
# RIGHT
for r in PS[Direction.RIGHT]:
  # 正面衝突
  if r[Y] in right_left_y_dic:
    for l in right_left_y_dic[r[Y]]:
      if r[X] < l[X]:
        col_dist = min(col_dist, ((l[X]-r[X]) * 10) // 2)
  # 横
  if (r[X]-r[Y]) in right_down_dic:
    for d in right_down_dic[r[X]-r[Y]]:
      if r[X] < d[X]:
        col_dist = min(col_dist, (d[X]-r[X]) * 10)

down_left_dic = to_d(groupby(PS[Direction.LEFT], key=lambda t: t[X]+t[Y]))
# DOWN
for d in PS[Direction.DOWN]:
  # 横
  if (d[X]+d[Y]) in down_left_dic:
    for l in down_left_dic[d[X]+d[Y]]:
      if d[X] < l[X]:
        col_dist = min(col_dist, (l[X]-d[X]) * 10)

print(col_dist if col_dist < INF else 'SAFE')

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from enum import IntEnum

rl = sys.stdin.buffer.readline
INF = sys.maxsize

class Direction(IntEnum):
  UP = 1
  RIGHT = 2
  DOWN = 3
  LEFT = 4

direction_of_str = {
  b'U': Direction.UP,
  b'R': Direction.RIGHT,
  b'D': Direction.DOWN,
  b'L': Direction.LEFT,
}

X = 0
Y = 1

def read_data(n):
  d = {
    Direction.UP: [],
    Direction.RIGHT: [],
    Direction.DOWN: [],
    Direction.LEFT: []
  }
  for _ in range(n):
    l = rl().split()
    d[direction_of_str[l[2]]].append((int(l[0]), int(l[1])))
  return d

N = int(rl())
PS = read_data(N)

col_dist = INF

# UP
for u in PS[Direction.UP]:
  # 正面衝突
  for d in PS[Direction.DOWN]:
    if u[X] == d[X] and u[Y] < d[Y]:
      col_dist = min(col_dist, (d[Y]-u[Y]) // 2)
  # 横
  for r in PS[Direction.RIGHT]:
    if u[Y] < r[Y] and r[Y]-u[Y] == u[X]-r[X]:
      col_dist = min(col_dist, r[Y]-u[Y])
  for l in PS[Direction.LEFT]:
    if u[Y] < l[Y] and l[Y]-u[Y] == l[X]-u[X]:
      col_dist = min(col_dist, l[Y]-u[Y])

# RIGHT
for r in PS[Direction.RIGHT]:
  # 正面衝突
  for l in PS[Direction.LEFT]:
    if r[Y] == l[Y] and r[X] < l[X]:
      col_dist = min(col_dist, (l[X]-r[X]) // 2)
  # 横
  for d in PS[Direction.DOWN]:
    if r[X] < d[X] and d[X]-r[X] == d[Y]-r[Y]:
      col_dist = min(col_dist, d[X]-r[X])

# DOWN
for d in PS[Direction.DOWN]:
  # 横
  for l in PS[Direction.LEFT]:
    if d[X] < l[X] and l[X]-d[X] == d[Y]-l[Y]:
      col_dist = min(col_dist, l[X]-d[X])

print(col_dist * 10 if col_dist < INF else 'SAFE')

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from enum import IntEnum

rl = sys.stdin.buffer.readline
INF = sys.maxsize

class Direction(IntEnum):
  UP = 1
  RIGHT = 2
  DOWN = 3
  LEFT = 4

direction_of_str = {
  b'U': Direction.UP,
  b'R': Direction.RIGHT,
  b'D': Direction.DOWN,
  b'L': Direction.LEFT,
}

X = 0
Y = 1

def read_data(n):
  d = {
    Direction.UP: [],
    Direction.RIGHT: [],
    Direction.DOWN: [],
    Direction.LEFT: []
  }
  for _ in range(n):
    l = rl().split()
    d[direction_of_str[l[2]]].append((int(l[0]), int(l[1])))
  return d

N = int(rl())
PS = read_data(N)

col_dist = INF

# UP
while len(PS[Direction.UP]) != 0:
  u = PS[Direction.UP].pop()
  # 正面衝突
  for d in PS[Direction.DOWN]:
    if u[X] == d[X] and u[Y] < d[Y]:
      col_dist = min(col_dist, (d[Y]-u[Y]) // 2)
  # 横
  for r in PS[Direction.RIGHT]:
    if u[Y] < r[Y] and r[Y]-u[Y] == u[X]-r[X]:
      col_dist = min(col_dist, r[Y]-u[Y])
  for l in PS[Direction.LEFT]:
    if u[Y] < l[Y] and l[Y]-u[Y] == l[X]-u[X]:
      col_dist = min(col_dist, l[Y]-u[Y])

# RIGHT
while len(PS[Direction.RIGHT]) != 0:
  r = PS[Direction.RIGHT].pop()
  # 正面衝突
  for l in PS[Direction.LEFT]:
    if r[Y] == l[Y] and r[X] < l[X]:
      col_dist = min(col_dist, (l[X]-r[X]) // 2)
  # 横
  for d in PS[Direction.DOWN]:
    if r[X] < d[X] and d[X]-r[X] == d[Y]-r[Y]:
      col_dist = min(col_dist, d[X]-r[X])

# DOWN
while len(PS[Direction.DOWN]) != 0:
  d = PS[Direction.DOWN].pop()
  # 横
  for l in PS[Direction.LEFT]:
    if d[X] < l[X] and l[X]-d[X] == d[Y]-l[Y]:
      col_dist = min(col_dist, l[X]-d[X])

print(col_dist * 10 if col_dist < INF else 'SAFE')

Submission status: Wrong Answer

Solution code:
import heapq
import copy
n = int(input())

size = 2*10**5
def f(ary):
    t_ans = 10**100
    for i in range(len(ary)):
        t_ud = ary[i]
        if len(t_ud) != 0:
            #print(t_ud)
            t_ud.sort()
            t_u = -10**100
            for j in range(len(t_ud)):
                if t_ud[j][1] == 'U':
                    t_u = t_ud[j][0]
                elif t_u == -10**100:
                    continue
                else:
                    t_ans = min(t_ans, (t_ud[j][0] - t_u))
    return t_ans

xyu = []

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

ud = [[] for i in range(size+1)]
rl = [[] for i in range(size+1)]
ul = [[] for i in range(size*2+2)]
ur = [[] for i in range(size*2+2)]
dl = [[] for i in range(size*2+2)]
dr = [[] for i in range(size*2+2)]

for i in range(n):
    if xyu[i][2] == 'U':
        ud[xyu[i][0]].append([xyu[i][1],'U'])
        ul[xyu[i][1]-xyu[i][0]+size].append([xyu[i][1],'U'])
        ur[xyu[i][1]+xyu[i][0]].append([xyu[i][1],'U'])
    if xyu[i][2] == 'D':
        ud[xyu[i][0]].append([xyu[i][1],'D'])
        dl[xyu[i][0]+xyu[i][1]].append([xyu[i][1],'D'])
        dr[xyu[i][1]-xyu[i][0]+size].append([xyu[i][1],'D'])
    if xyu[i][2] == 'R':
        rl[xyu[i][0]].append([xyu[i][1],'U'])
        ur[xyu[i][1]+xyu[i][0]].append([xyu[i][1],'D'])
        dr[xyu[i][1]-xyu[i][0]+size].append([xyu[i][1],'U'])
    if xyu[i][2] == 'L':
        rl[xyu[i][0]].append([xyu[i][1],'D'])
        ul[xyu[i][1]-xyu[i][0]+size].append([xyu[i][1],'D'])
        dl[xyu[i][0]+xyu[i][1]].append([xyu[i][1],'U'])
                  
ans = 10**100
ans = min(ans,f(ud)*5)
ans = min(ans,f(rl)*5)
ans = min(ans,f(ul)*10)
ans = min(ans,f(ur)*10)
ans = min(ans,f(dl)*10)
ans = min(ans,f(dr)*10)
if ans == 10**100:
    print('SAFE')
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
import copy
n = int(input())

size = 2*10**5
def f(ary):
    t_ans = 10**100
    for i in range(len(ary)):
        t_ud = ary[i]
        if len(t_ud) != 0:
            #print(t_ud)
            t_ud.sort()
            t_u = -10**100
            for j in range(len(t_ud)):
                if t_ud[j][1] == 'U':
                    t_u = t_ud[j][0]
                elif t_u == -10**100:
                    continue
                else:
                    t_ans = min(t_ans, (t_ud[j][0] - t_u))
    return t_ans

xyu = []

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

ud = [[] for i in range(size+1)]
rl = [[] for i in range(size+1)]
ul = [[] for i in range(size*2+2)]
ur = [[] for i in range(size*2+2)]
dl = [[] for i in range(size*2+2)]
dr = [[] for i in range(size*2+2)]

for i in range(n):
    if xyu[i][2] == 'U':
        ud[xyu[i][0]].append([xyu[i][1],'U'])
        ul[xyu[i][1]-xyu[i][0]+size].append([xyu[i][1],'U'])
        ur[xyu[i][1]+xyu[i][0]].append([xyu[i][1],'U'])
    if xyu[i][2] == 'D':
        ud[xyu[i][0]].append([xyu[i][1],'D'])
        dl[xyu[i][0]+xyu[i][1]].append([xyu[i][1],'D'])
        dr[xyu[i][1]-xyu[i][0]].append([xyu[i][1],'D'])
    if xyu[i][2] == 'R':
        rl[xyu[i][0]].append([xyu[i][1],'U'])
        ur[xyu[i][1]+xyu[i][0]].append([xyu[i][1],'D'])
        dr[xyu[i][1]-xyu[i][0]].append([xyu[i][1],'U'])
    if xyu[i][2] == 'L':
        rl[xyu[i][0]].append([xyu[i][1],'D'])
        ul[xyu[i][1]-xyu[i][0]+size].append([xyu[i][1],'D'])
        dl[xyu[i][0]+xyu[i][1]].append([xyu[i][1],'U'])
                  
ans = 10**100
ans = min(ans,f(ud)*5)
ans = min(ans,f(rl)*5)
ans = min(ans,f(ul)*10)
ans = min(ans,f(ur)*10)
ans = min(ans,f(dl)*10)
ans = min(ans,f(dr)*10)
if ans == 10**100:
    print('SAFE')
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
import copy
n = int(input())

size = 2*10**5
print("SAFE")
exit()
def f(ary):
    t_ans = 10**100
    for i in range(len(ary)):
        t_ud = ary[i]
        if len(t_ud) != 0:
            #print(t_ud)
            t_ud.sort()
            t_u = -10**100
            for j in range(len(t_ud)):
                if t_ud[j][1] == 'U':
                    t_u = t_ud[j][0]
                elif t_u == -10**100:
                    continue
                else:
                    t_ans = min(t_ans, (t_ud[j][0] - t_u))
    return t_ans

xyu = []

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

ud = [[] for i in range(size+1)]
rl = [[] for i in range(size+1)]
ul = [[] for i in range(size*2+2)]
ur = [[] for i in range(size*2+2)]
dl = [[] for i in range(size*2+2)]
dr = [[] for i in range(size*2+2)]

for i in range(n):
    if xyu[i][2] == 'U':
        ud[xyu[i][0]].append([xyu[i][1],'U'])
        ul[xyu[i][1]-xyu[i][0]+size].append([xyu[i][1],'U'])
        ur[xyu[i][1]+xyu[i][0]].append([xyu[i][1],'U'])
    if xyu[i][2] == 'D':
        ud[xyu[i][0]].append([xyu[i][1],'D'])
        dl[xyu[i][0]+xyu[i][1]].append([xyu[i][1],'D'])
        dr[xyu[i][1]-xyu[i][0]].append([xyu[i][1],'D'])
    if xyu[i][2] == 'R':
        rl[xyu[i][0]].append([xyu[i][1],'U'])
        ur[xyu[i][1]+xyu[i][0]].append([xyu[i][1],'D'])
        dr[xyu[i][1]-xyu[i][0]].append([xyu[i][1],'U'])
    if xyu[i][2] == 'L':
        rl[xyu[i][0]].append([xyu[i][1],'D'])
        ul[xyu[i][1]-xyu[i][0]+size].append([xyu[i][1],'D'])
        dl[xyu[i][0]+xyu[i][1]].append([xyu[i][1],'U'])
                  
ans = 10**100
ans = min(ans,f(ud)*5)
ans = min(ans,f(rl)*5)
ans = min(ans,f(ul)*10)
ans = min(ans,f(ur)*10)
ans = min(ans,f(dl)*10)
ans = min(ans,f(dr)*10)
if ans == 10**100:
    print('SAFE')
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
import copy
n = int(input())

size = 2*10**5
print("SAFE")
exit()
def f(ary):
    t_ans = 10**100
    for i in range(len(ary)):
        t_ud = ary[i]
        if len(t_ud) != 0:
            #print(t_ud)
            t_ud.sort()
            t_u = -10**100
            for j in range(len(t_ud)):
                if t_ud[j][1] == 'U':
                    t_u = t_ud[j][0]
                elif t_u == -10**100:
                    continue
                else:
                    t_ans = min(t_ans, (t_ud[j][0] - t_u))
    return t_ans

xyu = []

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

ud = [[] for i in range(size+1)]
rl = [[] for i in range(size+1)]
ul = [[] for i in range(size*2+2)]
ur = [[] for i in range(size*2+2)]
dl = [[] for i in range(size*2+2)]
dr = [[] for i in range(size*2+2)]

for i in range(n):
    if xyu[i][2] == 'U':
        ud[xyu[i][0]-1].append([xyu[i][1],'U'])
        ul[xyu[i][1]-xyu[i][0]+size].append([xyu[i][1],'U'])
        ur[xyu[i][1]+xyu[i][0]].append([xyu[i][1],'U'])
    if xyu[i][2] == 'D':
        ud[xyu[i][0]-1].append([xyu[i][1],'D'])
        dl[xyu[i][0]+xyu[i][1]].append([xyu[i][1],'D'])
        dr[xyu[i][1]-xyu[i][0]].append([xyu[i][1],'D'])
    if xyu[i][2] == 'R':
        rl[xyu[i][0]-1].append([xyu[i][1],'U'])
        ur[xyu[i][1]+xyu[i][0]].append([xyu[i][1],'D'])
        dr[xyu[i][1]-xyu[i][0]].append([xyu[i][1],'U'])
    if xyu[i][2] == 'L':
        rl[xyu[i][0]-1].append([xyu[i][1],'D'])
        ul[xyu[i][1]-xyu[i][0]+size].append([xyu[i][1],'D'])
        dl[xyu[i][0]+xyu[i][1]].append([xyu[i][1],'U'])
                  
ans = 10**100
ans = min(ans,f(ud)*5)
ans = min(ans,f(rl)*5)
ans = min(ans,f(ul)*10)
ans = min(ans,f(ur)*10)
ans = min(ans,f(dl)*10)
ans = min(ans,f(dr)*10)
if ans == 10**100:
    print('SAFE')
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
import copy
n = int(input())

size = 2*10**5

def f(ary):
    t_ans = 10**100
    for i in range(len(ary)):
        t_ud = ary[i]
        if len(t_ud) != 0:
            #print(t_ud)
            t_ud.sort()
            t_u = -10**100
            for j in range(len(t_ud)):
                if t_ud[j][1] == 'U':
                    t_u = t_ud[j][0]
                elif t_u == -10**100:
                    continue
                else:
                    t_ans = min(t_ans, (t_ud[j][0] - t_u))
    return t_ans

xyu = []

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

ud = [[] for i in range(size+1)]
rl = [[] for i in range(size+1)]
ul = [[] for i in range(size*2+2)]
ur = [[] for i in range(size*2+2)]
dl = [[] for i in range(size*2+2)]
dr = [[] for i in range(size*2+2)]

for i in range(n):
    if xyu[i][2] == 'U':
        ud[xyu[i][0]-1].append([xyu[i][1],'U'])
        ul[xyu[i][1]-xyu[i][0]+size].append([xyu[i][1],'U'])
        ur[xyu[i][1]+xyu[i][0]].append([xyu[i][1],'U'])
    if xyu[i][2] == 'D':
        ud[xyu[i][0]-1].append([xyu[i][1],'D'])
        dl[xyu[i][0]+xyu[i][1]].append([xyu[i][1],'D'])
        dr[xyu[i][1]-xyu[i][0]].append([xyu[i][1],'D'])
    if xyu[i][2] == 'R':
        rl[xyu[i][0]-1].append([xyu[i][1],'U'])
        ur[xyu[i][1]+xyu[i][0]].append([xyu[i][1],'D'])
        dr[xyu[i][1]-xyu[i][0]].append([xyu[i][1],'U'])
    if xyu[i][2] == 'L':
        rl[xyu[i][0]-1].append([xyu[i][1],'D'])
        ul[xyu[i][1]-xyu[i][0]+size].append([xyu[i][1],'D'])
        dl[xyu[i][0]+xyu[i][1]].append([xyu[i][1],'U'])
                  
ans = 10**100
ans = min(ans,f(ud)*5)
ans = min(ans,f(rl)*5)
ans = min(ans,f(ul)*10)
ans = min(ans,f(ur)*10)
ans = min(ans,f(dl)*10)
ans = min(ans,f(dr)*10)
if ans == 10**100:
    print('SAFE')
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
import copy
n = int(input())

size = 2*10**5

def f(ary):
    t_ans = 10**100
    for i in range(len(ary)):
        t_ud = ary[i]
        if len(t_ud) != 0:
            #print(t_ud)
            t_ud.sort()
            t_u = -10**100
            for j in range(len(t_ud)):
                if t_ud[j][1] == 'U':
                    t_u = t_ud[j][0]
                elif t_u == -10**100:
                    continue
                else:
                    t_ans = min(t_ans, (t_ud[j][0] - t_u))
    return t_ans

xyu = []

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

ud = [[] for i in range(size+1)]
rl = [[] for i in range(size+1)]
ul = [[] for i in range(size*2+2)]
ur = [[] for i in range(size*2+2)]
dl = [[] for i in range(size*2+2)]
dr = [[] for i in range(size*2+2)]

for i in range(n):
    if xyu[i][2] == 'U':
        ud[xyu[i][0]-1].append([xyu[i][1],'U'])
        ul[xyu[i][1]-xyu[i][0]+size].append([xyu[i][1],'U'])
        ur[xyu[i][1]+xyu[i][0]].append([xyu[i][1],'U'])
    if xyu[i][2] == 'D':
        ud[xyu[i][0]-1].append([xyu[i][1],'D'])
        dl[xyu[i][0]+xyu[i][1]].append([xyu[i][1],'D'])
        dr[xyu[i][1]-xyu[i][0]].append([xyu[i][1],'D'])
    if xyu[i][2] == 'R':
        rl[xyu[i][0]-1].append([xyu[i][1],'U'])
        ur[xyu[i][1]+xyu[i][0]].append([xyu[i][1],'D'])
        dr[xyu[i][1]-xyu[i][0]].append([xyu[i][1],'U'])
    if xyu[i][2] == 'L':
        rl[xyu[i][0]-1].append([xyu[i][1],'D'])
        ul[xyu[i][1]-xyu[i][0]+size].append([xyu[i][1],'D'])
        dl[xyu[i][0]+xyu[i][1]].append([xyu[i][1],'U'])
                  
ans = 10**100
ans = min(ans,f(ud)*5)
ans = min(ans,f(rl)*5)
ans = min(ans,f(ul)*10)
ans = min(ans,f(ur)*10)
ans = min(ans,f(dl)*10)
ans = min(ans,f(dr)*10)
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
n=int(input())
a= [list(input().split()) for i in range(n)]
for i in range(n):
    a[i][0]=int(a[i][0])
    a[i][1]=int(a[i][1])
xx=sorted(a)
xxx=sorted(a,key=lambda x:x[1])
print(xx)
print(xxx)

# x座標ごとに並び替え,U,Dを格納
xU=[[] for i in range(200001)]
xD=[[] for i in range(200001)]
# y座標ごとに並び替え,R,Lを格納
yR=[[] for i in range(200001)]
yL=[[] for i in range(200001)]
# 斜め
zU=[[] for i in range(-200001,200001)]
zD=[[] for i in range(-200001,200001)]
zR=[[] for i in range(-200001,200001)]
zL=[[] for i in range(-200001,200001)]

ans=pow(10,9)

for a1,a2,a3 in a:
    if a3=='U':
        xU[a1].append(a2)
        zU[(a2-a1)].append(a2)
    elif a3=='D':
        xD[a1].append(a2)
        zD[(a2-a1)].append(a2)
    elif a3=='R':
        yR[a2].append(a1)
        zR[(a2-a1)].append(a2)
    else:
        yL[a2].append(a1)
        zL[(a2-a1)].append(a2)





for i in range(-200001,200001):
    if len(xU[i])!=0 and len(xD[i])!=0:
        xU[i].sort()
        xD[i].sort()
        if xU[i][0]<=xD[i][-1]:
            for j in xD[i]:
                v=bisect.bisect_right(xU[i],j)
                if v!=0:
                    ans=min(ans,(j-xU[i][v-1])*5)

    elif len(yR[i])!=0 and len(yL[i])!=0:
        yR[i].sort()
        yL[i].sort()
        if yR[i][0]<=yL[i][-1]:
            for j in yL[i]:
                v=bisect.bisect_right(yR[i],j)
                if v!=0:
                    ans=min(ans,(j-yR[i][v-1])*5)

    elif len(zR[i]) != 0 and len(zD[i]) != 0:
        zR[i].sort()
        zD[i].sort()
        if zR[i][0] <= zD[i][-1]:
            for j in zD[i]:
                v = bisect.bisect_right(zR[i], j)
                if v != 0:
                    ans = min(ans, (j - zR[i][v - 1]) * 5)

    elif len(zL[i]) != 0 and len(zU[i]) != 0:
        zL[i].sort()
        zU[i].sort()
        if zU[i][-1] <=  zL[i][0]:
            for j in zL[i]:
                v = bisect.bisect_right(zU[i], j)
                if v != 0:
                    ans = min(ans, (j - zU[i][v - 1]) * 5)

if ans!=pow(10,9):
    print(ans)
else:
    print('SAFE')

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50

def calc(Asum, Bsum, A, B, ans, ope = 1):
    if not len(A) or not len(B):
        return ans

    #print(Asum, Bsum)
    B_ = [val for val, _, _ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        if bi >= len(B_): continue
        x1, y1 = A[ai]
        val, _, bi2 = Bsum[bi]
        #print(bi, val, asum)
        if val == asum:
            x2, y2 = B[bi2]
            time = (x2 - x1) * ope
            if time < 0:
                continue
            ans = min(ans, time*10)
        else:
            continue
    return ans

def calc_line(Asum, Bsum, A, B, ans):
    if not len(A) or not len(B):
        return ans

    # print(Asum, Bsum)
    B_ = [val for val, _,_ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        if bi >= len(B_):continue
        x1, y1 = A[ai]
        val, _, bi2 = Bsum[bi]
        if val == asum:
            bi2 = Bsum[bi][2]
            x2, y2 = B[bi2]
            time = y2 - y1 if x1 == x2 else x2 - x1
            if time < 0:
                continue
            ans = min(ans, time*5)
        else:
            continue
    return ans

def run():
    U, D, R, L = [],[],[],[]
    N = int(sysread())
    for i in range(N):
        x,y,u = input().split()
        #print(x, y, u)
        x,y = int(x), int(y)
        if u == 'U':
            U.append((x,y))
        elif u == 'D':
            D.append((x,y))
        elif u == 'R':
            R.append((x,y))
        elif u == 'L':
            L.append((x,y))

    #print(U, D, R, L)
    ans = INF
    # U vs R
    Usum = sorted([(x+y, i) for i, (x,y) in enumerate(U)])
    Rsum = sorted([(x+y,-x, i) for i, (x,y) in enumerate(R)], key = lambda x : (x[0], x[1]))
    ans = calc(Usum, Rsum, U, R, ans, ope=-1)

    # D vs L
    Dsum = sorted([(x + y, i) for i, (x, y) in enumerate(D)])
    Lsum = sorted([(x + y,x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc(Dsum, Lsum, D, L, ans, ope=1)

    # U vs L
    Usub = sorted([(x - y, i) for i, (x, y) in enumerate(U)])
    Lsub = sorted([(x - y,x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc(Usub, Lsub, U, L, ans, ope=1)

    # D vs R
    Dsub = sorted([(x - y, i) for i, (x, y) in enumerate(D)])
    Rsub = sorted([(x - y,-x, i) for i, (x, y) in enumerate(R)], key = lambda x : (x[0], x[1]))
    ans = calc(Dsub, Rsub, D, R, ans, ope=-1)

    # U vs D
    UU = sorted([(x, i) for i, (x,y) in enumerate(U)])
    DD = sorted([(x, -y, i) for i, (x, y) in enumerate(D)], key = lambda x : (x[0], x[1]))
    ans = calc_line(UU, DD, U, D, ans)

    # R vs L
    RR = sorted([(y, i) for i, (x, y) in enumerate(R)])
    LL = sorted([(y, -x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc_line(RR, LL, R, L, ans)

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



if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50

def calc(Asum, Bsum, A, B, ans, ope = 1):
    if not len(A) or not len(B):
        return ans

    #print(Asum, Bsum)
    B_ = [val for val, _, _ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        if bi >= len(B_): continue
        x1, y1 = A[ai]
        val, _, bi2 = Bsum[bi]
        #print(bi, val, asum)
        if val == asum:
            x2, y2 = B[bi2]
            time = (x2 - x1) * ope
            if time < 0:
                continue
            ans = min(ans, time*10)
        else:
            continue
    return ans

def calc_line(Asum, Bsum, A, B, ans):
    if not len(A) or not len(B):
        return ans

    # print(Asum, Bsum)
    B_ = [val for val, _,_ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        if bi >= len(B_):continue
        x1, y1 = A[ai]
        val, _, bi2 = Bsum[bi]
        if val == asum:
            bi2 = Bsum[bi][2]
            x2, y2 = B[bi2]
            time = y2 - y1 if x1 == x2 else x2 - x1
            if time < 0:
                continue
            ans = min(ans, time*5)
        else:
            continue
    return ans

def run():
    U, D, R, L = [],[],[],[]
    N = int(sysread())
    for i in range(N):
        x,y,u = input().split()
        #print(x, y, u)
        x,y = int(x), int(y)
        if u == 'U':
            U.append((x,y))
        elif u == 'D':
            D.append((x,y))
        elif u == 'R':
            R.append((x,y))
        elif u == 'L':
            L.append((x,y))

    #print(U, D, R, L)
    ans = INF
    # U vs R
    Usum = sorted([(x+y, i) for i, (x,y) in enumerate(U)])
    Rsum = sorted([(x+y,-x, i) for i, (x,y) in enumerate(R)], key = lambda x : (x[0], x[1]))
    ans = calc(Usum, Rsum, U, R, ans, ope=-1)

    # D vs L
    Dsum = sorted([(x + y, i) for i, (x, y) in enumerate(D)])
    Lsum = sorted([(x + y,x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc(Dsum, Lsum, D, L, ans, ope=1)

    # U vs L
    Usub = sorted([(x - y, i) for i, (x, y) in enumerate(U)])
    Lsub = sorted([(x - y,x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc(Usub, Lsub, U, L, ans, ope=1)

    # D vs R
    Dsub = sorted([(x - y, i) for i, (x, y) in enumerate(D)])
    Rsub = sorted([(x - y,-x, i) for i, (x, y) in enumerate(R)], key = lambda x : (x[0], x[1]))
    ans = calc(Dsub, Rsub, D, R, ans, ope=-1)

    # U vs D
    UU = sorted([(x, i) for i, (x,y) in enumerate(U)])
    DD = sorted([(x, y, i) for i, (x, y) in enumerate(D)], key = lambda x : (x[0], x[1]))
    ans = calc_line(UU, DD, U, D, ans)

    # R vs L
    RR = sorted([(y, i) for i, (x, y) in enumerate(R)])
    LL = sorted([(y, x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc_line(RR, LL, R, L, ans)

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



if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50

def calc(Asum, Bsum, A, B, ans, ope = 1):
    if not len(A) or not len(B):
        return ans

    #print(Asum, Bsum)
    B_ = [val for val, _, _ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        if bi >= len(B_): continue
        x1, y1 = A[ai]
        val, _, bi2 = Bsum[bi]
        #print(bi, val, asum)
        if val == asum:
            x2, y2 = B[bi2]
            time = (x2 - x1) * ope
            if time < 0:
                continue
            ans = min(ans, time*10)
        else:
            continue
    return ans

def calc_line(Asum, Bsum, A, B, ans):
    if not len(A) or not len(B):
        return ans

    # print(Asum, Bsum)
    B_ = [val for val, _,_ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        if bi >= len(B_):continue
        x1, y1 = A[ai]
        val, _, bi2 = Bsum[bi]
        if val == asum:
            bi2 = Bsum[bi][2]
            x2, y2 = B[bi2]
            time = y2 - y1 if x1 == x2 else x2 - x1
            if time < 0:
                continue
            ans = min(ans, time*5)
        else:
            continue
    return ans

def run():
    U, D, R, L = [],[],[],[]
    N = int(sysread())
    for i in range(N):
        x,y,u = input().split()
        #print(x, y, u)
        x,y = int(x), int(y)
        if u == 'U':
            U.append((x,y))
        elif u == 'D':
            D.append((x,y))
        elif u == 'R':
            R.append((x,y))
        elif u == 'L':
            L.append((x,y))

    #print(U, D, R, L)
    ans = INF
    # U vs R
    Usum = sorted([(x+y, i) for i, (x,y) in enumerate(U)])
    Rsum = sorted([(x+y,-x, i) for i, (x,y) in enumerate(R)], key = lambda x : (x[0], x[1]))
    ans = calc(Usum, Rsum, U, R, ans, ope=-1)

    # D vs L
    Dsum = sorted([(x + y, i) for i, (x, y) in enumerate(D)])
    Lsum = sorted([(x + y,x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc(Dsum, Lsum, D, L, ans, ope=1)

    # U vs L
    Usub = sorted([(x - y, i) for i, (x, y) in enumerate(U)])
    Lsub = sorted([(x - y,-x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc(Usub, Lsub, U, L, ans, ope=1)

    # D vs R
    Dsub = sorted([(x - y, i) for i, (x, y) in enumerate(D)])
    Rsub = sorted([(x - y,x, i) for i, (x, y) in enumerate(R)], key = lambda x : (x[0], x[1]))
    ans = calc(Dsub, Rsub, D, R, ans, ope=-1)

    # U vs D
    UU = sorted([(x, i) for i, (x,y) in enumerate(U)])
    DD = sorted([(x, y, i) for i, (x, y) in enumerate(D)], key = lambda x : (x[0], x[1]))
    ans = calc_line(UU, DD, U, D, ans)

    # R vs L
    RR = sorted([(y, i) for i, (x, y) in enumerate(R)])
    LL = sorted([(y, x, i) for i, (x, y) in enumerate(L)], key = lambda x : (x[0], x[1]))
    ans = calc_line(RR, LL, R, L, ans)

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



if __name__ == "__main__":
    run()