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:
import bisect
def main():
    N = int(input())
    dx = {}
    dy = {}
    dxpy = {}
    dxmy = {}
    for _ in range(N):
        x, y, u = input().split()
        x = int(x)
        y = int(y)
        if u in dx:
            if x in dx[u]:
                dx[u][x].append(y)
            else:
                dx[u][x] = [y]
            if y in dy[u]:
                dy[u][y].append(x)
            else:
                dy[u][y] = [x]
            if x + y in dxpy[u]:
                dxpy[u][x+y].append(y)
            else:
                dxpy[u][x+y] = [y]
            if x - y in dxmy[u]:
                dxmy[u][x-y].append(y)
            else:
                dxmy[u][x-y] = [y]
        else:
            dx[u] = {}
            dx[u][x] = [y]
            dy[u] = {}
            dy[u][y] = [x]
            dxpy[u] = {}
            dxpy[u][x+y] = [y]
            dxmy[u] = {}
            dxmy[u][x+y] = [y]
    for d in [dx, dy, dxpy, dxmy]:
        for j in d:
            for k in d[j]:
                d[j][k].sort()
    M = 3 * (10**5)
    ###SHOUMEN SHOTOTSU
    # U and D
    if "U" in dx and "D" in dx:
        for x in dx["U"]:
            if x not in dx["D"]:
                continue
            for y in dx["U"][x]:
                yi = bisect.bisect_left(dx["D"][x], y)
                if yi < len(dx["D"][x]):
                    M = min((dx["D"][x][yi] - y)*5, M)
    # L and R
    if "L" in dy and "R" in dy:
        for y in dy["R"]:
            if y not in dy["L"]:
                continue
            for x in dy["R"][y]:
                xi = bisect.bisect_left(dy["L"][y], x)
                if xi < len(dy["L"][y]):
                    M = min((dy["L"][y][xi] - x)*5, M)
    ###KOUSA
    # D and R
    if "D" in dy and "R" in dy:
        for s in dxpy["R"]:
            if s not in dxpy["D"]:
                continue
            for p in dxpy["R"][s]:
                yi = bisect.bisect_left(dxpy["D"][s], p)
                if yi < len(dxpy["D"][s]):
                    M = min((dxpy["D"][s][yi] - p)*10, M)
    # L and U
    if "L" in dy and "U" in dy:
        for s in dxpy["U"]:
            if s not in dxpy["L"]:
                continue
            for p in dxpy["U"][s]:
                yi = bisect.bisect_left(dxpy["L"][s], p)
                if yi < len(dxpy["L"][s]):
                    M = min((dxpy["L"][s][yi] - p)*10, M)
    # U and R
    if "U" in dy and "R" in dy:
        for s in dxmy["U"]:
            if s not in dxmy["R"]:
                continue
            for p in dxmy["U"][s]:
                yi = bisect.bisect_left(dxmy["R"][s], p)
                if yi < len(dxmy["R"][s]):
                    M = min((dxmy["R"][s][yi] - p)*10, M)
    # L and D
    if "D" in dy and "L" in dy:
        for s in dxmy["L"]:
            if s not in dxmy["D"]:
                continue
            for p in dxmy["L"][s]:
                yi = bisect.bisect_left(dxmy["D"][s], p)
                if yi < len(dxmy["D"][s]):
                    M = min((dxmy["D"][s][yi] - p)*10, M)
    return M
x = main()
if x < 3 * (10**5):
    print(x)
else:
    print('SAFE')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from bisect import bisect_right

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

dis = 10 ** 19 

# 正面衝突用
box = {"U":defaultdict(list),"D":defaultdict(list),"L":defaultdict(list),"R":defaultdict(list)}

for x, y, u in XYU:
  x = int(x)
  y = int(y)
  if u in ["U","D"]:
    box[u][x].append(y) 
  else:
    box[u][y].append(x)
    
for k1 in box.keys():
  for k2 in box[k1].keys():
    box[k1][k2] = sorted(box[k1][k2])
    
for k, v in box["U"].items():
  if k in box["D"]:
    for pos in v:
      if pos < box["D"][k][-1]:
        a = bisect_right(box["D"][k], pos)
        dis = min(dis, box["D"][k][a] - pos) * 10 // 2

for k, v in box["R"].items():
  if k in box["L"]:
    for pos in v:
      if pos < box["L"][k][-1]:  
        a = bisect_right(box["L"][k], pos)
        dis = min(dis, box["L"][k][a] - pos) * 10 // 2

# 右肩下がりの同一直線上から発進した機体の衝突用
cox = {"U":defaultdict(list),"D":defaultdict(list),"L":defaultdict(list),"R":defaultdict(list)}

for x, y, u in XYU:
  x = int(x)
  y = int(y)
  cox[u][x+y].append(x) 
    
for k1 in cox.keys():
  for k2 in cox[k1].keys():
    cox[k1][k2] = sorted(cox[k1][k2])
    
for k, v in cox["R"].items():
  if k in cox["U"]:
    for pos in v:
      if pos < cox["U"][k][-1]:
        a = bisect_right(cox["U"][k], pos)
        dis = min(dis, cox["U"][k][a] - pos) * 10

for k, v in cox["D"].items():
  if k in cox["L"]:
    for pos in v:
      if pos < cox["L"][k][-1]:  
        a = bisect_right(cox["L"][k], pos)
        dis = min(dis, cox["L"][k][a] - pos) * 10

# 右肩上がりの同一直線上から発進した機体の衝突用
dox = {"U":defaultdict(list),"D":defaultdict(list),"L":defaultdict(list),"R":defaultdict(list)}

for x, y, u in XYU:
  x = int(x)
  y = int(y)
  dox[u][x-y].append(x) 
    
for k1 in dox.keys():
  for k2 in dox[k1].keys():
    dox[k1][k2] = sorted(dox[k1][k2])
    
for k, v in dox["U"].items():
  if k in dox["L"]:
    for pos in v:
      if pos < dox["L"][k][-1]:
        a = bisect_right(dox["L"][k], pos)
        dis = min(dis, dox["L"][k][a] - pos) * 10

for k, v in dox["R"].items():
  if k in dox["D"]:
    for pos in v:
      if pos < dox["D"][k][-1]:  
        a = bisect_right(dox["D"][k], pos)
        dis = min(dis, dox["D"][k][a] - pos) * 10

if dis == 10 ** 19:
  print("SAFE")
else:
  print(dis)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from bisect import bisect_right

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

dis = 10 ** 19 

# 正面衝突用
box = {"U":defaultdict(list),"D":defaultdict(list),"L":defaultdict(list),"R":defaultdict(list)}

for x, y, u in XYU:
  x = int(x)
  y = int(y)
  if u in ["U","D"]:
    box[u][x].append(y) 
  else:
    box[u][y].append(x)
    
for k1 in box.keys():
  for k2 in box[k1].keys():
    box[k1][k2] = sorted(box[k1][k2])
    
for k, v in box["U"].items():
  if k in box["D"]:
    for pos in v:
      if pos < box["D"][k][0]:
        a = bisect_right(box["D"][k], pos)
        dis = min(dis, box["D"][k][a] - pos) * 10 // 2

for k, v in box["R"].items():
  if k in box["L"]:
    for pos in v:
      if pos < box["L"][k][0]:  
        a = bisect_right(box["L"][k], pos)
        dis = min(dis, box["L"][k][a] - pos) * 10 // 2

# 右肩下がりの同一直線上から発進した機体の衝突用
cox = {"U":defaultdict(list),"D":defaultdict(list),"L":defaultdict(list),"R":defaultdict(list)}

for x, y, u in XYU:
  x = int(x)
  y = int(y)
  cox[u][x+y].append(x) 
    
for k1 in cox.keys():
  for k2 in cox[k1].keys():
    cox[k1][k2] = sorted(cox[k1][k2])
    
for k, v in cox["R"].items():
  if k in cox["U"]:
    for pos in v:
      if pos < cox["U"][k][0]:
        a = bisect_right(cox["U"][k], pos)
        dis = min(dis, cox["U"][k][a] - pos) * 10

for k, v in cox["D"].items():
  if k in cox["L"]:
    for pos in v:
      if pos < cox["L"][k][0]:  
        a = bisect_right(cox["L"][k], pos)
        dis = min(dis, cox["L"][k][a] - pos) * 10

# 右肩上がりの同一直線上から発進した機体の衝突用
dox = {"U":defaultdict(list),"D":defaultdict(list),"L":defaultdict(list),"R":defaultdict(list)}

for x, y, u in XYU:
  x = int(x)
  y = int(y)
  dox[u][x-y].append(x) 
    
for k1 in dox.keys():
  for k2 in dox[k1].keys():
    dox[k1][k2] = sorted(dox[k1][k2])
    
for k, v in dox["U"].items():
  if k in dox["L"]:
    for pos in v:
      if pos < dox["L"][k][0]:
        a = bisect_right(dox["L"][k], pos)
        dis = min(dis, dox["L"][k][a] - pos) * 10

for k, v in dox["R"].items():
  if k in dox["D"]:
    for pos in v:
      if pos < dox["D"][k][0]:  
        a = bisect_right(dox["D"][k], pos)
        dis = min(dis, dox["D"][k][a] - pos) * 10

if dis == 10 ** 19:
  print("SAFE")
else:
  print(dis)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import defaultdict
n = int(input())
UR = defaultdict(list)
UD = defaultdict(list)
UL = defaultdict(list)
DR = defaultdict(list)
DL = defaultdict(list)
RL = defaultdict(list)

for i in range(n):
    x,y,u = input().split()
    x,y = int(x), int(y)
    if u == "U":    
        UD[x].append((y,u=="U"))
        UR[x+y].append((y-x,u=="U"))
        UL[x-y].append((x+y,u=="U"))
    elif u == "D":    
        UD[x].append((y,u=="U"))
        DR[x-y].append((x+y,u=="D"))
        DL[x+y].append((y-x,u=="D"))
    elif u == "R":   
        RL[y].append((x,u=="R"))
        DR[x-y].append((x+y,u=="D"))
        UR[x+y].append((y-x,u=="U"))
        
    else:
        RL[y].append((x,u=="R"))
        UL[x-y].append((x+y,u=="U"))
        DL[x+y].append((y-x,u=="D"))

ans = float("INF")

for x in [UD, UL, UR, DR, DL, RL]:
    for v in x.values():
        v.sort()

        for i in range(len(v)-1):
            if v[i][1] != v[i+1][1]:
                ans = min(ans,v[i+1][0]-v[i][0])


if ans == float("INF"):
    print("SAFE")
else:
    print(ans*5)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import defaultdict
n = int(input())
UR = defaultdict(list)
UD = defaultdict(list)
UL = defaultdict(list)
DR = defaultdict(list)
DL = defaultdict(list)
RL = defaultdict(list)

for i in range(n):
    x,y,u = input().split()
    x,y = int(x), int(y)
    if u == "U":    
        UD[x].append((y,u=="U"))
        UR[x+y].append((x-y,u=="U"))
        UL[x-y].append((x+y,u=="U"))
    elif u == "D":    
        UD[x].append((y,u=="U"))
        DR[x-y].append((x+y,u=="D"))
        DL[x+y].append((x-y,u=="D"))
    elif u == "R":   
        RL[y].append((x,u=="R"))
        DR[x-y].append((x+y,u=="D"))
        UR[x+y].append((x-y,u=="U"))
        
    else:
        RL[y].append((x,u=="R"))
        UL[x-y].append((x+y,u=="U"))
        DL[x+y].append((x-y,u=="D"))

ans = float("INF")

for x in [UD, UL, UR, DR, DL, RL]:
    for v in x.values():
        v.sort()

        for i in range(len(v)-1):
            if v[i][1] != v[i+1][1]:
                ans = min(ans,v[i+1][0]-v[i][0])


if ans == float("INF"):
    print("SAFE")
else:
    print(ans*5)

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

dgrup = [[] for i in range(ldg+1)]
dgldw = [[] 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]))
        heapq.heappush(dgrup[int(xyu[i][0])+int(xyu[i][1])],int(xyu[i][1]))
    if xyu[i][2] == 'D':
        heapq.heappush(dw[int(xyu[i][0])],int(xyu[i][1]))
        heapq.heappush(dgdw[int(xyu[i][0])-int(xyu[i][1])+lxy],int(xyu[i][1]))
        heapq.heappush(dgldw[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[int(xyu[i][0])+int(xyu[i][1])],int(xyu[i][1]))
        heapq.heappush(dgdrh[int(xyu[i][0])-int(xyu[i][1])+lxy],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])],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(dglf[i]) > 0 and tdgup > tdglf:
            tdglf = heapq.heappop(dglf[i])
        if tdgup < tdglf:
            ans = min((tdglf-tdgup)*10, ans)


    tdgrh = -1
    tdglf = -1        
    while len(dgrup[i]) > 0:
        tdgrup = heapq.heappop((dgrup[i]))
        while len(dgrh[i]) > 0 and tdgrup > tdgrh:
            tdgrh = heapq.heappop(dgrh[i])
        if tdgrup < tdgrh:
            ans = min((tdgrh-tdgrup)*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)
    
    tdgrh = 10**100
    tdglf = 10**100
    
    while len(dgldw[i]) > 0:
        tdgup = heapq.heappop((dgldw[i]))
        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:
I=N=9**9
P,M,L,R,U,*D='x+y,','x-y,',[],[],[]
for t in[*open(0)][1:]:*z,u=t.split();exec(u+'+=[*map(int,z)],')
for*A,S in(D,U,'x,y,'),(L,R,'y,x,'),(U,R,P+M),(L,D,P+M),(L,U,M+P):
 n=p=-I
 for x,y,r in eval('sorted((%sQ)for Q,P in enumerate(A)for x,y in P)'%S):
  if r:n,p=x,y
  elif x==n:N=min(N,y-p)
print(N%I*5or'SAFE')

Submission status: Wrong Answer

Solution code:
I=N=9**9
P,M,L,R,U,*D='x+y,','x-y,',[],[],[]
for t in[*open(0)][1:]:*z,u=t.split();exec(u+'+=[*map(int,z)],')
for*A,S in(D,U,'x,y,'),(L,R,'y,x,'),(U,R,P+M),(L,D,P+M),(D,R,M+P):
 n=p=-I
 for x,y,r in eval('sorted((%sQ)for Q,P in enumerate(A)for x,y in P)'%S):
  if r:n,p=x,y
  elif x==n:N=min(N,y-p)
print(N%I*5or'SAFE')

Submission status: Wrong Answer

Solution code:
I=N=9**9
P,M,L,R,U,*D='x+y,','x-y,',[],[],[]
for t in[*open(0)][1:]:*z,u=t.split();exec(u+'+=[*map(int,z)],')
for*A,S in(D,U,'x,y,'),(L,R,'y,x,'),(U,R,P+M),(L,U,M+P),(D,R,M+P):
 n=p=-I
 for x,y,r in eval('sorted((%sQ)for Q,P in enumerate(A)for x,y in P)'%S):
  if r:n,p=x,y
  elif x==n:N=min(N,y-p)
print(N%I*5or'SAFE')

Submission status: Wrong Answer

Solution code:
I=N=9**9
P,M,L,R,U,*D='x+y,','x-y,',[],[],[]
for t in[*open(0)][1:]:*z,u=t.split();exec(u+'+=[*map(int,z)],')
for*A,S in(D,U,'x,y,'),(L,R,'y,x,'),(L,D,P+M),(L,U,M+P),(D,R,M+P):
 n=p=-I
 for x,y,r in eval('sorted((%sQ)for Q,P in enumerate(A)for x,y in P)'%S):
  if r:n,p=x,y
  elif x==n:N=min(N,y-p)
print(N%I*5or'SAFE')

Submission status: Wrong Answer

Solution code:
I=N=9**9
P,M,L,R,U,*D='x+y,','x-y,',[],[],[]
for t in[*open(0)][1:]:*z,u=t.split();exec(u+'+=[*map(int,z)],')
for*A,S in(D,U,'x,y,'),(U,R,P+M),(L,D,P+M),(L,U,M+P),(D,R,M+P):
 n=p=-I
 for x,y,r in eval('sorted((%sQ)for Q,P in enumerate(A)for x,y in P)'%S):
  if r:n,p=x,y
  elif x==n:N=min(N,y-p)
print(N%I*5or'SAFE')

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
Rp = {}
Lp = {}
Rm = {}
Lm = {}
U = []
R = []
L = []
D = []

for i in range(n):
    x,y,u = input().split()
    x,y = int(x), int(y)
    if u == "U":    
        U.append((x,y))
    elif u == "D":    
        D.append((x,y))
    elif u == "R":   
        if x+y in Rp:
            Rp[x+y].append((x,y))
        else:
            Rp[x+y] = [(x,y)]
        if x-y in Rm:
            Rm[x-y].append((x,y))
        else:
            Rm[x-y] = [(x,y)]
        R.append((y,x))
        
    else:
        if x+y in Lp:
            Lp[x+y].append((x,y))
        else:
            Lp[x+y] = [(x,y)]
        if x-y in Lm:
            Lm[x-y].append((x,y))
        else:
            Lm[x-y] = [(x,y)]
        L.append((y,x))

ans = float("INF")

for x,y in U:
    if x+y in Rp:
        for nx,ny in Rp[x+y]:
            if ny >= y:
                ans = min(ans,abs(ny-y)*10)
    if x-y in Lm:
        for nx,ny in Lm[x-y]:
            if ny >= y:
                ans = min(ans,abs(ny-y)*10)
for x,y in D:
    if x+y in Lp:
        for nx,ny in Lp[x+y]:
            if y >= ny:
                ans = min(ans,abs(ny-y)*10)
    if x-y in Rm:
        for nx,ny in Rm[x-y]:
            if y >= ny:
                ans = min(ans,abs(ny-y)*10)

def syaku(A,B):
    if A == [] or B == []:
        return float("INF")
    la = len(A)
    lb = len(B)
    count = float("INF")
    b = 0
    for i in range(la):
        x,y = A[i]
        if i > 0 and x == A[i-1][0]:
            if len(l) > 0:
                for nx,ny in l:
                    if y >= ny:
                        count = min(count, abs(y-ny)*5)
        else:
            l = []
            while b < lb and B[b][0] <= x:
                if B[b][0] == x:
                    if y >= B[b][1]:
                        count = min(count,abs(y-B[b][1])*5)
                    l.append(B[b])
                b += 1
    return count



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

Submission status: Wrong Answer

Solution code:
I=N=9**9
P,M,L,R,U,*D='x+y,','x-y,',[],[],[]
for t in[*open(0)][1:]:*z,u=t.split();exec(u+'+=[*map(int,z)],')
for*A,S in(L,R,'y,x,'),(U,R,P+M),(L,D,P+M),(L,U,M+P),(D,R,M+P):
 n=p=-I
 for x,y,r in eval('sorted((%sQ)for Q,P in enumerate(A)for x,y in P)'%S):
  if r:n,p=x,y
  elif x==n:N=min(N,y-p)
print(N%I*5or'SAFE')

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

dgrup = [[] for i in range(ldg+1)]
dgldw = [[] 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]))
        heapq.heappush(dgrup[ldg-int(xyu[i][0])-int(xyu[i][1])],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]))
        heapq.heappush(dgldw[int(xyu[i][0])-int(xyu[i][1])+lxy],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(dglf[i]) > 0 and tdgup > tdglf:
            tdglf = heapq.heappop(dglf[i])
        if tdgup < tdglf:
            ans = min((tdglf-tdgup)*10, ans)


    tdgrh = -1
    tdglf = -1        
    while len(dgrup[i]) > 0:
        tdgrup = heapq.heappop((dgrup[i]))
        while len(dgrh[i]) > 0 and tdgrup > tdgrh:
            tdgrh = heapq.heappop(dgrh[i])
        if tdgrup < tdgrh:
            ans = min((tdgrh-tdgrup)*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)
    
    tdgrh = 10**100
    tdglf = 10**100
    
    while len(dgldw[i]) > 0:
        tdgup = heapq.heappop((dgldw[i]))
        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 bisect import bisect_left

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

dis = 10 ** 19 

# 正面衝突用
box = {"U":defaultdict(list),"D":defaultdict(list),"L":defaultdict(list),"R":defaultdict(list)}

for x, y, u in XYU:
  x = int(x)
  y = int(y)
  if u in ["U","D"]:
    box[u][x].append(y) 
  else:
    box[u][y].append(x)
    
for k1 in box.keys():
  for k2 in box[k1].keys():
    box[k1][k2] = sorted(box[k1][k2])
    
for k, v in box["U"].items():
  if k in box["D"]:
    for pos in v:
      if pos < box["D"][k][0]:
        a = bisect_left(box["D"][k], pos)
        dis = min(dis, box["D"][k][a] - pos) * 10 // 2

for k, v in box["R"].items():
  if k in box["L"]:
    for pos in v:
      if pos < box["L"][k][0]:  
        a = bisect_left(box["L"][k], pos)
        dis = min(dis, box["L"][k][a] - pos) * 10 // 2

# 右肩下がりの同一直線上から発進した機体の衝突用
cox = {"U":defaultdict(list),"D":defaultdict(list),"L":defaultdict(list),"R":defaultdict(list)}

for x, y, u in XYU:
  x = int(x)
  y = int(y)
  cox[u][x+y].append(x) 
    
for k1 in cox.keys():
  for k2 in cox[k1].keys():
    cox[k1][k2] = sorted(cox[k1][k2])
    
for k, v in cox["R"].items():
  if k in cox["U"]:
    for pos in v:
      if pos < cox["U"][k][0]:
        a = bisect_left(cox["U"][k], pos)
        dis = min(dis, cox["U"][k][a] - pos) * 10

for k, v in cox["D"].items():
  if k in cox["L"]:
    for pos in v:
      if pos < cox["L"][k][0]:  
        a = bisect_left(cox["L"][k], pos)
        dis = min(dis, cox["L"][k][a] - pos) * 10

# 右肩上がりの同一直線上から発進した機体の衝突用
dox = {"U":defaultdict(list),"D":defaultdict(list),"L":defaultdict(list),"R":defaultdict(list)}

for x, y, u in XYU:
  x = int(x)
  y = int(y)
  dox[u][x-y].append(x) 
    
for k1 in dox.keys():
  for k2 in dox[k1].keys():
    dox[k1][k2] = sorted(dox[k1][k2])
    
for k, v in dox["U"].items():
  if k in dox["L"]:
    for pos in v:
      if pos < dox["L"][k][0]:
        a = bisect_left(dox["L"][k], pos)
        dis = min(dis, dox["L"][k][a] - pos) * 10

for k, v in dox["R"].items():
  if k in dox["D"]:
    for pos in v:
      if pos < dox["D"][k][0]:  
        a = bisect_left(dox["D"][k], pos)
        dis = min(dis, dox["D"][k][a] - pos) * 10

if dis == 10 ** 19:
  print("SAFE")
else:
  print(dis)

Submission status: Wrong Answer

Solution code:
d=[[],[],[],[]]
for t in[*open(0)][1:]:*z,u=t.split();d[ord(u)%5]+=[*map(int,z)],
I=N=9e9
P,M='x+y,','x-y,'
for*A,S in(3,0,'x,y,'),(1,2,'y,x,'),(0,2,P+M),(1,3,P+M),(1,0,M+P),(3,2,M+P):
 n=p=-I
 for x,y,r in eval('sorted((%sQ)for Q,P in enumerate(A)for x,y in d[P])'%S):
  if r:n,p=x,y
  elif x==n:N=min(N,y-p)
print(N%I*5or'SAFE')

Submission status: Wrong Answer

Solution code:
import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
from collections import defaultdict
from bisect import bisect_left
def resolve():
    n = int(input())
    direction = {'U' : 0, 'R' : 1, 'D' : 2, 'L' : 3}
    X = defaultdict(lambda:[[] for _ in range(4)])
    Y = defaultdict(lambda:[[] for _ in range(4)])
    Y_minus_X = defaultdict(lambda:[[] for _ in range(4)])
    X_plus_Y = defaultdict(lambda:[[] for _ in range(4)])
    for _ in range(n):
        x, y, u = input().split()
        x = int(x)
        y = int(y)
        d = direction[u]
        X[x][d].append(y)
        Y[y][d].append(x)
        Y_minus_X[y - x][d].append(x)
        X_plus_Y[x + y][d].append(x)

    ans = INF
    for x in X:
        down = sorted(X[x][2])
        up = sorted(X[x][0])
        for u in up:
            idx = bisect_left(down, u)
            if idx == len(down):
                continue
            ans = min(ans, 5 * (down[idx] - u))

    for y in Y:
        right = sorted(Y[y][1])
        left = sorted(Y[y][3])
        for r in right:
            idx = bisect_left(left, r)
            if idx == len(left):
                continue
            ans = min(ans, 5 * (left[idx] - r))

    for c in Y_minus_X:
        right = sorted(Y_minus_X[c][1])
        down = sorted(Y_minus_X[c][2])
        for r in right:
            idx = bisect_left(down, r)
            if idx == len(down):
                continue
            ans = min(ans, 10 * (down[idx] - r))
        up = sorted(Y_minus_X[c][0])
        left = sorted(Y_minus_X[c][3])
        for u in up:
            idx = bisect_left(left, u)
            if idx == len(left):
                continue
            ans = min(ans, 10 * (left[idx] - u))

    for c in X_plus_Y:
        right = X_plus_Y[c][1]
        up = X_plus_Y[c][0]
        for r in right:
            idx = bisect_left(up, r)
            if idx == len(up):
                continue
            ans = min(ans, 10 * (up[idx] - r))
        down = X_plus_Y[c][2]
        left = X_plus_Y[c][3]
        for d in down:
            idx = bisect_left(left, d)
            if idx == len(left):
                continue
            ans = min(ans, 10 * (left[idx] - d))

    if ans != INF:
        print(ans)
    else:
        print("SAFE")
resolve()

Submission status: Wrong Answer

Solution code:
import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
from collections import defaultdict
from bisect import bisect
def resolve():
    n = int(input())
    direction = {'U' : 0, 'R' : 1, 'D' : 2, 'L' : 3}
    X = defaultdict(lambda:[[] for _ in range(4)])
    Y = defaultdict(lambda:[[] for _ in range(4)])
    Y_minus_X = defaultdict(lambda:[[] for _ in range(4)])
    X_plus_Y = defaultdict(lambda:[[] for _ in range(4)])
    for _ in range(n):
        x, y, u = input().split()
        x = int(x)
        y = int(y)
        d = direction[u]
        X[x][d].append(y)
        Y[y][d].append(x)
        Y_minus_X[y - x][d].append(x)
        X_plus_Y[x + y][d].append(x)

    ans = INF
    for x in X:
        down = sorted(X[x][2])
        up = sorted(X[x][0])
        for u in up:
            idx = bisect(down, u)
            if idx == len(down):
                continue
            ans = min(ans, 5 * (down[idx] - u))

    for y in Y:
        right = sorted(Y[y][1])
        left = sorted(Y[y][3])
        for r in right:
            idx = bisect(left, r)
            if idx == len(left):
                continue
            ans = min(ans, 5 * (left[idx] - r))

    for c in Y_minus_X:
        right = sorted(Y_minus_X[c][1])
        down = sorted(Y_minus_X[c][2])
        for r in right:
            idx = bisect(down, r)
            if idx == len(down):
                continue
            ans = min(ans, 10 * (down[idx] - r))
        up = sorted(Y_minus_X[c][0])
        left = sorted(Y_minus_X[c][3])
        for u in up:
            idx = bisect(left, u)
            if idx == len(left):
                continue
            ans = min(ans, 10 * (left[idx] - u))

    for c in X_plus_Y:
        right = X_plus_Y[c][1]
        up = X_plus_Y[c][0]
        for r in right:
            idx = bisect(up, r)
            if idx == len(up):
                continue
            ans = min(ans, 10 * (up[idx] - r))
        down = X_plus_Y[c][2]
        left = X_plus_Y[c][3]
        for d in down:
            idx = bisect(left, d)
            if idx == len(left):
                continue
            ans = min(ans, 10 * (left[idx] - d))

    if ans != INF:
        print(ans)
    else:
        print("SAFE")
resolve()

Submission status: Wrong Answer

Solution code:
U,R,D,L="URDL"
d={i:[]for i in"URDL"}
_,*a=map(str.split,open(0))
for*z,u in a:d[u]+=[*map(int,z)],
N,P,M=9e9,"x+y,","x-y,"
for*F,S in(D,U,"x,y,"),(L,R,"y,x,"),(U,R,P+M),(L,D,P+M),(L,U,M+P),(D,R,M+P):
 n=p=-9e9
 for x,y,r in sorted(eval(f"[({S}Q)for Q,P in enumerate(F)for x,y in d[P]]")):
  if r:n,p=x,y
  elif x==n:N=min(N,(y-p)*5)
print(["SAFE",N*5][N<9e9])