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:
#root.search(x): ◯.data == x のものが存在するある場合それを出力、ない場合Noneを出力
#root.insert(x): ◯.data == xが存在する場合Falseを出力、ない場合◯.data == x なる頂点を作る
#y.to_s(): 頂点yのdataを出力
#y.left_s(): 頂点yのleftを出力(ない場合はNone)
#root.search_lower(x,None): ○.dataがxより小さいものの中で最も大きいものを出力、ない場合Noneを出力
#root.search_higher(x,None): xより大きいものの中で最も小さいものを出力、ない場合Noneを出力

################################################################################
class Avltree:
    def __init__(self,key=None):
        self.key = key
        self.left = None
        self.right = None 
        self.balance = "Even"
    def search(self,key):
        if self.key == None:
            return None
        if self.key > key:
            if self.left == None:
                return None
            else:
                return self.left.search(key)
        if self.key < key:
            if self.right == None:
                return None
            else:
                return self.right.search(key)
        return self # self.key == keyの場合
        
    def search_lower(self,key,key_lower):
        if self.key == None:
            return key_lower
        if self.key > key:
            if self.left == None:
                return key_lower
            else:
                return self.left.search_lower(key,key_lower)
        if self.key < key:
            key_lower = self.key
            if self.right == None:
                return key_lower
            else:
                return self.right.search_lower(key,key_lower)
        #self.key == keyの場合
        if self.left == None:
            return key_lower
        else:
            if key_lower == None:
                return self.left.end_higher(self.left.key)
            else:
                return max(key_lower,self.left.end_higher(self.left.key))
    
    def search_higher(self,key,key_higher):
        if self.key == None:
            return key_higher
        if self.key > key:
            key_higher = self.key
            if self.left == None:
                return key_higher
            else:
                return self.left.search_higher(key,key_higher)
        if self.key < key:
            if self.right == None:
                return key_higher
            else:
                return self.right.search_higher(key,key_higher)
        #self.key == keyの場合
        if self.right == None:
            return key_higher
        else:
            if key_higher == None:
                return self.right.end_lower(self.right.key)
            else:
                return min(key_higher,self.right.end_lower(self.right.key))
        
    
    def end_lower(self,end_lower_key):
        if self.left == None:
            return end_lower_key
        else:
            end_lower_key = self.left.key
            return self.left.end_lower(end_lower_key)
    def end_higher(self,end_higher_key):
        if self.right == None:
            return end_higher_key
        else:
            end_higher_key = self.right.key
            return self.right.end_higher(end_higher_key)
    
            
    def DoubleRightRotation(self):
        tl = self.left
        self.left = tl.right.right
        tl.right.right = self # selfはそのノード
        tlr = tl.right
        tl.right = tlr.left
        tlr.left = tl
        if tlr.balance == "Left":
            tlr.right.balance = "Right"
            tlr.left.balance = "Even"
        elif tlr.balance == "Right":
            tlr.right.balance = "Even"
            tlr.left.balance = "Left"
        elif tlr.balance == "Even":
            tlr.right.balance = "Even"
            tlr.left.balance = "Even"
        tlr.balance = "Even"
        return tlr
    def DoubleLeftRotation(self):
        tr = self.right
        self.right = tr.left.left
        tr.left.left = self
        trl = tr.left
        tr.left = trl.right
        trl.right = tr
        if trl.balance == "Right":
            trl.left.balance = "Left"
            trl.right.balance = "Even"
        elif trl.balance == "Left":
            trl.left.balance = "Even"
            trl.right.balance = "Right"
        elif trl.balance == "Even":
            trl.left.balance = "Even"
            trl.right.balance = "Even"
        trl.balance = "Even"
        return trl
    def SingleLeftRotation(self):
        tr = self.right
        tr.balance = "Even"
        self.balance = "Even"
        self.right = tr.left
        tr.left = self
        return tr
    def SingleRightRotation(self):
        tl = self.left
        tl.balance = "Even"
        self.balance = "Even"
        self.left = tl.right
        tl.right = self
        return tl
    def replace(self,p,v): # 親ノードpの下にある自分(self)をvに置き換える。
        if p.left == self:
            p.left = v
        else :
            p.right = v 
    def insert(self,key): # rootでのみ呼ばれる挿入
        if self.key == None: # rootを含むrotationはしないことにする。
            self.key = key
            return self
        if key < self.key:
            if self.left == None:
                self.left = Avltree(key)
            else:
                self.left.insertx(self,key)
        elif key > self.key:
            if self.right == None:
                self.right = Avltree(key)
            else:
                self.right.insertx(self,key)
        else: # key == self.key:
            pass # do not overwrite
    def insertx(self,p,key): # replaceを呼ぶために一つ上の親を持っているinsert
        if self.key > key:
            if self.left == None:
                self.left = Avltree(key)
            else:
                if not self.left.insertx(self, key): # 左の木が生長しなければ、
                    return False # 成長しない
            if self.balance == "Right":
                self.balance = "Even"
                return False
            elif self.balance == "Even":
                self.balance = "Left"
                return True # 成長した
            elif self.balance == "Left":
                if self.left.balance == "Right":
                    self.replace(p,self.DoubleRightRotation())
                elif self.left.balance == "Left":
                    self.replace(p,self.SingleRightRotation())
                return False # rotationを行うと成長しない
        if self.key < key:
            if self.right == None:
                self.right = Avltree(key)
            else:
                if not self.right.insertx(self, key):
                    return False 
            if self.balance == "Left":
                self.balance = "Even"
                return False
            elif self.balance == "Even":
                self.balance = "Right"
                return True
            elif self.balance == "Right":
                if self.right.balance == "Left": 
                    self.replace(p,self.DoubleLeftRotation())
                elif self.right.balance == "Right":
                    self.replace(p,self.SingleLeftRotation())
                return False
        return False # self.key == keyの時は何もしないので成長もしない 

    def to_s(self):
        return self.key
    def left_s(self):
        if self.left == None:
            return None
        return (self.left).key
    def right_s(self):
        if self.right == None:
            return None
        return (self.right).key

from collections import defaultdict
# d = defaultdict(int)で0で初期化
# d = defaultdict(lambda: 100)で100で初期化
n = int(input())
a = [list(input().split()) for i in range(n)]
for i in range(n):
    if a[i][2] == "R":
        a[i][2] = 1
    elif a[i][2] == "L":
        a[i][2] = 2
    elif a[i][2] == "U" :
        a[i][2] = 3
    else:
        a[i][2] = 4
    a[i][0] = int(a[i][0])
    a[i][1]=int(a[i][1])
a.sort(key=lambda x: x[2])
R1 = defaultdict(Avltree)
R2 = defaultdict(Avltree)
L1 = defaultdict(Avltree)
L2 = defaultdict(Avltree)
U = defaultdict(Avltree)
R = defaultdict(Avltree)

M=10000000
for x, y, u in a:
    if u == 1:
        R1[y - x].insert(x)
        R2[x + y].insert(x)
        R[y].insert(x)
    elif u == 2:
        L1[x + y].insert(x)
        L2[y - x].insert(x)
        k = R[y].search_lower(x, None)
        if k:
            M = min((x - k) * 5, M)

    if u == 3:
        k = L2[y - x].search_higher(x, None)
        if k:
            M = min(M, (k - x) * 10)
        
        k = R2[x + y].search_lower(x, None)
        if k:
            M = min(M, (x - k) * 10)
        
        U[x].insert(y)
    if u == 4:
        k = L1[x + y].search_higher(x, None)
        if k:
            M = min(M, (k - x) * 10)

        k = R1[y - x].search_lower(x, None)
        if k:
            M = min(M, (x - k) * 10)

        k = U[x].search_lower(y, None)
        if k:
            M = min(M, (y - k) * 5)
print(M if M < 10000000 else "SAFE")

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
XYU = []
from collections import defaultdict
UR = defaultdict(list)
UL = defaultdict(list)
DR = defaultdict(list)
DL = defaultdict(list)
LR = defaultdict(list)
UD = defaultdict(list)
for _ in range(N):
    x, y, u = input().split()
    x, y = int(x), int(y)
    XYU.append((x, y, u))
XYU.sort()
for x, y, u in XYU:
    if u=='U':
        UL[(x-y)].append((x, y))
    if u=='R':
        LR[y].append((x, y))
    if u=='D':
        DL[(x+y)].append((x, y))

XYU.sort(key=lambda x:x[1])
for x, y, u in XYU:
    if u=='U':
        UR[(x+y)].append((x, y))
        UD[x].append((x, y))
    if u=='D':
        DR[(x-y)].append((x, y))

ans = 10**18
def check(x, y, u):
    global ans
    if u=='R':
        for px, py in UR[(x+y)]:
            if y<py: break
            if x<px and y>py:
                ans = min(ans, px-x)
                
        for px, py in DR[(x-y)]:
            if y<py: break
            if x<px and py>y:
                ans = min(ans, px-x)
                
    if u=='L':
        for px, py in UL[(x-y)]:
            if px>x: break
            if x>px and y>py:
                ans = min(ans, x-px)
                
        for px, py in DL[(x+y)]:
            if px>x: break
            if x>px and py>y:
                ans = min(ans, x-px) 
                
        for px, py in LR[y]:
            if px>x: break
            if x>px:
                ans = min(ans, (x-px)/2)
                
    if u=='D':
        for px, py in UD[x]:
            if y<py: break
            if py<y:
                ans = min(ans, (y-py)/2)

for x, y, u in XYU:
    check(x, y, u)

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

Submission status: Wrong Answer

Solution code:
N = int(input())
XYU = [list(map(str,input().split())) for _ in range(N)]
from collections import deque
INF = float('inf')

UD = [deque() for _ in range(200001)]
LR = [deque() for _ in range(200001)]


for X,Y,u in XYU:
  X = int(X)
  Y = int(Y)
  if (u=='R')or(u=='L'):
    LR[Y].append((X,u))
  else:
    UD[X].append((Y,u))
    
safe = True
ans = INF
for l in UD:
  length = len(l)
  if length>=2:
    l = sorted(l)
    for i in range(length - 1):
      a = l[i]
      b = l[i+1]
      if (a[1]=='U')and(b[1]=='D'):
        safe = False
        ans = min(ans,(b[0] - a[0])*5)
        
for l in LR:
  length = len(l)
  if length>=2:
    l = sorted(l)
    for i in range(length - 1):
      a = l[i]
      b = l[i+1]
      if (a[1]=='R')and(b[1]=='L'):
        safe = False
        ans = min(ans,(b[0] - a[0])*5)
          
if safe:
  print('SAFE')
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
big = 800001
sml = 400000
pp = [[[] for _ in range(big)] for _ in range(8)]
mm = [[[] for _ in range(big)] for _ in range(4)]
for _ in range(n):
  x,y,d = input().split()
  x = int(x)
  y = int(y)
  if d == 'R':
    pp[0][+x+y].append(+y)
    pp[2][+x-y].append(-y)
    mm[0][y].append(x)
  if d == 'L':
    pp[4][-x+y].append(+y)
    pp[6][-x-y].append(-y)
    mm[1][y].append(x)
  if d == 'U':
    pp[1][+x+y].append(+x)
    pp[5][-x+y].append(-x)
    mm[2][x].append(y)
  if d == 'D':
    pp[3][+x-y].append(+x)
    pp[7][-x-y].append(-x)
    mm[3][x].append(y)
import bisect
inf = 10**10
ans = inf
def ser(lx,ly,su):
  global ans
  for xx in lx:
    b = bisect.bisect_left(ly,su-xx)
    try:
      ans = min(ans,(xx+ly[b]-su)*10)
    except:
      pass
for d in range(4):
  xd = 2*d
  yd = 2*d+1
  for sun in range(-sml,sml+1):
    if pp[xd][sun] and pp[yd][sun]:
      #print(pp[xd][sun],pp[yd][sun],sun,xd,yd)
      ser(pp[xd][sun],pp[yd][sun],sun)
for sun in range(0,sml+1):
  if mm[0][sun] and mm[1][sun]:
    for xx in mm[0][sun]:
      b = bisect.bisect_left(mm[1][sun],xx)
      try:
        ans = min(ans,(mm[1][sun][b]-xx)*5)
      except:
        pass
for sun in range(0,sml+1):
  if mm[2][sun] and mm[3][sun]:
    for yy in mm[2][sun]:
      b = bisect.bisect_left(mm[3][sun],yy)
      try:
        ans = min(ans,(mm[3][sun][b]-yy)*5)
      except:
        pass
#print(ans)
if ans == inf:
  print('SAFE')
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
#root.search(x): ◯.data == x のものが存在するある場合それを出力、ない場合Noneを出力
#root.insert(x): ◯.data == xが存在する場合Falseを出力、ない場合◯.data == x なる頂点を作る
#y.to_s(): 頂点yのdataを出力
#y.left_s(): 頂点yのleftを出力(ない場合はNone)
#root.search_lower(x,None): ○.dataがxより小さいものの中で最も大きいものを出力、ない場合Noneを出力
#root.search_higher(x,None): xより大きいものの中で最も小さいものを出力、ない場合Noneを出力

################################################################################
class Avltree:
    def __init__(self,key=None):
        self.key = key
        self.left = None
        self.right = None 
        self.balance = "Even"
    def search(self,key):
        if self.key == None:
            return None
        if self.key > key:
            if self.left == None:
                return None
            else:
                return self.left.search(key)
        if self.key < key:
            if self.right == None:
                return None
            else:
                return self.right.search(key)
        return self # self.key == keyの場合
        
    def search_lower(self,key,key_lower):
        if self.key == None:
            return key_lower
        if self.key > key:
            if self.left == None:
                return key_lower
            else:
                return self.left.search_lower(key,key_lower)
        if self.key < key:
            key_lower = self.key
            if self.right == None:
                return key_lower
            else:
                return self.right.search_lower(key,key_lower)
        #self.key == keyの場合
        if self.left == None:
            return key_lower
        else:
            if key_lower == None:
                return self.left.end_higher(self.left.key)
            else:
                return max(key_lower,self.left.end_higher(self.left.key))
    
    def search_higher(self,key,key_higher):
        if self.key == None:
            return key_higher
        if self.key > key:
            key_higher = self.key
            if self.left == None:
                return key_higher
            else:
                return self.left.search_higher(key,key_higher)
        if self.key < key:
            if self.right == None:
                return key_higher
            else:
                return self.right.search_higher(key,key_higher)
        #self.key == keyの場合
        if self.right == None:
            return key_higher
        else:
            if key_higher == None:
                return self.right.end_lower(self.right.key)
            else:
                return min(key_higher,self.right.end_lower(self.right.key))
        
    
    def end_lower(self,end_lower_key):
        if self.left == None:
            return end_lower_key
        else:
            end_lower_key = self.left.key
            return self.left.end_lower(end_lower_key)
    def end_higher(self,end_higher_key):
        if self.right == None:
            return end_higher_key
        else:
            end_higher_key = self.right.key
            return self.right.end_higher(end_higher_key)
    
            
    def DoubleRightRotation(self):
        tl = self.left
        self.left = tl.right.right
        tl.right.right = self # selfはそのノード
        tlr = tl.right
        tl.right = tlr.left
        tlr.left = tl
        if tlr.balance == "Left":
            tlr.right.balance = "Right"
            tlr.left.balance = "Even"
        elif tlr.balance == "Right":
            tlr.right.balance = "Even"
            tlr.left.balance = "Left"
        elif tlr.balance == "Even":
            tlr.right.balance = "Even"
            tlr.left.balance = "Even"
        tlr.balance = "Even"
        return tlr
    def DoubleLeftRotation(self):
        tr = self.right
        self.right = tr.left.left
        tr.left.left = self
        trl = tr.left
        tr.left = trl.right
        trl.right = tr
        if trl.balance == "Right":
            trl.left.balance = "Left"
            trl.right.balance = "Even"
        elif trl.balance == "Left":
            trl.left.balance = "Even"
            trl.right.balance = "Right"
        elif trl.balance == "Even":
            trl.left.balance = "Even"
            trl.right.balance = "Even"
        trl.balance = "Even"
        return trl
    def SingleLeftRotation(self):
        tr = self.right
        tr.balance = "Even"
        self.balance = "Even"
        self.right = tr.left
        tr.left = self
        return tr
    def SingleRightRotation(self):
        tl = self.left
        tl.balance = "Even"
        self.balance = "Even"
        self.left = tl.right
        tl.right = self
        return tl
    def replace(self,p,v): # 親ノードpの下にある自分(self)をvに置き換える。
        if p.left == self:
            p.left = v
        else :
            p.right = v 
    def insert(self,key): # rootでのみ呼ばれる挿入
        if self.key == None: # rootを含むrotationはしないことにする。
            self.key = key
            return self
        if key < self.key:
            if self.left == None:
                self.left = Avltree(key)
            else:
                self.left.insertx(self,key)
        elif key > self.key:
            if self.right == None:
                self.right = Avltree(key)
            else:
                self.right.insertx(self,key)
        else: # key == self.key:
            pass # do not overwrite
    def insertx(self,p,key): # replaceを呼ぶために一つ上の親を持っているinsert
        if self.key > key:
            if self.left == None:
                self.left = Avltree(key)
            else:
                if not self.left.insertx(self, key): # 左の木が生長しなければ、
                    return False # 成長しない
            if self.balance == "Right":
                self.balance = "Even"
                return False
            elif self.balance == "Even":
                self.balance = "Left"
                return True # 成長した
            elif self.balance == "Left":
                if self.left.balance == "Right":
                    self.replace(p,self.DoubleRightRotation())
                elif self.left.balance == "Left":
                    self.replace(p,self.SingleRightRotation())
                return False # rotationを行うと成長しない
        if self.key < key:
            if self.right == None:
                self.right = Avltree(key)
            else:
                if not self.right.insertx(self, key):
                    return False 
            if self.balance == "Left":
                self.balance = "Even"
                return False
            elif self.balance == "Even":
                self.balance = "Right"
                return True
            elif self.balance == "Right":
                if self.right.balance == "Left": 
                    self.replace(p,self.DoubleLeftRotation())
                elif self.right.balance == "Right":
                    self.replace(p,self.SingleLeftRotation())
                return False
        return False # self.key == keyの時は何もしないので成長もしない 

    def to_s(self):
        return self.key
    def left_s(self):
        if self.left == None:
            return None
        return (self.left).key
    def right_s(self):
        if self.right == None:
            return None
        return (self.right).key

from collections import defaultdict
# d = defaultdict(int)で0で初期化
# d = defaultdict(lambda: 100)で100で初期化
n = int(input())
a = [list(input().split()) for i in range(n)]
for i in range(n):
    if a[i][2] == "R":
        a[i][2] = 1
    elif a[i][2] == "L":
        a[i][2] = 2
    elif a[i][2] == "U" :
        a[i][2] = 3
    else:
        a[i][2] = 4
    a[i][0] = int(a[i][0])
    a[i][1]=int(a[i][1])
a.sort(key=lambda x: x[2])
m = 200001
R1 = defaultdict(Avltree)
R2 = defaultdict(Avltree)
L1 = defaultdict(Avltree)
L2 = defaultdict(Avltree)
U = defaultdict(Avltree)
R = defaultdict(Avltree)

M=100000000000
for x, y, u in a:
    if u == 1:
        R1[y - x + m].insert(x)
        R2[x + y].insert(x)
        R[y].insert(x)
    elif u == 2:
        L1[x + y].insert(x)
        L2[y - x + m].insert(x)
        k = R[y].search_lower(x, None)
        if k:
            M = min((x - k) * 5, M)

    if u == 3:
        k = L2[y - x + m].search_higher(x, None)
        if k:
            M = min(M, (k-x)*10)
        k = R2[x + y].search_lower(x, None)
        if k:
            M = min(M, (x - k)*10)
        U[x].insert(y)
    if u == 4:
        k = L1[x + y].search_higher(x, None)
        if k:
            M = min(M, (k-x)*10)
        k = L1[y - x + m].search_lower(x, None)
        if k:
            M = min(M, (x - k)*10)
        k = U[x].search_lower(y, None)
        if k:
            M = min(M, (y-k)*5)
print(M if M<100000000000 else "SAFE")

Submission status: Wrong Answer

Solution code:
n = int(input())
x = [0]*n
y = [0]*n

ux = [0]*n
uy = [0]*n


for i in range(n):
    px,py,u =  input().split()
    if u=="U":
        uy[i] =1
    elif u=="R":
        ux[i] =1
    elif u=="D":
        uy[i] =-1
    elif u=="L":
        ux[i] =-1
    x[i]= int(px)
    y[i]= int(py)
time = 2*10**7
for i in range(n):
    for j in range(n-1-i):

            if x[i]-x[i+1+j] == 0 and  y[i]-y[i+1+j] == 0:
                time = min(time,0)
                break
            elif (x[i] - x[i + 1 + j]) == (y[i] - y[i + 1 + j]) and (ux[i] - ux[i + 1 + j]) == (uy[i] - uy[i + 1 + j]):
                time = min(time, abs((x[i] - x[i + 1 + j]) / 0.1))
            elif (x[i] - x[i + 1 + j]) == -(y[i] - y[i + 1 + j]) and (ux[i] - ux[i + 1 + j]) == -(uy[i] - uy[i + 1 + j]):
                time = min(time, abs((x[i] - x[i + 1 + j]) / 0.1))
            elif x[i]-x[i+1+j] == 0 and not uy[i]==0 and not uy[i+1+j]==0 and not uy[i+1+j]== uy[i]:
                if (y[i]-y[i+1+j])<0 and uy[i]>0:
                    time = min(time,abs((y[i]-y[i+1+j])/0.2))
                elif (y[i]-y[i+1+j])>0 and uy[i]<0:
                    time = min(time,abs((y[i]-y[i+1+j])/0.2))
            elif y[i]-y[i+1+j] == 0 and not ux[i]==0 and not ux[i+1+j]==0 and not ux[i+1+j]== uy[i]:
                if (x[i] - x[i + 1 + j]) < 0 and ux[i] > 0:
                    time = min(time,abs((x[i]-x[i+1+j])/0.2))
                elif (x[i] - x[i + 1 + j]) > 0 and ux[i] < 0:
                    time = min(time,abs((x[i]-x[i+1+j])/0.2))




if time == 2*10**7:
    print("SAFE")
else:
    print(int(time))

Submission status: Wrong Answer

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

only = [[[]for i in range(2*10**5+1)]for j in range(4)]
plus = [[[]for i in range(4*10**5+1)]for j in range(4)]
minus = [[[]for i in range(4*10**5+1)]for j in range(4)]
a = ['D','U','L','R']
for i in range(n):
    x,y,z = readstrs()
    x = int(x)
    y = int(y)
    b = a.index(z)
    if b<2:
        only[b][x].append(y)
    else:
        only[b][y].append(x)
    plus[b][x+y].append(x)
    minus[b][x-y].append(x)

def keisan(a,b,ans):
    a.sort()
    b.sort()
    k = 0
    j = 0
    while k<len(a) and j<len(b):
        if a[k]>b[j]:
            ans = min(ans,a[k]-b[j])
            j += 1
        else:
            k += 1
    return ans

for m in [0,2]:
    for i in range(2*10**5+1):
        a = only[m][i]
        b = only[m+1][i]
        if a and b:
            ans = keisan(a,b,ans)

if ans != 10**10:
    ans//=2

for p,q in [[2,1],[0,3]]:
    for i in range(4*10**5+1):
        a = minus[p][i]
        b = minus[q][i]
        if a and b:
            ans = keisan(a,b,ans)

for p,q in [[1,3],[2,0]]:
    for i in range(4*10**5+1):
        a = plus[p][i]
        b = plus[q][i]
        if a and b:
            ans = keisan(a,b,ans)

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

Submission status: Wrong Answer

Solution code:
N=int(input())
C=[]
d=[]
for i in range(N):
  X,Y,U=input().split()
  for j in range(len(C)):
    if (int(X)==C[j][0] or int(Y)==C[j][1] or int(X)+int(Y)==C[j][0]+C[j][1])and ((U=="U" and C[j][2]=="D")or(U=="D" and C[j][2]=="U")or(U=="R" and C[j][2]=="L")or(U=="L"and C[j][2]=="R")):
      d.append(max(abs(int(X)-C[j][0]),abs(int(Y)-C[j][1])))
  C.append([int(X),int(Y),U])
if not d:
  print("SAFE")
else:
  t=min(d)
  print(int(t*5))

Submission status: Wrong Answer

Solution code:
n = int(input())
x = [0]*n
y = [0]*n

ux = [0]*n
uy = [0]*n


for i in range(n):
    px,py,u =  input().split()
    if u=="U":
        uy[i] =1
    elif u=="R":
        ux[i] =1
    elif u=="D":
        uy[i] =-1
    elif u=="L":
        ux[i] =-1
    x[i]= int(px)
    y[i]= int(py)
time = 2*10**7
for i in range(n):
    for j in range(n-1-i):

            if x[i]-x[i+1+j] == 0 and  y[i]-y[i+1+j] == 0:
                time = min(time,0)
            elif x[i]-x[i+1+j] == 0 and not uy[i]==0 and not uy[i+1+j]==0 and not uy[i+1+j]== uy[i]:
                if (y[i]-y[i+1+j])<0 and uy[i]>0:
                    time = min(time,abs((y[i]-y[i+1+j])/0.2))
                elif (y[i]-y[i+1+j])>0 and uy[i]<0:
                    time = min(time,abs((y[i]-y[i+1+j])/0.2))
            elif y[i]-y[i+1+j] == 0 and not ux[i]==0 and not ux[i+1+j]==0 and not ux[i+1+j]== uy[i]:
                if (x[i] - x[i + 1 + j]) < 0 and ux[i] > 0:
                    time = min(time,abs((x[i]-x[i+1+j])/0.2))
                elif (x[i] - x[i + 1 + j]) > 0 and ux[i] < 0:
                    time = min(time,abs((x[i]-x[i+1+j])/0.2))
            elif (ux[i]-ux[i+1+j])==0 or (uy[i]-uy[i+1+j])==0:
                time =time

            elif (x[i]-x[i+1+j]) ==(y[i]-y[i+1+j]) and (ux[i]-ux[i+1+j]) ==(uy[i]-uy[i+1+j]):
                time = min(time,abs((x[i]-x[i+1+j])/0.1))
            elif (x[i]-x[i+1+j]) ==-(y[i]-y[i+1+j]) and (ux[i]-ux[i+1+j]) ==-(uy[i]-uy[i+1+j]):
                time = min(time,abs((x[i]-x[i+1+j])/0.1))

if time == 2*10**7:
    print("SAFE")
else:
    print(int(time))