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:
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
xyu = [readChars() for i in range(n)]

class mydict:
	def __init__(self):
		self.d = {}
	def add(self,key,value):
		if key in self.d:
			self.d[key].append(value)
		else:
			self.d[key]=[value]
	def p(self):
		print(self.d)
	def mysort(self):
		for i in self.d:
			self.d[i].sort(key=lambda x:x[0])
	def getDict(self):
		return self.d;

ud = mydict()
rl = mydict()
for x,y,u in xyu:
	if u=="U" or u=="D":
		ud.add(x,[y,u])
	else:
		rl.add(y,[x,u])
ud.mysort()
rl.mysort()
#ud.p()
#rl.p()
ans = float("inf")

tar = ud.getDict()
for key in tar:
	s = ""
	be = "-1"
	bev = "-1"
	for v in tar[key]:
		if be=="U" and v[1]=="D":
			ans = min(ans,(int(v[0])-int(bev))*5)
		be = v[1]
		bev = v[0]
tar = rl.getDict()
for key in tar:
	s = ""
	be = "-1"
	bev = "-1"
	for v in tar[key]:
		if be=="R" and v[1]=="L":
			ans = min(ans,(int(v[0])-int(bev))*5)
		be = v[1]
		bev = v[0]

xy = mydict()
for x,y,u in xyu:
	xy.add(int(x)+int(y),[int(x),int(y),u])
#xy.p()
xy.mysort()
d = xy.getDict()
for key in d:
	beu = "-1"
	bex = -1
	bey = -1
	for arr in d[key]:
		if beu=="R" and arr[2]=="U":
			ans = min(ans,(arr[0]-bex)*10)
		if beu=="D" and arr[2]=="L":
			ans = min(ans,(arr[0]-bex)*10)
		beu = arr[2]
		bex = arr[0]
mxy = mydict()
for x,y,u in xyu:
	mxy.add(int(y)-int(x),[int(x),int(y),u])
mxy.mysort()
d = mxy.getDict()
for key in d:
	beu = "-1"
	bex = -1
	bey = -1
	for arr in d[key]:
		if beu=="R" and arr[2]=="D":
			ans = min(ans,(arr[0]-bex)*10)
		if beu=="U" and arr[2]=="L":
			ans = min(ans,(arr[0]-bex)*10)
		beu = arr[2]
		bex = arr[0]

if ans==float("inf"):
	print("SAFE")
else:
	print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
import copy
n = int(input())
xyu = [input().split() for i in range(n)]

lxy = 2*10**5

rh = [[] for i in range(lxy+1)]
lf = [[] for i in range(lxy+1)]

up = [[] for i in range(lxy+1)]
dw = [[] for i in range(lxy+1)]

ldg = 4*10**5

dgrh = [[] for i in range(ldg+1)]
dglf = [[] for i in range(ldg+1)]

dgdrh = [[] for i in range(ldg+1)]
dgdlf = [[] for i in range(ldg+1)]

dgup = [[] for i in range(ldg+1)]
dgdw = [[] for i in range(ldg+1)]


for i in range(n):
    if xyu[i][2] == 'U':
        heapq.heappush(up[int(xyu[i][0])],int(xyu[i][1]))
        heapq.heappush(dgup[int(xyu[i][0])-int(xyu[i][1])+lxy],int(xyu[i][1]))
    if xyu[i][2] == 'D':
        heapq.heappush(dw[int(xyu[i][0])],int(xyu[i][1]))
        heapq.heappush(dgdw[ldg-int(xyu[i][0])-int(xyu[i][1])],int(xyu[i][1]))
    if xyu[i][2] == 'R':
        heapq.heappush(rh[int(xyu[i][1])],int(xyu[i][0]))
        heapq.heappush(dgrh[ldg-int(xyu[i][0])-int(xyu[i][1])],int(xyu[i][1]))
        heapq.heappush(dgdrh[ldg-int(xyu[i][0])-int(xyu[i][1])],int(xyu[i][1]))
    if xyu[i][2] == 'L':
        heapq.heappush(lf[int(xyu[i][1])],int(xyu[i][0]))
        heapq.heappush(dglf[int(xyu[i][0])-int(xyu[i][1])+lxy],int(xyu[i][1]))
        heapq.heappush(dgdlf[int(xyu[i][0])-int(xyu[i][1])+lxy],int(xyu[i][1]))

ans = 10**100



for i in range(2*10**5+1):
    tdw = -1
    while len(up[i]) > 0:
        tup = heapq.heappop((up[i]))
        while len(dw[i]) > 0 and tup > tdw:
            tdw = heapq.heappop(dw[i])
        if tup < tdw:
            ans = min((tdw-tup)*5, ans)
            
    tlf = -1
    while len(rh[i]) > 0:
       trh = heapq.heappop((rh[i]))
       while len(lf[i]) > 0 and trh > tlf:
           tlf = heapq.heappop(lf[i])
       if trh < tlf:
           ans = min((tlf-trh)*5, ans)

#print(ans)
for i in range(4*10**5+1):   
    tdgrh = -1
    tdglf = -1
    while len(dgup[i]) > 0:
        tdgup = heapq.heappop((dgup[i]))
        #print(tdgup)
        while len(dgrh[i]) > 0 and tdgup > tdgrh:
            tdgrh = heapq.heappop(dgrh[i])
        if tdgup < tdgrh:
            ans = min((tdgrh-tdgup)*10, ans)
        while len(dglf[i]) > 0 and tdgup > tdglf:
            tdglf = heapq.heappop(dglf[i])
        if tdgup < tdglf:
            ans = min((tdglf-tdgup)*10, ans)
            
    tdgrh = 10**100
    tdglf = 10**100
    while len(dgdw[i]) > 0:
        tdgup = heapq.heappop((dgdw[i]))
        while len(dgdrh[i]) > 0 and tdgup < tdgrh:
            tdgrh = heapq.heappop(dgdrh[i])
        if tdgup > tdgrh:
            ans = min((-tdgrh+tdgup)*10, ans)
        while len(dgdlf[i]) > 0 and tdgup < tdglf:
            tdglf = heapq.heappop(dgdlf[i])
        if tdgup > tdglf:
            ans = min((-tdglf+tdgup)*10, ans)
        

#print(ans)
#exit()

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

Submission status: Wrong Answer

Solution code:
_, *a = map(str.split, open(0))
U, R, D, L = "URDL"
INF = float("inf")
d = {}
for *z, u in a:
  d.setdefault(u, []).append([*map(int, z)])
N = INF
for A, B, S in [(U, D, "x, y"), (R, L, "y, x"), 
                (R, U, "x+y, x-y"), (D, L, "x+y, x-y"), 
                (U, L, "x-y, x+y"), (R, D, "x-y, x+y")]:
  p = -INF
  L = eval(f"[({S}, Q) for P, Q in [(A, 1), (B, 0)] for x, y in d.get(P,[])]")
  for _, y, r in sorted(L):
    if r:p = y
    else:N = min(N, (y - p)*5)
print("SAFE" if N == INF else N)

Submission status: Wrong Answer

Solution code:
import heapq
import copy
n = int(input())
xyu = [input().split() for i in range(n)]

lxy = 2*10**5

rh = [[] for i in range(lxy+1)]
lf = [[] for i in range(lxy+1)]

up = [[] for i in range(lxy+1)]
dw = [[] for i in range(lxy+1)]

ldg = 4*10**5

dgrh = [[] for i in range(ldg+1)]
dglf = [[] for i in range(ldg+1)]

dgdrh = [[] for i in range(ldg+1)]
dgdlf = [[] for i in range(ldg+1)]

dgup = [[] for i in range(ldg+1)]
dgdw = [[] for i in range(ldg+1)]


for i in range(n):
    if xyu[i][2] == 'U':
        heapq.heappush(up[int(xyu[i][0])],int(xyu[i][1]))
        heapq.heappush(dgup[int(xyu[i][0])-int(xyu[i][1])+lxy],int(xyu[i][1]))
    if xyu[i][2] == 'D':
        heapq.heappush(dw[int(xyu[i][0])],int(xyu[i][1]))
        heapq.heappush(dgdw[ldg-int(xyu[i][0])-int(xyu[i][1])],int(xyu[i][1]))
    if xyu[i][2] == 'R':
        heapq.heappush(rh[int(xyu[i][1])],int(xyu[i][0]))
        heapq.heappush(dgrh[ldg-int(xyu[i][0])-int(xyu[i][1])],int(xyu[i][1]))
        heapq.heappush(dgdrh[ldg-int(xyu[i][0])-int(xyu[i][1])],int(xyu[i][1]))
    if xyu[i][2] == 'L':
        heapq.heappush(lf[int(xyu[i][1])],int(xyu[i][0]))
        heapq.heappush(dglf[int(xyu[i][0])-int(xyu[i][1])+lxy],int(xyu[i][1]))
        heapq.heappush(dgdlf[int(xyu[i][0])-int(xyu[i][1])+lxy],int(xyu[i][1]))

ans = 10**100



for i in range(2*10**5+1):
    tdw = -1
    while len(up[i]) > 0:
        tup = heapq.heappop((up[i]))
        while len(dw[i]) > 0 and tup > tdw:
            tdw = heapq.heappop(dw[i])
        if tup < tdw:
            ans = min((tdw-tup)*5, ans)
            
    tlf = -1
    while len(rh[i]) > 0:
       trh = heapq.heappop((rh[i]))
       while len(lf[i]) > 0 and trh > tlf:
           tlf = heapq.heappop(lf[i])
       if trh < tlf:
           ans = min((tlf-trh)*5, ans)

for i in range(4*10**5+1):   
    tdgrh = -1
    tdglf = -1
    while len(dgup[i]) > 0:
        tdgup = heapq.heappop((dgup[i]))
        #print(tdgup)
        while len(dgrh[i]) > 0 and tdgup > tdgrh:
            tdgrh = heapq.heappop(dgrh[i])
        if tdgup < tdgrh:
            ans = min((tdgrh-tdgup)*10, ans)
        while len(dglf[i]) > 0 and tdgup > tdglf:
            tdglf = heapq.heappop(dglf[i])
        if tdgup < tdglf:
            ans = min((tdglf-tdgup)*10, ans)
            
    tdgrh = 10**100
    tdglf = 10**100
    while len(dgdw[i]) > 0:
        tdgup = heapq.heappop((dgdw[i]))
        while len(dgdrh[i]) > 0 and tdgup < tdgrh:
            tdgrh = heapq.heappop(dgdrh[i])
        if tdgup > tdgrh:
            ans = min((-tdgrh+tdgup)*10, ans)
        while len(dgdlf[i]) > 0 and tdgup < tdglf:
            tdglf = heapq.heappop(dgdlf[i])
        if tdgup < tdglf:
            ans = min((-tdglf+tdgup)*10, ans)
        

#print(ans)
#exit()

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
xyu = [readChars() for i in range(n)]

class mydict:
	def __init__(self):
		self.d = {}
	def add(self,key,value):
		if key in self.d:
			self.d[key].append(value)
		else:
			self.d[key]=[value]
	def p(self):
		print(self.d)
	def mysort(self):
		for i in self.d:
			self.d[i].sort(key=lambda x:x[0])
	def getDict(self):
		return self.d;

ud = mydict()
rl = mydict()
for x,y,u in xyu:
	if u=="U" or u=="D":
		ud.add(x,[y,u])
	else:
		rl.add(y,[x,u])
ud.mysort()
rl.mysort()
#ud.p()
#rl.p()
ans = float("inf")

tar = ud.getDict()
for key in tar:
	s = ""
	be = "-1"
	bev = "-1"
	for v in tar[key]:
		if be=="U" and v[1]=="D" and int(bev)<=int(v[0]):
			ans = min(ans,(int(v[0])-int(bev))*5)
		be = v[1]
		bev = v[0]
tar = rl.getDict()
for key in tar:
	s = ""
	be = "-1"
	bev = "-1"
	for v in tar[key]:
		if be=="R" and v[1]=="L":
			ans = min(ans,(int(v[0])-int(bev))*5)
		be = v[1]
		bev = v[0]

xy = mydict()
for x,y,u in xyu:
	xy.add(int(x)+int(y),[int(x),int(y),u])
#xy.p()
xy.mysort()
d = xy.getDict()
for key in d:
	beu = "-1"
	bex = -1
	bey = -1
	for arr in d[key]:
		if beu=="R" and arr[2]=="U":
			ans = min(ans,(arr[0]-bex)*10)
		if beu=="D" and arr[2]=="L":
			ans = min(ans,(arr[0]-bex)*10)
		beu = arr[2]
		bex = arr[0]
mxy = mydict()
for x,y,u in xyu:
	mxy.add(int(y)-int(x),[int(x),int(y),u])
mxy.mysort()
d = mxy.getDict()
for key in d:
	beu = "-1"
	bex = -1
	bey = -1
	for arr in d[key]:
		if beu=="R" and arr[2]=="D":
			ans = min(ans,(arr[0]-bex)*10)
		if beu=="U" and arr[2]=="L":
			ans = min(ans,(arr[0]-bex)*10)
		beu = arr[2]
		bex = arr[0]

if ans==float("inf"):
	print("SAFE")
else:
	print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
import copy
n = int(input())
xyu = [input().split() for i in range(n)]

lxy = 2*10**5

rh = [[] for i in range(lxy+1)]
lf = [[] for i in range(lxy+1)]

up = [[] for i in range(lxy+1)]
dw = [[] for i in range(lxy+1)]

ldg = 4*10**5

dgrh = [[] for i in range(ldg+1)]
dglf = [[] for i in range(ldg+1)]

dgdrh = [[] for i in range(ldg+1)]
dgdlf = [[] for i in range(ldg+1)]

dgup = [[] for i in range(ldg+1)]
dgdw = [[] for i in range(ldg+1)]


for i in range(n):
    if xyu[i][2] == 'U':
        heapq.heappush(up[int(xyu[i][0])],int(xyu[i][1]))
        heapq.heappush(dgup[int(xyu[i][0])-int(xyu[i][1])+lxy],int(xyu[i][1]))
    if xyu[i][2] == 'D':
        heapq.heappush(dw[int(xyu[i][0])],int(xyu[i][1]))
        heapq.heappush(dgdw[ldg-int(xyu[i][0])-int(xyu[i][1])],int(xyu[i][1]))
    if xyu[i][2] == 'R':
        heapq.heappush(rh[int(xyu[i][1])],int(xyu[i][0]))
        heapq.heappush(dgrh[ldg-int(xyu[i][0])-int(xyu[i][1])],int(xyu[i][1]))
        heapq.heappush(dgdrh[ldg-int(xyu[i][0])-int(xyu[i][1])],int(xyu[i][1]))
    if xyu[i][2] == 'L':
        heapq.heappush(lf[int(xyu[i][1])],int(xyu[i][0]))
        heapq.heappush(dglf[int(xyu[i][0])-int(xyu[i][1])+lxy],int(xyu[i][1]))
        heapq.heappush(dgdlf[int(xyu[i][0])-int(xyu[i][1])+lxy],int(xyu[i][1]))

ans = 10**100



for i in range(2*10**5):
    tdw = -1
    while len(up[i]) > 0:
        tup = heapq.heappop((up[i]))
        while len(dw[i]) > 0 and tup > tdw:
            tdw = heapq.heappop(dw[i])
        if tup < tdw:
            ans = min((tdw-tup)*5, ans)
            
    tlf = -1
    while len(rh[i]) > 0:
       trh = heapq.heappop((rh[i]))
       while len(lf[i]) > 0 and trh > tlf:
           tlf = heapq.heappop(lf[i])
       if trh < tlf:
           ans = min((tlf-trh)*5, ans)
            
    tdgrh = -1
    tdglf = -1
    while len(dgup[i]) > 0:
        tdgup = heapq.heappop((dgup[i]))
        while len(dgrh[i]) > 0 and tdgup > tdgrh:
            tdgrh = heapq.heappop(dgrh[i])
        if tdgup < tdgrh:
            ans = min((tdgrh-tdgup)*10, ans)
        while len(dglf[i]) > 0 and tdgup > tdglf:
            tdgrh = heapq.heappop(dglf[i])
        if tdgup < tdglf:
            ans = min((tdglf-tdgup)*10, ans)
            
    tdgrh = 10**100
    tdglf = 10**100
    while len(dgdw[i]) > 0:
        tdgup = heapq.heappop((dgdw[i]))
        while len(dgdrh[i]) > 0 and tdgup < tdgrh:
            tdgrh = heapq.heappop(dgdrh[i])
        if tdgup > tdgrh:
            ans = min((-tdgrh+tdgup)*10, ans)
        while len(dgdlf[i]) > 0 and tdgup < tdglf:
            tdgrh = heapq.heappop(dgdlf[i])
        if tdgup < tdglf:
            ans = min((-tdglf+tdgup)*10, ans)
        

#print(ans)
#exit()

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

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())
XYU = [list(input().split()) for _ in range(N)]

XYU = deque(XYU)

ud = {"U": (0, 1), "D": (0, -1), "R": (1, 0), "L": (-1, 0)}

candi = []

for _ in range(N):
    x0, y0, us0 = XYU.pop()
    x0 = int(x0)
    y0 = int(y0)
    u0 = ud[us0]

    for x1, y1, us1 in XYU:
        x1 = int(x1)
        y1 = int(y1)
        u1 = ud[us1]
        if us0 == us1:
            break

        # 直交で衝突しうる場合
        if u0[0] * u1[0] + u0[1] * u1[1] == 0:
            # 衝突する座標
            if u0[0] == 0:
                xx = x0
            else:
                yy = y0

            if u1[0] == 0:
                xx = x1
            else:
                yy = y1

            # xx, yyまでの距離が等しいく向きが正しい
            p0 = (xx - x0, yy - y0)
            p1 = (xx - x1, yy - y1)
            d0 = p0[0] * u0[0] + p0[1] * u0[1]
            d1 = p1[0] * u1[0] + p1[1] * u1[1]
            if d0 == d1 and d0 > 0:
                continue
                # candi.append(abs(p0[0] + p0[1]) * 10)

        # 並行で衝突しうる場合
        elif u0[0] == u1[0] * -1 and u0[1] == u1[1] * -1:
            if u0[0] == 0:
                if x0 == x1:
                    if y0 > y1 and u0[1] == -1:
                        candi.append(abs(y0 - y1) * 5)
                    elif y0 < y1 and u0[1] == 1:
                        candi.append(abs(y0 - y1) * 5)
            else:
                if y0 == y1:
                    if x0 > x1 and u0[0] == -1:
                        candi.append(abs(x0 - x1) * 5)
                    elif x0 < x1 and u0[0] == 1:
                        candi.append(abs(x0 - x1) * 5)


if candi:
    print(max(candi))
else:
    print("SAFE")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
xyu = [readChars() for i in range(n)]

class mydict:
	def __init__(self):
		self.d = {}
	def add(self,key,value):
		if key in self.d:
			self.d[key].append(value)
		else:
			self.d[key]=[value]
	def p(self):
		print(self.d)
	def mysort(self):
		for i in self.d:
			self.d[i].sort(key=lambda x:x[0])
	def getDict(self):
		return self.d;

ud = mydict()
rl = mydict()
for x,y,u in xyu:
	if u=="U" or u=="D":
		ud.add(x,[y,u])
	else:
		rl.add(y,[x,u])
ud.mysort()
rl.mysort()
#ud.p()
#rl.p()
ans = float("inf")

tar = ud.getDict()
for key in tar:
	s = ""
	be = "-1"
	bev = "-1"
	for v in tar[key]:
		if be=="U" and v[1]=="D" and int(bev)<=int(v[0]):
			ans = min(ans,(int(v[0])-int(bev))*5)
		be = v[1]
		bev = v[0]
tar = rl.getDict()
for key in tar:
	s = ""
	be = "-1"
	bev = "-1"
	for v in tar[key]:
		if be=="R" and v[1]=="L":
			ans = min(ans,(int(v[0])-int(bev))*5)
		be = v[1]
		bev = v[0]

xy = mydict()
for x,y,u in xyu:
	xy.add(int(x)+int(y),[int(x),int(y),u])
#xy.p()
xy.mysort()
d = xy.getDict()
for key in d:
	beu = "-1"
	bex = -1
	bey = -1
	for arr in d[key]:
		if beu=="R" and arr[2]=="U":
			ans = min(ans,(arr[0]-bex)*10)
		if beu=="R" and arr[2]=="D":
			ans = min(ans,(arr[0]-bex)*10)
		if beu=="U" and arr[2]=="L":
			ans = min(ans,(arr[0]-bex)*10)
		if beu=="D" and arr[2]=="L":
			ans = min(ans,(arr[0]-bex)*10)
		beu = arr[2]
		bex = arr[0]

if ans==float("inf"):
	print("SAFE")
else:
	print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
xyu = [readChars() for i in range(n)]

class mydict:
	def __init__(self):
		self.d = {}
	def add(self,key,value):
		if key in self.d:
			self.d[key].append(value)
		else:
			self.d[key]=[value]
	def p(self):
		print(self.d)
	def mysort(self):
		for i in self.d:
			self.d[i].sort(key=lambda x:x[0])
	def getDict(self):
		return self.d;

ud = mydict()
rl = mydict()
for x,y,u in xyu:
	if u=="U" or u=="D":
		ud.add(x,[y,u])
	else:
		rl.add(y,[x,u])
ud.mysort()
rl.mysort()
#ud.p()
#rl.p()
ans = float("inf")

tar = ud.getDict()
for key in tar:
	s = ""
	be = "-1"
	bev = "-1"
	for v in tar[key]:
		if be=="U" and v[1]=="D" and int(bev)<=int(v[0]):
			ans = min(ans,(int(v[0])-int(bev))*5)
		be = v[1]
		bev = v[0]
tar = rl.getDict()
for key in tar:
	s = ""
	be = "-1"
	bev = "-1"
	for v in tar[key]:
		if be=="U" and v[1]=="D":
			ans = min(ans,(int(v[0])-int(bev))*5)
		be = v[1]
		bev = v[0]

xy = mydict()
for x,y,u in xyu:
	xy.add(int(x)+int(y),[int(x),int(y),u])
#xy.p()
xy.mysort()
d = xy.getDict()
for key in d:
	beu = "-1"
	bex = -1
	bey = -1
	for arr in d[key]:
		if beu=="R" and arr[2]=="U":
			ans = min(ans,(arr[0]-bex)*10)
		if beu=="R" and arr[2]=="D":
			ans = min(ans,(arr[0]-bex)*10)
		if beu=="U" and arr[2]=="L":
			ans = min(ans,(arr[0]-bex)*10)
		if beu=="D" and arr[2]=="L":
			ans = min(ans,(arr[0]-bex)*10)
		beu = arr[2]
		bex = arr[0]

if ans==float("inf"):
	print("SAFE")
else:
	print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())
XYU = [list(input().split()) for _ in range(N)]

XYU = deque(XYU)

ud = {"U": (0, 1), "D": (0, -1), "R": (1, 0), "L": (-1, 0)}

candi = []

for _ in range(N):
    x0, y0, us0 = XYU.pop()
    x0 = int(x0)
    y0 = int(y0)
    u0 = ud[us0]

    for x1, y1, us1 in XYU:
        x1 = int(x1)
        y1 = int(y1)
        u1 = ud[us1]
        if us0 == us1:
            break

        # 直交で衝突しうる場合
        if u0[0] * u1[0] + u0[1] * u1[1] == 0:
            # 衝突する座標
            if u0[0] == 0:
                xx = x0
            else:
                yy = y0

            if u1[0] == 0:
                xx = x1
            else:
                yy = y1

            # xx, yyまでの距離が等しいく向きが正しい
            p0 = (xx - x0, yy - y0)
            p1 = (xx - x1, yy - y1)
            d0 = p0[0] * u0[0] + p0[1] * u0[1]
            d1 = p1[0] * u1[0] + p1[1] * u1[1]
            if d0 == d1 and d0 > 0:
                candi.append(abs(p0[0] + p0[1]) * 10)

        # 並行で衝突しうる場合
        elif u0[0] == u1[0] * -1 and u0[1] == u1[1] * -1:
            if u0[0] == 0:
                if x0 == x1:
                    if y0 > y1 and u0[1] == -1:
                        # candi.append(abs(y0 - y1) * 5)
                        continue
                    elif y0 < y1 and u0[1] == 1:
                        # candi.append(abs(y0 - y1) * 5)
                        continue
            else:
                if y0 == y1:
                    if x0 > x1 and u0[0] == -1:
                        # candi.append(abs(x0 - x1) * 5)
                        continue
                    elif x0 < x1 and u0[0] == 1:
                        # candi.append(abs(x0 - x1) * 5)
                        continue


if candi:
    print(max(candi))
else:
    print("SAFE")

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())
XYU = [list(input().split()) for _ in range(N)]

XYU = deque(XYU)

ud = {"U": (0, 1), "D": (0, -1), "R": (1, 0), "L": (-1, 0)}

candi = []

for i in range(N):
    x0, y0, us0 = XYU.pop()
    x0 = int(x0)
    y0 = int(y0)
    u0 = ud[us0]

    for x1, y1, us1 in XYU:
        x1 = int(x1)
        y1 = int(y1)
        u1 = ud[us1]
        if us0 == us1:
            break

        # 直交で衝突しうる場合
        if u0[0] * u1[0] + u0[1] * u1[1] == 0:
            # 衝突する座標
            if u0[0] == 0:
                xx = x0
            else:
                yy = y0

            if u1[0] == 0:
                xx = x1
            else:
                yy = y1

            # 飛んでいく方向に衝突座標があり，衝突する？
            p0 = (xx - x0, yy - y0)
            p1 = (xx - x1, yy - y1)

            # xx, yyまでの距離が等しい
            d0 = p0[0] * u0[0] + p0[1] * u0[1]
            d1 = p1[0] * u1[0] + p1[1] * u1[1]
            if d0 == d1 and d0 > 0:
                # print(p0, u0)
                # print(p1, u1)
                # 0の進行方向とxx, yyの向きが一致
                # if u0[0] * p0[0] >= 0 and u0[1] * p0[1] >= 0:
                # 1の進行方向とxx, yyの向きが一致
                # if u1[0] * p1[0] >= 0 and u1[1] * p1[1] >= 0:
                candi.append(abs(p0[0] + p0[1]) * 10)

        # 並行で衝突しうる場合
        elif u0[0] == u1[0] * -1 and u0[1] == u1[1] * -1:
            if u0[0] == 0:
                if x0 == x1:
                    if y0 > y1 and u0[1] == -1:
                        candi.append(abs(y0 - y1) * 5)
                    elif y0 < y1 and u0[1] == 1:
                        candi.append(abs(y0 - y1) * 5)
            else:
                if y0 == y1:
                    if x0 > x1 and u0[0] == -1:
                        candi.append(abs(x0 - x1) * 5)
                    elif x0 < x1 and u0[0] == 1:
                        candi.append(abs(x0 - x1) * 5)


if candi:
    print(max(candi))
else:
    print("SAFE")

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())
XYU = [list(input().split()) for _ in range(N)]

XYU = deque(XYU)

ud = {"U": (0, 1), "D": (0, -1), "R": (1, 0), "L": (-1, 0)}

candi = []

for i in range(N):
    x0, y0, us0 = XYU.pop()
    x0 = int(x0)
    y0 = int(y0)
    u0 = ud[us0]

    for x1, y1, us1 in XYU:
        x1 = int(x1)
        y1 = int(y1)
        u1 = ud[us1]
        if us0 == us1:
            break

        # 直交で衝突しうる場合
        if u0[0] * u1[0] + u0[1] * u1[1] == 0:
            # 衝突する座標
            if u0[0] == 0:
                xx = x0
            else:
                yy = y0

            if u1[0] == 0:
                xx = x1
            else:
                yy = y1

            # 飛んでいく方向に衝突座標があり，衝突する？
            p0 = (xx - x0, yy - y0)
            p1 = (xx - x1, yy - y1)

            # xx, yyまでの距離が等しい
            if p0[0] * u0[0] + p0[1] * u0[1] == p1[0] * u1[0] + p1[1] * u1[1]:
                # 0の進行方向とxx, yyの向きが一致
                # if u0[0] * p0[0] >= 0 and u0[1] * p0[1] >= 0:
                # 1の進行方向とxx, yyの向きが一致
                # if u1[0] * p1[0] >= 0 and u1[1] * p1[1] >= 0:
                candi.append(abs(p0[0] + p0[1]) * 10)

        # 並行で衝突しうる場合
        if u0[0] == u1[0] * -1 and u0[1] == u1[1] * -1:
            if u0[0] == 0:
                if x0 == x1:
                    if y0 > y1 and u0[1] == -1:
                        candi.append(abs(y0 - y1) * 5)
                    elif y0 < y1 and u0[1] == 1:
                        candi.append(abs(y0 - y1) * 5)
            else:
                if y0 == y1:
                    if x0 > x1 and u0[0] == -1:
                        candi.append(abs(x0 - x1) * 5)
                    elif x0 < x1 and u0[0] == 1:
                        candi.append(abs(x0 - x1) * 5)

if candi:
    print(max(candi))
else:
    print("SAFE")

Submission status: Wrong Answer

Solution code:
#

import sys
from bisect import bisect_left, bisect_right
input=sys.stdin.readline

def main():
    N=int(input())
    ds=[[] for i in range(400001)] #i=y-x+200000 el=x
    rs=[[] for i in range(400001)]
    us=[[] for i in range(400001)]
    ls=[[] for i in range(400001)]
    db=[[] for i in range(400001)] #i=y+x el=x
    rb=[[] for i in range(400001)]
    ub=[[] for i in range(400001)]
    lb=[[] for i in range(400001)]
    dy=[[] for i in range(200001)] #i=x el=y
    uy=[[] for i in range(200001)]
    rx=[[] for i in range(200001)] #i=y el=x
    lx=[[] for i in range(200001)]
    
    mint=2*10**8
    for i in range(N):
        x,y,u=input().split()
        x=int(x)
        y=int(y)
        sli=y-x+200000
        bsi=y+x
        
        if u=="U":
            us[sli].append(x)
            ub[bsi].append(x)
            uy[x].append(y)
            
            usi=bisect_left(ls[sli],x)
            if len(ls[sli])>0 and usi<len(ls[sli]):
                mint=min(mint, (ls[sli][usi]-x)*10)
                
            ubi=bisect_right(rb[bsi],x)
            if len(rb[bsi])>0 and ubi!=0:
                mint=min(mint, (x-rb[bsi][ubi-1])*10)
                
            uyi=bisect_left(dy[x],y)
            if len(dy[x])>0 and uyi<len(dy[x]):
                mint=min(mint, (dy[x][uyi]-y)*5)
        elif u=="R":
            rs[sli].append(x)
            rb[bsi].append(x)
            rx[y].append(x)
            
            rsi=bisect_left(ds[sli],x)
            if len(ds[sli])>0 and rsi<len(ds[sli]):
                mint=min(mint, (ds[sli][rsi]-x)*10)
            
            rbi=bisect_left(ub[bsi],x)
            if len(ub[bsi])>0 and rbi<len(ub[bsi]):
                mint=min(mint, (ub[bsi][rbi]-x)*10)
            
            rxi=bisect_left(lx[y],x)
            if len(lx[y])>0 and rxi<len(lx[y]):
                mint=min(mint, (lx[y][rxi]-x)*5)
        elif u=="D":
            ds[sli].append(x)
            db[bsi].append(x)
            dy[x].append(y)
            
            dsi=bisect_right(rs[sli],x)
            if len(rs[sli])>0 and dsi>0:
                mint=min(mint, (x-rs[sli][dsi-1])*10)
            
            dbi=bisect_left(lb[bsi],x)
            if len(lb[bsi])>0 and dbi<len(lb[bsi]):
                mint=min(mint, (lb[bsi][dbi]-x)*10)
            
            dyi=bisect_right(uy[x],y)
            if len(uy[x])>0 and dyi>0:
                mint=min(mint, (y-uy[x][dyi-1])*5)
        else:
            ls[sli].append(x)
            lb[bsi].append(x)
            lx[y].append(x)
            
            lsi=bisect_right(us[sli],x)
            if len(us[sli])>0 and lsi>0:
                mint=min(mint, (x-us[sli][lsi-1])*10)
            
            lbi=bisect_right(db[bsi],x)
            if len(db[bsi])>0 and lbi>0:
                mint=min(mint, (x-db[bsi][lbi-1])*10)
            
            lxi=bisect_right(rx[y],x)
            if len(rx[y])>0 and lxi>0:
                mint=min(mint, (x-rx[y][lxi-1])*5)
    if mint >= 2*10**8:
        print("SAFE")
    else:
        print(mint)
                
                
    
    
    
if __name__=="__main__":
    main()