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 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**7
    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**7:
        print("SAFE")
    else:
        print(mint)
                
                
    
    
    
if __name__=="__main__":
    main()

Submission status: Wrong Answer

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

n = int(input())
Rp = []
Lp = []
Up = []
Dp = []


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

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):
        xy,_,x,y = A[i]
        if i > 0 and xy == A[i-1][0]:
            if len(l) > 0:
                for nxy,_,nx,ny in l:
                    if ny >= y:
                        count = min(count, abs(x-nx)*10)
        else:
            l = []
            while b < lb and B[b][0] <= xy:
                if B[b][0] == xy:
                    if B[b][3] >= y:
                        count = min(count,abs(x-B[b][2])*10)
                    l.append(B[b])
                b += 1
    return count

def syaku2(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):
        xy,_,x,y = A[i]
        if i > 0 and xy == A[i-1][0]:
            if len(l) > 0:
                for nxy,_,nx,ny in l:
                    if y >= ny:
                        count = min(count, abs(x-nx)*10)
        else:
            l = []
            while b < lb and B[b][0] <= xy:
                if B[b][0] == xy:
                    if y >= B[b][3]:
                        count = min(count,abs(x-B[b][2])*10)
                    l.append(B[b])
                b += 1
    return count

def syaku3(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][2]:
            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][2] <= x:
                if B[b][2] == x:
                    if y >= B[b][3]:
                        count = min(count,abs(y-B[b][3])*5)
                    l.append(B[b])
                b += 1
    return count
ans = float("INF")
Rp.sort()
Up.sort()
Lp.sort()
Dp.sort()
ans = min(ans,syaku(Up,Rp))
ans = min(ans,syaku2(Dp,Lp))
Rp.sort(key = lambda x: x[1])
Up.sort(key = lambda x: x[1])
Lp.sort(key = lambda x: x[1])
Dp.sort(key = lambda x: x[1])
ans = min(ans,syaku(Up,Lp))
ans = min(ans,syaku2(Dp,Rp))
Rp.sort()
Up.sort()
Lp.sort()
Dp.sort()
ans = min(ans,syaku3(Dp,Up))
ans = min(ans,syaku3(Lp,Rp))

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

Submission status: Wrong Answer

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

n = int(input())
Rp = []
Lp = []
Up = []
Dp = []
U = []
D = []
R = []
L = []

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


U.sort()
D.sort()
R.sort()
L.sort()
ans = float("INF")

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):
        xy,_,x,y = A[i]
        if i > 0 and xy == A[i-1][0]:
            if len(l) > 0:
                for nxy,_,nx,ny in l:
                    if ny >= y:
                        count = min(count, abs(x-nx)*10)
        else:
            l = []
            while b < lb and B[b][0] <= xy:
                if B[b][0] == xy:
                    if B[b][3] >= y:
                        count = min(count,abs(x-B[b][2])*10)
                    l.append(B[b])
                b += 1
    return count

def syaku2(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):
        xy,_,x,y = A[i]
        if i > 0 and xy == A[i-1][0]:
            if len(l) > 0:
                for nxy,_,nx,ny in l:
                    if y >= ny:
                        count = min(count, abs(x-nx)*10)
        else:
            l = []
            while b < lb and B[b][0] <= xy:
                if B[b][0] == xy:
                    if y >= B[b][3]:
                        count = min(count,abs(x-B[b][2])*10)
                    l.append(B[b])
                b += 1
    return count

def syaku3(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

Rp.sort()
Up.sort()
Lp.sort()
Dp.sort()
ans = min(ans,syaku(Up,Rp))
ans = min(ans,syaku2(Dp,Lp))
Rp.sort(key = lambda x: x[1])
Up.sort(key = lambda x: x[1])
Lp.sort(key = lambda x: x[1])
Dp.sort(key = lambda x: x[1])
ans = min(ans,syaku(Up,Lp))
ans = min(ans,syaku2(Dp,Rp))

ans = min(ans,syaku3(D,U))
ans = min(ans,syaku3(L,R))

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
U = []
D = []
R = []
L = []
U_ = defaultdict(list)
D_ = defaultdict(list)
R_ = defaultdict(list)
L_ = defaultdict(list)
Ux = defaultdict(list)
Dx = defaultdict(list)
Ry = defaultdict(list)
Ly = defaultdict(list)
for _ in range(n):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    if u == 'U':
        Ux[x].append(y)
        U_[x+y].append((x, y))
        U.append((x, y))
    elif u == 'D':
        Dx[x].append(y)
        D_[x+y].append((x, y))
        D.append((x, y))
    elif u == 'R':
        Ry[y].append(x)
        R_[x+y].append((x, y))
        R.append((x, y))
    else:
        Ly[y].append(x)
        L_[x+y].append((x, y))
        L.append((x, y))

for i in Ux.values():
    i.sort()
for i in Ry.values():
    i.sort()

ans = inf
for i, j in Dx.items():
    for k in j:
        r = br(Ux[i], k)
        if r != 0:
            ans = min(ans, int((k - Ux[i][r-1]) / 2 * 10))
for i, j in Ly.items():
    for k in j:
        r = br(Ry[i], k)
        if r != 0:
            ans = min(ans, int((k - Ry[i][r-1]) / 2 * 10))
for (i, j) in R:
    for (k, l) in D_[i+j]:
        if l - j >= 0:
            ans = min(ans, (l - j) * 10)

for (i, j) in R:
    for (k, l) in U_[i+j]:
        if j - l >= 0:
            ans = min(ans, (j - l) * 10)

for (i, j) in L:
    for (k, l) in U_[i+j]:
        if i - k >= 0:
            ans = min(ans, (i - k) * 10)

for (i, j) in L:
    for (k, l) in D_[i+j]:
        if i - k >= 0:
            ans = min(ans, (i - k) * 10)

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

Submission status: Wrong Answer

Solution code:
N = int(input())

from collections import defaultdict 
ud = defaultdict(list)
rl = defaultdict(list)
ur = defaultdict(list)
ul = defaultdict(list)
rd = defaultdict(list)
ld = defaultdict(list)

for i in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    if u == 'U':
        ud[x].append((y, u))
        ur[x+y].append((y, u))
        ul[x-y].append((y, u))
    elif u == 'D':
        ud[x].append((y, u))
        rd[x-y].append((y, u))
        ld[x+y].append((y, u))
    elif u == 'L':
        rl[y].append((x, u))
        ul[x-y].append((y, u))
        ld[x+y].append((y, u))
    elif u == 'R':
        rl[y].append((x, u))
        ur[x+y].append((y, u))
        rd[x-y].append((y, u))

ans = float('inf')

for _, arr in ud.items():
    arr.sort()
    pre = (None, None)
    for y, u in arr:
        if pre[1] == 'U' and u == 'D':
            ans = min(ans, (abs(y-pre[0])//2) * 10)
        pre = (y, u)
for _, arr in rl.items():
    arr.sort()
    pre = (None, None)
    for x, u in arr:
        if pre[1] == 'R' and u == 'L':
            ans = min(ans, (abs(x-pre[0])//2) * 10)
        pre = (x, u)
for _, arr in ur.items():
    arr.sort()
    pre = (None, None)
    for y, u in arr:
        if pre[1] == 'U' and u == 'R':
            ans = min(ans, abs(y-pre[0]) * 10)
        pre = (y, u)
for _, arr in ul.items():
    arr.sort()
    pre = (None, None)
    for y, u in arr:
        if pre[1] == 'U' and u == 'L':
            ans = min(ans, abs(y-pre[0]) * 10)
        pre = (y, u)
for _, arr in rd.items():
    arr.sort()
    pre = (None, None)
    for y, u in arr:
        if pre[1] == 'R' and u == 'D':
            ans = min(ans, abs(y-pre[0]) * 10)
        pre = (y, u)
for _, arr in ld.items():
    arr.sort()
    pre = (None, None)
    for y, u in arr:
        if pre[1] == 'L' and u == 'D':
            ans = min(ans, abs(y-pre[0]) * 10)
        pre = (y, u)

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

Submission status: Wrong Answer

Solution code:
N = int(input())

from collections import defaultdict 
ud = defaultdict(list)
rl = defaultdict(list)
ur = defaultdict(list)
ul = defaultdict(list)
rd = defaultdict(list)
ld = defaultdict(list)

for i in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    if u == 'U':
        ud[x].append((y, u))
        ur[x+y].append((y, u))
        ul[x-y].append((y, u))
    elif u == 'D':
        ud[x].append((y, u))
        rd[x-y].append((y, u))
        ld[x+y].append((y, u))
    elif u == 'L':
        rl[y].append((x, u))
        ul[x-y].append((y, u))
        ld[x+y].append((y, u))
    elif u == 'R':
        rl[y].append((x, u))
        ur[x+y].append((y, u))
        rd[x-y].append((y, u))

ans = float('inf')

for _, arr in ud.items():
    arr.sort()
    pre = (None, None)
    for y, u in arr:
        if pre[1] == 'U' and u == 'D':
            ans = min(ans, (abs(y-pre[0])//2) * 10)
        else:
            pre = (y, u)
for _, arr in rl.items():
    arr.sort()
    pre = (None, None)
    for x, u in arr:
        if pre[1] == 'R' and u == 'L':
            ans = min(ans, (abs(x-pre[0])//2) * 10)
        else:
            pre = (x, u)
for _, arr in ur.items():
    arr.sort()
    pre = (None, None)
    for y, u in arr:
        if pre[1] == 'U' and u == 'R':
            ans = min(ans, abs(y-pre[0]) * 10)
        else:
            pre = (y, u)
for _, arr in ul.items():
    arr.sort()
    pre = (None, None)
    for y, u in arr:
        if pre[1] == 'U' and u == 'L':
            ans = min(ans, abs(y-pre[0]) * 10)
        else:
            pre = (y, u)
for _, arr in rd.items():
    arr.sort()
    pre = (None, None)
    for y, u in arr:
        if pre[1] == 'R' and u == 'D':
            ans = min(ans, abs(y-pre[0]) * 10)
        else:
            pre = (y, u)
for _, arr in ld.items():
    arr.sort()
    pre = (None, None)
    for y, u in arr:
        if pre[1] == 'L' and u == 'D':
            ans = min(ans, abs(y-pre[0]) * 10)
        else:
            pre = (y, u)

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

Submission status: Wrong Answer

Solution code:
N = int(input())

from collections import defaultdict 
ud = defaultdict(list)
rl = defaultdict(list)
ur = defaultdict(list)
ul = defaultdict(list)
rd = defaultdict(list)
ld = defaultdict(list)

for i in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    if u == 'U':
        ud[x].append((y, u))
        ur[x+y].append((y, u))
        ul[x-y].append((y, u))
    elif u == 'D':
        ud[x].append((y, u))
        rd[x-y].append((y, u))
        ld[x+y].append((y, u))
    elif u == 'L':
        rl[y].append((x, u))
        ul[x-y].append((y, u))
        ld[x+y].append((y, u))
    elif u == 'R':
        rl[y].append((x, u))
        ur[x+y].append((y, u))
        rd[x-y].append((y, u))

ans = float('inf')

for _, arr in ud.items():
    arr.sort()
    pre = (None, None)
    for y, u in arr:
        if pre[1] == 'U' and u == 'D':
            ans = min(ans, ((y-pre[0])//2) * 10)
        else:
            pre = (y, u)
for _, arr in rl.items():
    arr.sort()
    pre = (None, None)
    for x, u in arr:
        if pre[1] == 'R' and u == 'L':
            ans = min(ans, ((x-pre[0])//2) * 10)
        else:
            pre = (x, u)
for _, arr in ur.items():
    arr.sort()
    pre = (None, None)
    for y, u in arr:
        if pre[1] == 'U' and u == 'R':
            ans = min(ans, (y-pre[0]) * 10)
        else:
            pre = (y, u)
for _, arr in ul.items():
    arr.sort()
    pre = (None, None)
    for y, u in arr:
        if pre[1] == 'U' and u == 'L':
            ans = min(ans, (y-pre[0]) * 10)
        else:
            pre = (y, u)
for _, arr in rd.items():
    arr.sort()
    pre = (None, None)
    for y, u in arr:
        if pre[1] == 'R' and u == 'D':
            ans = min(ans, (y-pre[0]) * 10)
        else:
            pre = (y, u)
for _, arr in ld.items():
    arr.sort()
    pre = (None, None)
    for y, u in arr:
        if pre[1] == 'L' and u == 'D':
            ans = min(ans, (y-pre[0]) * 10)
        else:
            pre = (y, u)

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
U, D, R, L = [], [], [], []
Ux = defaultdict(list)
Dx = defaultdict(list)
Ry = defaultdict(list)
Ly = defaultdict(list)
for _ in range(n):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    if u == 'U':
        Ux[x].append(y)
        U.append((x, y))
    elif u == 'D':
        Dx[x].append(y)
        D.append((x, y))
    elif u == 'R':
        Ry[y].append(x)
        R.append((x, y))
    else:
        Ly[y].append(x)
        L.append((x, y))

for i in Ux.values():
    i.sort()
for i in Ry.values():
    i.sort()

ans = inf
for i, j in Dx.items():
    for k in j:
        r = br(Ux[i], k)
        if r != 0:
            ans = min(ans, (k - Ux[i][r-1]) / 2 * 10)
for i, j in Ly.items():
    for k in j:
        r = br(Ry[i], k)
        if r != 0:
            ans = min(ans, (k - Ry[i][r-1]) / 2 * 10)
for (i, j) in R:
    for (k, l) in D:
        if l - j == k - i and l - j >= 0:
            ans = min(ans, (l - j) * 10)

for (i, j) in R:
    for (k, l) in U:
        if j - l == k - i and j - l >= 0:
            ans = min(ans, (j - l) * 10)

for (i, j) in L:
    for (k, l) in U:
        if i - k == j - l and i - k >= 0:
            ans = min(ans, (i - k) * 10)

for (i, j) in L:
    for (k, l) in D:
        if i - k == l - j and i - k >= 0:
            ans = min(ans, (i - k) * 10)

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

Submission status: Wrong Answer

Solution code:
N = int(input())

from collections import defaultdict 
ud = defaultdict(list)
rl = defaultdict(list)
ur = defaultdict(list)
ul = defaultdict(list)
rd = defaultdict(list)
ld = defaultdict(list)

for i in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    if u == 'U':
        ud[x].append((y, u))
        ur[x+y].append((y, u))
        ul[x+y].append((y, u))
    elif u == 'D':
        ud[x].append((y, u))
        rd[x+y].append((y, u))
        ld[x+y].append((y, u))
    elif u == 'L':
        rl[y].append((x, u))
        ul[x+y].append((y, u))
        ld[x+y].append((y, u))
    elif u == 'R':
        rl[y].append((x, u))
        ur[x+y].append((y, u))
        rd[x+y].append((y, u))

ans = float('inf')

for _, arr in ud.items():
    arr.sort()
    pre = (None, None)
    for y, u in arr:
        if pre[1] == 'U' and u == 'D':
            ans = min(ans, ((y-pre[0])//2) * 10)
        else:
            pre = (y, u)
for _, arr in rl.items():
    arr.sort()
    pre = (None, None)
    for x, u in arr:
        if pre[1] == 'R' and u == 'L':
            ans = min(ans, ((x-pre[0])//2) * 10)
        else:
            pre = (x, u)
for _, arr in ur.items():
    arr.sort()
    pre = (None, None)
    for y, u in arr:
        if pre[1] == 'U' and u == 'R':
            ans = min(ans, (y-pre[0]) * 10)
        else:
            pre = (y, u)
for _, arr in ul.items():
    arr.sort()
    pre = (None, None)
    for y, u in arr:
        if pre[1] == 'U' and u == 'L':
            ans = min(ans, (y-pre[0]) * 10)
        else:
            pre = (y, u)
for _, arr in rd.items():
    arr.sort()
    pre = (None, None)
    for y, u in arr:
        if pre[1] == 'R' and u == 'D':
            ans = min(ans, (y-pre[0]) * 10)
        else:
            pre = (y, u)
for _, arr in ld.items():
    arr.sort()
    pre = (None, None)
    for y, u in arr:
        if pre[1] == 'L' and u == 'D':
            ans = min(ans, (y-pre[0]) * 10)
        else:
            pre = (y, u)

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
U, D, R, L = [], [], [], []
Ux = defaultdict(list)
Dx = defaultdict(list)
Ry = defaultdict(list)
Ly = defaultdict(list)
for _ in range(n):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    if u == 'U':
        Ux[x].append(y)
        U.append((x, y))
    elif u == 'D':
        Dx[x].append(y)
        D.append((x, y))
    elif u == 'R':
        Ry[y].append(x)
        R.append((x, y))
    else:
        Ly[y].append(x)
        L.append((x, y))

for i in Ux.values():
    i.sort()
for i in Ry.values():
    i.sort()

ans = inf
for i, j in Dx.items():
    for k in j:
        r = br(Ux[i], k)
        if r != 0:
            ans = min(ans, (k - Ux[i][r-1]) // 2 * 10)
for i, j in Ly.items():
    for k in j:
        r = br(Ry[i], k)
        if r != 0:
            ans = min(ans, (k - Ry[i][r-1]) // 2 * 10)
for (i, j) in R:
    for (k, l) in D:
        if l - j == k - i and l - j >= 0:
            ans = min(ans, (l - j) * 10)

for (i, j) in R:
    for (k, l) in U:
        if j - l == k - i and j - l >= 0:
            ans = min(ans, (j - l) * 10)

for (i, j) in L:
    for (k, l) in U:
        if i - k == j - l and i - k >= 0:
            ans = min(ans, (i - k) * 10)

for (i, j) in L:
    for (k, l) in D:
        if i - k == l - j and i - k >= 0:
            ans = min(ans, (i - k) * 10)

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

Submission status: Wrong Answer

Solution code:
n = int(input())
from collections import defaultdict
import bisect
xd0=defaultdict(list)
xd90=defaultdict(list)
xd180=defaultdict(list)
xd270=defaultdict(list)
xyd0=defaultdict(list)
xyd90=defaultdict(list)
xyd180=defaultdict(list)
xyd270=defaultdict(list)

def rotate(x,y,u):
    if u=='U': u0='R'
    elif u=='R': u0='D'
    elif u=='D': u0='L'
    else: u0='U'
    x0=y
    y0=200000-x
    return x0,y0,u0

for _ in range(n):
    x,y,u=input().split()
    x,y=int(x),int(y)
    if u=='U' or u=='D':
        bisect.insort(xd0[x],(y,u))
    x90,y90,u90=rotate(x,y,u)
    if u90=='U' or u90=='D':
        bisect.insort(xd90[x90],(y90,u90))
    x180,y180,u180=rotate(x90,y90,u90)
    if u180=='U' or u180=='D':
        bisect.insort(xd180[x180],(y180,u180))
    x270,y270,u270=rotate(x180,y180,u180)
    if u270=='U' or u270=='D':
        bisect.insort(xd270[x270],(y270,u270))
    if u=='U' or u=='R':
        bisect.insort(xyd0[x+y],(y,u))
    if u90=='U' or u90=='R':
        bisect.insort(xyd90[x90+y90],(y90,u90))
    if u180=='U' or u180=='R':
        bisect.insort(xyd180[x180+y180],(y180,u180))
    if u270=='U' or u270=='R':
        bisect.insort(xyd270[x270+y270],(y270,u270))
print('SAFE')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

n = int(input())
line = defaultdict(list)  # R,L
row = defaultdict(list)  # U,D
A = defaultdict(list)  # U,R
B = defaultdict(list)  # L,D
C = defaultdict(list)  # U,L
D = defaultdict(list)  # R,D

for i in range(n):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    if u == "U":
        row[x].append((y, u == "U"))
        A[y + x].append((y - x, u == "U"))
        C[y - x].append((y + x, u == "U"))
    elif u == "D":
        row[x].append((y, u == "U"))
        B[y + x].append((y - x, u == "L"))
        D[y - x].append((y + x, u == "R"))
    elif u == "L":
        line[y].append((x, u == "R"))
        B[y + x].append((y - x, u == "L"))
        D[y - x].append((y + x, u == "R"))
    else:
        line[y].append((x, u == "R"))
        A[y + x].append((y - x, u == "U"))
        C[y - x].append((y + x, u == "U"))

ans = 10**18
for Z in (line, row, A, B, C, D):
    for z_list in Z.values():
        z_list.sort()
        for i in range(len(z_list) - 1):
            if z_list[i][1] and not z_list[i + 1][1]:
                ans = min(ans, abs(z_list[i][0] - z_list[i + 1][0]) * 5)

if ans == 10**18:
    print("SAFE")
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

n = int(input())
line = defaultdict(list)  # R,L
row = defaultdict(list)  # U,D
A = defaultdict(list)  # U,R
B = defaultdict(list)  # L,D
C = defaultdict(list)  # U,L
D = defaultdict(list)  # R,D

for i in range(n):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    if u == "U":
        row[x].append((y, u == "U"))
        A[y + x].append((y - x, u == "U"))
        C[y - x].append((y + x, u == "U"))
    elif u == "D":
        row[x].append((y, u == "U"))
        B[y + x].append((y - x, u == "L"))
        D[y - x].append((y + x, u == "R"))
    elif u == "L":
        line[y].append((x, u == "R"))
        B[y + x].append((y - x, u == "L"))
        D[y - x].append((y + x, u == "R"))
    else:
        line[y].append((x, u == "R"))
        A[y + x].append((y - x, u == "U"))
        C[y - x].append((y + x, u == "U"))

ans = 10**18
for Z in (line, row, A, B, C, D):
    for z_list in Z.values():
        z_list.sort()
        for (v1, flag1), (v2, flag2) in zip(z_list, z_list[1:]):
            if flag1 and not flag2:
                ans = min(ans, abs(v1 - v2) * 5)

if ans == 10**18:
    print("SAFE")
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
n = int(input())
x = [0] * n
y = [0] * n
dir = [0] * n
INF = 10**8


def g(v):
    v.sort(key=lambda x: x[0])
    ret = INF
    for i in range(1, len(v)):
        if v[i - 1][1] == 0 and v[1][1] != 0:
            ret = min(ret, v[i][0] - v[i - 1][0])
    return ret


def f(d):
    ret = INF
    for k, v in d.items():
        ret = min(ret, g(v))
    return ret


for i in range(n):
    # x[i], y[i], c = map(int, input().split())
    sx, sy, c = input().split()
    x[i] = int(sx)
    y[i] = int(sy)
    if c == 'U':
        dir[i] = 0
    if c == 'R':
        dir[i] = 1
    if c == 'D':
        dir[i] = 2
    if c == 'L':
        dir[i] = 3

ans = INF
for ri in range(4):
    # UD
    d = defaultdict(list)
    for i in range(n):
        if dir[i] != 0 and dir[i] != 2:
            continue
        d[x[i]].append((y[i], dir[i]))
    ans = min(ans, f(d) * 5)
    # UR
    d2 = defaultdict(list)
    for i in range(n):
        if dir[i] != 0 and dir[i] != 1:
            continue
        d2[x[i] + y[i]].append((y[i], dir[i]))
    ans = min(ans, f(d2) * 10)

    # rotate
    for i in range(n):
        px = x[i]
        py = y[i]
        x[i] = py
        y[i] = -px
        dir[i] = (dir[i] + 1) % 4
if ans >= INF:
    print('SAFE')
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
n = int(input())
x = [0] * n
y = [0] * n
dir = [0] * n
INF = 10**8


def g(v):
    v.sort(key=lambda x: x[0])
    ret = INF
    for i in range(1, len(v)):
        if v[i - 1][1] == 0 and v[1][1] != 0:
            ret = min(ret, v[i][0] - v[i - 1][0])
    return ret


def f(d):
    ret = INF
    for k, v in d.items():
        ret = min(ret, g(v))
    return ret


for i in range(n):
    # x[i], y[i], c = map(int, input().split())
    sx, sy, c = input().split()
    x[i] = int(sx)
    y[i] = int(sy)
    if c == 'U':
        dir[i] = 0
    if c == 'R':
        dir[i] = 1
    if c == 'D':
        dir[i] = 2
    if c == 'L':
        dir[i] = 3

ans = INF
for ri in range(4):
    # UD
    d = defaultdict(list)
    for i in range(n):
        if dir[i] != 0 and dir[i] != 2:
            continue
        d[x[i]].append((y[i], dir[i]))
    ans = min(ans, f(d) * 5)
    # UR
    d = defaultdict(list)
    for i in range(n):
        if dir[i] != 0 and dir[i] != 1:
            continue
        d[x[i] + y[i]].append((y[i], dir[i]))
    ans = min(ans, f(d) * 10)

    # rotate
    for i in range(n):
        px = x[i]
        py = y[i]
        x[i] = py
        y[i] = -px
        dir[i] = (dir[i] + 1) % 4
if ans >= INF:
    print('SAFE')
else:
    print(ans)