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: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

from operator import itemgetter

n = readint()

ans = 10**10

u = []
d = []
l = []
r = []
uu = [[]for i in range(2*10**5+1)]
dd = [[]for i in range(2*10**5+1)]
ll = [[]for i in range(2*10**5+1)]
rr = [[]for i in range(2*10**5+1)]
for i in range(n):
    x,y,a = readstrs()
    x = int(x)
    y = int(y)
    if a=='U':
        u.append([x,y,x+y,x-y])
        uu[x].append(y)
    elif a == 'D':
        d.append([x,y,x+y,x-y])
        dd[x].append(y)
    elif a == 'L':
        l.append([x,y,x+y,x-y])
        ll[y].append(x)
    elif a == 'R':
        r.append([x,y,x+y,x-y])
        rr[y].append(x)

for i in range(2*10**5+1):
    if uu[i] and dd[i]:
        uu[i].sort()
        dd[i].sort()
        lenu = len(uu[i])
        lend = len(dd[i])
        k = 0
        j = 0
        while k<lenu and j<lend:
            if dd[i][j]>uu[i][k]:
                ans = min(ans,(dd[i][j]-uu[i][k])//2)
                k += 1
            else:
                j += 1
    if ll[i] and rr[i]:
        ll[i].sort()
        rr[i].sort()
        lenl = len(ll[i])
        lenr = len(rr[i])
        k = 0
        j = 0
        while k<lenl and j<lenr:
            if ll[i][j]>rr[i][k]:
                ans = min(ans,(ll[i][j]-rr[i][k])//2)
                k += 1
            else:
                j += 1

uu = [[]for i in range(4*10**5+1)]
dd = [[]for i in range(4*10**5+1)]
ll = [[]for i in range(4*10**5+1)]
rr = [[]for i in range(4*10**5+1)]

for a in u:
    uu[a[3]].append(a[0])
for a in d:
    dd[a[3]].append(a[0])    
for a in l:
    ll[a[3]].append(a[0])
for a in r:
    rr[a[3]].append(a[0])

for i in range(4*10**5+1):
    if uu[i] and ll[i]:
        uu[i].sort()
        ll[i].sort()
        lenu = len(uu[i])
        lenl = len(ll[i])
        k = 0
        j = 0
        while k<lenu and j<lenl:
            if ll[i][j]>uu[i][k]:
                ans = min(ans,ll[i][j]-uu[i][k])
                k += 1
            else:
                j += 1
    if dd[i] and rr[i]:
        dd[i].sort()
        rr[i].sort()
        lend = len(dd[i])
        lenr = len(rr[i])
        k = 0
        j = 0
        while j<lend and k<lenr:
            if dd[i][j]>rr[i][k]:
                ans = min(ans,dd[i][j]-rr[i][k])
                k += 1
            else:
                j += 1

        
uu = [[]for i in range(4*10**5+1)]
dd = [[]for i in range(4*10**5+1)]
ll = [[]for i in range(4*10**5+1)]
rr = [[]for i in range(4*10**5+1)]

for a in u:
    uu[a[2]].append(a[0])
for a in d:
    dd[a[2]].append(a[0])    
for a in l:
    ll[a[2]].append(a[0])
for a in r:
    rr[a[2]].append(a[0])

for i in range(4*10**5+1):
    if uu[i] and rr[i]:
        uu[i].sort()
        rr[i].sort()
        lenu = len(uu[i])
        lenr = len(rr[i])
        k = 0
        j = 0
        while k<lenu and j<lenr:
            if rr[i][j]<uu[i][k]:
                ans = min(ans,-rr[i][j]+uu[i][k])
                k += 1
            else:
                j += 1
    if dd[i] and ll[i]:
        dd[i].sort()
        ll[i].sort()
        lend = len(dd[i])
        lenl = len(ll[i])
        k = 0
        j = 0
        while j<lend and k<lenl:
            if dd[i][j]<ll[i][k]:
                ans = min(ans,-dd[i][j]+ll[i][k])
                k += 1
            else:
                j += 1

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

Submission status: Runtime Error

Solution code:
from bisect import bisect_left
n = int(input())
st = 2*10**5+5
dataU = []
dataR = []
dataD = []
dataL = []
rDown = [[] for j in range(4*10**5+10)]
rUp = [[0] for j in range(4*10**5+10)]
lDown = [[0] for j in range(4*10**5+10)]
lUp = [[0] for j in range(4*10**5+10)]


for i in range(n):
  aa = list(input().split())
  aa[0] = int(aa[0])
  aa[1] = int(aa[1])
  if aa[-1] == "U":
    rUp[aa[1]+aa[0] + st].append(aa[0])
    lUp[aa[1]-aa[0] + st].append(aa[0])
    dataU.append(aa)
  elif aa[-1] == "R":
    dataR.append(aa)
  elif aa[-1] == "D":
    rDown[aa[1]-aa[0] + st].append(aa[0])
    lDown[aa[1]+aa[0] + st].append(aa[0])
    dataD.append(aa)
  elif aa[-1] == "L":
    dataR.append(aa)
ans = 10**10
for i in range(len(dataR)):
  if rDown[dataR[i][1]-dataR[i][0] + st] != []:
    rDown[dataR[i][1]-dataR[i][0] + st].sort()
    idx = bisect_left(rDown[dataR[i][1]-dataR[i][0] + st], dataR[i][0])
    if idx < len(rDown[dataR[i][1]-dataR[i][0] + st]):
      ans = min(ans, 10*(rDown[dataR[i][1]-dataR[i][0] + st][idx] - dataR[i][0]))
  if rUp[dataR[i][1]+dataR[i][0] + st] != []:
    rUp[dataR[i][1]+dataR[i][0] + st].sort()
    idx = bisect_left(rUp[dataR[i][1]+dataR[i][0] + st], dataR[i][0])
    if idx < len(rUp[dataR[i][1]+dataR[i][0] + st]):
      ans = min(ans, 10*(rUp[dataR[i][1]+dataR[i][0] + st] - dataR[i][0]))

  
print(ans)

Submission status: Runtime Error

Solution code:
from bisect import bisect_left
n = int(input())
st = 2*10**5+5
dataU = []
dataR = []
dataD = []
dataL = []
rDown = [[] for j in range(4*10**5+10)]
rUp = [[0] for j in range(4*10**5+10)]
lDown = [[0] for j in range(4*10**5+10)]
lUp = [[0] for j in range(4*10**5+10)]


for i in range(n):
  aa = list(input().split())
  if aa[-1] == "U":
    rUp[aa[1]+aa[0] + st].append(aa[0])
    lUp[aa[1]-aa[0] + st].append(aa[0])
    dataU.append(aa)
  elif aa[-1] == "R":
    dataR.append(aa)
  elif aa[-1] == "D":
    rDown[aa[1]-aa[0] + st].append(aa[0])
    lDown[aa[1]+aa[0] + st].append(aa[0])
    dataD.append(aa)
  elif aa[-1] == "L":
    dataR.append(aa)
ans = 10**10
for i in range(dataR):
  if rDown[dataR[i][1]-dataR[i][0] + st] != []:
    rDown[dataR[i][1]-dataR[i][0] + st].sort()
    idx = bisect_left(rDown[dataR[i][1]-dataR[i][0] + st], dataR[i][0])
    if idx < len(rDown[dataR[i][1]-dataR[i][0] + st]):
      ans = min(ans, 10*(rDown[dataR[i][1]-dataR[i][0] + st][idx] - dataR[i][0]))
  if rUp[dataR[i][1]+dataR[i][0] + st] != []:
    rUp[dataR[i][1]+dataR[i][0] + st].sort()
    idx = bisect_left(rUp[dataR[i][1]+dataR[i][0] + st], dataR[i][0])
    if idx < len(rUp[dataR[i][1]+dataR[i][0] + st]):
      ans = min(ans, 10*(rUp[dataR[i][1]+dataR[i][0] + st] - dataR[i][0]))
print(ans)
    

print(dataR)

Submission status: Runtime Error

Solution code:
n = int(input())
u_list = []
r_list = []
d_list = []
l_list = []
for _ in range(n):
    x, y, u = input().split()
    if u == "U":
        u_list.append([int(x), int(y)])
    elif u == "R":
        r_list.append([int(x), int(y)])
    elif u == "D":
        d_list.append([int(x), int(y)])
    else:
        l_list.append([int(x), int(y)])

ans = float("INF")

# LtoR
l_list.sort(key=lambda x:x[1])
r_list.sort(key=lambda x:x[1])
l1 = []
for _, i in l_list:
    l1.append(i)
r1 = []
for _, i in r_list:
    r1.append(i)
for i in range(len(l1)):
    if l1[i] in r1:
        index = bisect.bisect_left(r1, l1[i])
        ans = min(ans, (l_list[i][0]-r_list[index][0])*5)

# UtoD
u_list.sort(key=lambda x:x[0])
d_list.sort(key=lambda x:x[0])
u1 = []
for i, _ in u_list:
    u1.append(i)
d1 = []
for i, _ in d_list:
    d1.append(i)
for i in range(len(u1)):
    if u1[i] in d1:
        index = bisect.bisect_left(d1, u1[i])
        ans = min(ans, (d_list[i][1]-u_list[index][1])*5)
    
l1 = []
r1 = []
u1 = []
d1 = []
for i, j in l_list:
    l1.append(i+j)
for i, j in r_list:
    r1.append(-i+j)
for i, j in d_list:
    d1.append(i+j)
for i, j in u_list:
    u1.append(-i+j)
# LtoU
for i in range(len(l1)):
    if l1[i] in u1:
        ans = min(ans, abs(l_list[i][0]-l_list[i][1]))
    if l1[i] in d1:
        ans = min(ans, abs(l_list[i][0]-l_list[i][1]))
for i in range(len(r1)):
    if r1[i] in u1:
        ans = min(ans, abs(r_list[i][0]-r_list[i][1]))
    if r1[i] in d1:
        ans = min(ans, abs(r_list[i][0]-r_list[i][1]))
if ans == float("INF"):
    print("SAFE")
else:
    print(ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

from operator import itemgetter

n = readint()

ans = 10**10

u = []
d = []
l = []
r = []
uu = [[]for i in range(2*10**5+1)]
dd = [[]for i in range(2*10**5+1)]
ll = [[]for i in range(2*10**5+1)]
rr = [[]for i in range(2*10**5+1)]
for i in range(n):
    x,y,a = readstrs()
    x = int(x)
    y = int(y)
    if a=='U':
        u.append([x,y,x+y,x-y])
        uu[x].append(y)
    elif a == 'D':
        d.append([x,y,x+y,x-y])
        dd[x].append(y)
    elif a == 'L':
        l.append([x,y,x+y,x-y])
        ll[y].append(x)
    elif a == 'R':
        r.append([x,y,x+y,x-y])
        rr[y].append(x)

for i in range(2*10**5+1):
    if uu[i] and dd[i]:
        uu[i].sort()
        dd[i].sort()
        lenu = len(uu[i])
        lend = len(dd[i])
        k = 0
        j = 0
        while k<lenu and j<lend:
            if dd[i][j]>uu[i][k]:
                ans = min(ans,(dd[i][j]-uu[i][k])//2)
                k += 1
            else:
                j += 1
    if ll[i] and rr[i]:
        ll[i].sort()
        rr[i].sort()
        lenl = len(ll[i])
        lenr = len(rr[i])
        k = 0
        j = 0
        while k<lenl and j<lenr:
            if ll[i][j]>rr[i][k]:
                ans = min(ans,(ll[i][j]-rr[i][k])//2)
                k += 1
            else:
                j += 1

uu = [[]for i in range(4*10**5+1)]
dd = [[]for i in range(4*10**5+1)]
ll = [[]for i in range(4*10**5+1)]
rr = [[]for i in range(4*10**5+1)]

for a in u:
    uu[a[3]].append(a[0])
for a in d:
    dd[a[3]].append(a[0])    
for a in l:
    ll[a[3]].append(a[0])
for a in r:
    rr[a[3]].append(a[0])

for i in range(4*10**5+1):
    if uu[i] and ll[i]:
        uu[i].sort()
        ll[i].sort()
        lenu = len(uu[i])
        lenl = len(ll[i])
        k = 0
        j = 0
        while k<lenu and j<lenl:
            if ll[i][j]>uu[i][k]:
                ans = min(ans,ll[i][j]-uu[i][k])
                k += 1
            else:
                j += 1
    if dd[i] and rr[i]:
        dd[i].sort()
        rr[i].sort()
        lend = len(dd[i])
        lenr = len(rr[i])
        k = 0
        j = 0
        while k<lend and j<lenr:
            if dd[i][j]>rr[i][k]:
                ans = min(ans,dd[i][j]-rr[i][k])
                k += 1
            else:
                j += 1

        
uu = [[]for i in range(4*10**5+1)]
dd = [[]for i in range(4*10**5+1)]
ll = [[]for i in range(4*10**5+1)]
rr = [[]for i in range(4*10**5+1)]

for a in u:
    uu[a[2]].append(a[0])
for a in d:
    dd[a[2]].append(a[0])    
for a in l:
    ll[a[2]].append(a[0])
for a in r:
    rr[a[2]].append(a[0])

for i in range(4*10**5+1):
    if uu[i] and rr[i]:
        uu[i].sort()
        rr[i].sort()
        lenu = len(uu[i])
        lenr = len(rr[i])
        k = 0
        j = 0
        while k<lenu and j<lenr:
            if rr[i][j]<uu[i][k]:
                ans = min(ans,-rr[i][j]+uu[i][k])
                k += 1
            else:
                j += 1
    if dd[i] and ll[i]:
        dd[i].sort()
        ll[i].sort()
        lend = len(dd[i])
        lenl = len(ll[i])
        k = 0
        j = 0
        while k<lend and j<lenl:
            if dd[i][j]<ll[i][k]:
                ans = min(ans,-dd[i][j]+ll[i][k])
                k += 1
            else:
                j += 1

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

Submission status: Runtime Error

Solution code:
n = int(input())
b = 2000001
x = []
u = []
r = []
d = []
l = []
for i in range(n):
    A = list(input().split())
    a = [int(A[0]), int(A[1])]
    if A[2] == "U":
        u.append(a)
    elif A[2] == "R":
        r.append(a)
    elif A[2] == "D":
        d.append(a)
    else:
        l.append(a)
#ud
u1 = {}
d1 = {}
for i in range(len(u)):
    u1[u[i][0]] = u[i][1]
for i in range(len(d)):
    d1[d[i][0]] = d[i][1]
for i in range(200001):
    if i in u1 and i in d1:
        if u1[i] < d1[i]:
            b = min(b, (d1[i]-u1[i])*5)
#rl
r1 = {}
l1 = {}
for i in range(len(r)):
    r1[d[i][1]] = d[i][0]
for i in range(len(l)):
    l1[u[i][1]] = u[i][0]
for i in range(200001):
    if i in r1 and i in l1:
        if r1[i] < l1[i]:
            b = min(b, (l1[i]-r1[i])*5)
#ur
u2 = {}
r2 = {}
for i in range(len(u)):
    u2[u[i][0]+u[i][1]] = u[i][0]
for i in range(len(r)):
    r2[r[i][0]+r[i][1]] = r[i][0]
for i in range(400001):
    if i in u2 and i in r2:
        if r2[i] < u2[i]:
            b = min(b, (u2[i]-r2[i])*10)
#ld
l2 = {}
d2 = {}
for i in range(len(l)):
    l2[l[i][0]+l[i][1]] = l[i][0]
for i in range(len(d)):
    d2[d[i][0]+d[i][1]] = d[i][0]
for i in range(400001):
    if i in l2 and i in d2:
        if d2[i] < l2[i]:
            b = min(b, (l2[i]-d2[i])*10)
#lu
l3 = {}
u3 = {}
for i in range(len(l)):
    l3[l[i][0]-l[i][1]] = l[i][0]
for i in range(len(u)):
    l3[u[i][0]-u[i][1]] = u[i][0]
for i in range(-200000, 2000001):
    if i in l3 and i in u3:
        if u3[i] < l3[i]:
            b = min(b, (l3[i]-u3[i])*10)
#dr
d3 = {}
r3 = {}
for i in range(len(d)):
    d3[d[i][0]-d[i][1]] = d[i][0]
for i in range(len(r)):
    r3[r[i][0]-r[i][1]] = r[i][0]
for i in range(-200000, 2000001):
    if i in d3 and i in r3:
        if r3[i] < d3[i]:
            b = min(b, (d3[i]-r3[i])*10)
if b == 2000001:
    print("SAFE")
else:
    print(b)

Submission status: Runtime Error

Solution code:
#m-solutions2020 f

import sys
input = sys.stdin.readline

N = int(input())
U =[]
D =[]
R =[]
L =[]
for _ in range(N):
    x,y,a=list(input().rstrip().split())
    if a == 'U':
        U.append([int(x),int(y)])
    elif a == 'D':
        D.append([int(x),int(y)])
    elif a == 'R':
        R.append([int(x),int(y)])
    elif a == 'L':
        L.append([int(x),int(y)])

ans = float('inf')
U = tuple(U)
D = tuple(D)
R = tuple(R)
L = tuple(L)


for r in R:
    for l in L:
        if r[1] == l[1] and r[0]<=l[0]:#yが一致してxが近づくならぶつかる
            ans = min(ans,(l[0]-r[0])*5)
    for u in U:
        if (u[0]-r[0]>=0) and (r[1]-u[1]>=0) and (u[0]-r[0] == r[1]-u[1]):
            ans = min(ans,(u[0]-l[0])*10)
    for d in D:
        if (d[0]-r[0]>=0) and (d[1]-r[1]>=0) and (d[0]-r[0] == d[1]-r[1]):
            ans = min(ans,(d[0]-r[0])*10)

for l in L:
    for u in U:
        if (l[0]-u[0]>=0) and (l[1]-u[1]>=0) and (l[0]-u[0] == l[1]-u[1]):
            ans = min(ans,(l[0]-u[0])*10)
    for d in D:
        if (l[0]-d[0]>=0) and (d[1]-l[1]>=0) and (l[0]-d[0] == d[1]-l[1]):
            ans = min(ans,(l[0]-d[0])*10)

for u in U:
    for d in D:
        if u[0] == d[0] and u[1]<=d[1]:
            ans = min(ans,(d[1]-u[1])*5)

            
print('SAFE' if ans == float('inf') else ans)

Submission status: Runtime Error

Solution code:
n = int(input())
b = 2000001
x = []
u = []
r = []
d = []
l = []
for i in range(n):
    A = list(input().split())
    a = [int(A[0]), int(A[1])]
    if A[2] == "U":
        u.append(a)
    elif A[2] == "R":
        r.append(a)
    elif A[2] == "D":
        d.append(a)
    else:
        l.append(a)
#ud
u1 = {}
d1 = {}
for i in range(len(u)):
    u1[u[i][0]] = u[i][1]
for i in range(len(d)):
    d1[d[i][0]] = d[i][1]
for i in range(200001):
    if i in u1 and i in d1:
        if u1[i] < d1[i]:
            b = min(b, (d1[i]-u1[i])*5)
#rl
r1 = {}
l1 = {}
for i in range(len(r)):
    r1[d[i][1]] = d[i][0]
for i in range(len(l)):
    l1[u[i][1]] = u[i][0]
for i in range(200001):
    if i in r1 and i in l1:
        if r1[i] < l1[i]:
            b = min(b, (l1[i]-r1[i])*5)
#ur
u2 = {}
r2 = {}
for i in range(len(u)):
    u2[u[i][0]+u[i][1]] = u[i][0]
for i in range(len(r)):
    r2[r[i][0]+r[i][1]] = r[i][0]
for i in range(400001):
    if i in u2 and i in r2:
        if r2[i] < u2[i]:
            b = min(b, (u2[i]-r2[i])*10)
#ld
l2 = {}
d2 = {}
for i in range(len(l)):
    l2[l[i][0]+l[i][1]] = l[i][0]
for i in range(len(d)):
    d2[d[i][0]+d[i][1]] = d[i][0]
for i in range(400001):
    if i in l2 and i in d2:
        if d2[i] < l2[i]:
            b = min(b, (l2[i]-d2[i])*10)
#lu
l3 = {}
u3 = {}
for i in range(len(l)):
    l3[l[i][0]-l[i][1]] = l[i][0]
for i in range(len(u)):
    l3[u[i][0]-u[i][1]] = u[i][0]
for i in range(400001):
    if i in l3 and i in u3:
        if u3[i] < l3[i]:
            b = min(b, (l3[i]-u3[i])*10)
#dr
d3 = {}
r3 = {}
for i in range(len(d)):
    d3[d[i][0]-d[i][1]] = d[i][0]
for i in range(len(r)):
    r3[r[i][0]-r[i][1]] = r[i][0]
for i in range(400001):
    if i in d3 and i in r3:
        if r3[i] < d3[i]:
            b = min(b, (d3[i]-r3[i])*10)
if b == 2000001:
    print("SAFE")
else:
    print(b)

Submission status: Runtime Error

Solution code:
N = int(input())
U = list(); D = list(); R = list(); L = list()
for i in range(N):
  X, Y, direction = [str(x) for x in input().split()]
  if(direction == 'U'):
    U.append([int(X), int(Y)])
  elif(direction == 'D'):
    D.append([int(X), int(Y)])
  elif(direction == 'R'):
    R.append([int(X), int(Y)])
  elif(direction == 'L'):
    L.append([int(X), int(Y)])

ans = list()
for i in range(len(U)):
  for j in range(len(D)):
    if((U[i][0]==D[j][0]) and (U[i][1]>D[j][1])):
      ans.append(U[i][1] - D[j][1])
for i in range(len(R)):
  for j in range(len(L)):
    if((R[i][1]==L[i][1]) and (R[i][0]<L[j][0])):
      ans.append(L[j][0]-R[i][0])
for i in range(len(U)):
  for j in range(len(R)):
    if((U[i][1]<R[j][1]) and (U[i][0]>R[j][0]) and ((U[i][0]-R[j][0])==(R[j][1]-U[j][1]))):
      ans.append(U[i][0]-R[j][0])
for i in range(len(U)):
  for j in range(len(L)):
    if((U[i][1]<L[j][1]) and (U[i][0]<L[j][0]) and ((L[j][0]-U[i][0])==(L[j][1]-U[j][1]))):
      ans.append(L[j][0]-U[i][0])

for i in range(len(D)):
  for j in range(len(R)):
    if((D[i][1]>R[j][1]) and (D[i][0]>R[j][0]) and ((D[i][0]-R[j][0])==(D[j][1]-R[j][1]))):
      ans.append(D[i][0]-R[j][0])
for i in range(len(D)):
  for j in range(len(L)):
    if((D[i][1]>L[j][1]) and (D[i][0]<L[j][0]) and ((L[j][0]-D[i][0])==(D[j][1]-L[j][1]))):
      ans.append(L[j][0]-D[i][0])
if not ans:
  print('SAFE')
else:
  print(min(ans)*10)

Submission status: Runtime Error

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

Submission status: Runtime Error

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))
    if u in 'UD':
        max_x = max(max_x, int(x))
        min_x = min(min_x, int(x))
    else:
        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(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: Runtime Error

Solution code:
#m-solutions2020 f

import sys
input = sys.stdin.readline

N = int(input())
U =[]
D =[]
R =[]
L =[]
for _ in range(N):
    x,y,u=list(input().rstrip().split())
    if u == 'U':
        U.append([int(x),int(y)])
    elif u == 'D':
        D.append([int(x),int(y)])
    elif u == 'R':
        R.append([int(x),int(y)])
    elif u == 'L':
        L.append([int(x),int(y)])

ans = float('inf')

for r in R:
    for l in L:
        if r[1] == l[1] and r[0]<=l[0]:#yが一致してxが近づくならぶつかる
            ans = min(ans,(l[0]-r[0])*5)
    for u in U:
        if (u[0]-r[0]>0) and (r[1]-u[1]>0) and (u[0]-r[0] == r[1]-u[1]):
            ans = min(ans,(u[0]-l[0])*10)
    for d in D:
        if (d[0]-r[0]>0) and (d[1]-r[1]>0) and (d[0]-r[0] == d[1]-r[1]):
            ans = min(ans,(d[0]-r[0])*10)

for l in L:
    for u in U:
        if (l[0]-u[0]>0) and (l[1]-u[1]>0) and (l[0]-u[0] == l[1]-u[1]):
            ans = min(ans,(l[0]-u[0])*10)
    for d in D:
        if (l[0]-d[0]>0) and (d[1]-l[1]>0) and (l[0]-d[0] == d[1]-l[1]):
            ans = min(ans,(l[0]-d[0])*10)

for u in U:
    for d in D:
        if u[0] == d[0] and u[1]<=d[1]:
            ans = min(ans,(d[1]-u[1])*5)

            
print('SAFE' if ans == float('inf') else ans)

Submission status: Runtime Error

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))
    if u in 'UD':
        max_x = max(max_x, int(x))
        min_x = min(min_x, int(x))
    else:
        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(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: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
x = [[] for _ in range(200000)]
y = [[] for _ in range(200000)]
z = [[] for _ in range(400000)]
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])
    state = True
    n = -1
    for xxi in xx:
      if xxi[1] == 1:
        n = xxi[0]
      else:
        if n != -1:
          if (xxi[0] - n)*5 < t:
            t = (xxi[0]-n)*5
          n = -1
for xx in y:
  if len(xx) >= 2:
    xx.sort(key=lambda a:a[0])
    state = True
    n = -1
    for xxi in xx:
      if xxi[1] == 1:
        n = xxi[0]
      else:
        if n != -1:
          if (xxi[0] - n)*5 < t:
            t = (xxi[0] - n)*5
          n = -1
for xx in z:
  if len(xx) >= 2:
    xx.sort(key=lambda a:a[0])
    state = True
    n = -1
    for xxi in xx:
      if xxi[1] == 1:
        n = xxi[0]
      else:
        if n != -1:
          if (xxi[0] - n)*10 < t:
            t = (xxi[0] - n)*10
          n = -1                
if t == float("inf"):
  print("SAFE")
else:
  print(t)