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

Solution (Python):
from bisect import bisect
from collections import defaultdict

n = int(input())
up = [defaultdict(list), defaultdict(list), defaultdict(list)]
dp = [defaultdict(list), defaultdict(list), defaultdict(list)]
lp = [defaultdict(list), defaultdict(list), defaultdict(list)]
rp = [defaultdict(list), defaultdict(list), defaultdict(list)]

for _ in range(n):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    if u == 'U':
        up[0][x].append(y)
        up[1][x + y].append(x)
        up[2][x - y].append(x)
    elif u == 'D':
        dp[0][x].append(y)
        dp[1][x + y].append(x)
        dp[2][x - y].append(x)
    elif u == 'L':
        lp[0][y].append(x)
        lp[1][x + y].append(x)
        lp[2][x - y].append(x)
    else:
        rp[0][y].append(x)
        rp[1][x + y].append(x)
        rp[2][x - y].append(x)

INF = 10 ** 18
ans = INF

for x in up[0]:
    if x not in dp[0]:
        continue
    us = up[0][x]
    us.sort()
    for y in dp[0][x]:
        i = bisect(us, y)
        if i > 0:
            tmp = y - us[i - 1]
            ans = min(ans, tmp * 5)

for y in rp[0]:
    if y not in lp[0]:
        continue
    rs = rp[0][y]
    rs.sort()
    for x in lp[0][y]:
        i = bisect(rs, x)
        if i > 0:
            tmp = x - rs[i - 1]
            ans = min(ans, tmp * 5)

for xy in rp[1]:
    if xy not in up[1]:
        continue
    rs = rp[1][xy]
    rs.sort()
    for x in up[1][xy]:
        i = bisect(rs, x)
        if i > 0:
            tmp = x - rs[i - 1]
            ans = min(ans, tmp * 10)

for xy in dp[1]:
    if xy not in lp[1]:
        continue
    ds = dp[1][xy]
    ds.sort()
    for x in lp[1][xy]:
        i = bisect(ds, x)
        if i > 0:
            tmp = x - ds[i - 1]
            ans = min(ans, tmp * 10)

for xy in up[2]:
    if xy not in lp[2]:
        continue
    us = up[2][xy]
    us.sort()
    for x in lp[2][xy]:
        i = bisect(us, x)
        if i > 0:
            tmp = x - us[i - 1]
            ans = min(ans, tmp * 10)

for xy in rp[2]:
    if xy not in dp[2]:
        continue
    rs = rp[2][xy]
    rs.sort()
    for x in dp[2][xy]:
        i = bisect(rs, x)
        if i > 0:
            tmp = x - rs[i - 1]
            ans = min(ans, tmp * 10)

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

Solution (Python):
import sys
import collections
input = sys.stdin.readline

def main():
  N = int(input()[:-1])
  X = collections.defaultdict(list)  
  Y = collections.defaultdict(list)  
  XYp = collections.defaultdict(list) 
  XYn = collections.defaultdict(list) 
  YXp = collections.defaultdict(list) 
  YXn = collections.defaultdict(list) 
  
  for _ in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    if u in ('U', 'D'):
      X[x].append((y, u == 'U'))
    if u in ('R', 'L'):
      Y[y].append((x, u == 'R'))
    
    if u in ('U', 'R'):
      XYp[x + y].append((x - y, u == 'R'))
    if u in ('D', 'L'):
      XYn[x + y].append((x - y, u == 'D'))
      
    
    if u in ('U', 'L'):
      YXp[y - x].append((x + y, u == 'U'))
    if u in ('D', 'R'):
      YXn[y - x].append((x + y, u == 'R'))
  
  ans = 200000 * 100
  for x in [X, Y, XYp, XYn, YXp, YXn]:
    for V in x.values():
      V = sorted(V)
      
      for i in range(len(V)-1):
        if V[i][1] and not V[i+1][1]:
          ans = min(ans, V[i+1][0] - V[i][0])
  if ans == 200000 * 100:
    print('SAFE')
  else:
    print(ans * 5)
   
if __name__ == '__main__':
    main()

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    p = [input().split() for _ in range(n)]
    dr = defaultdict(lambda : [])
    dl = defaultdict(lambda : [])
    du = defaultdict(lambda : [])
    dd = defaultdict(lambda : [])
    ans = float("inf")
    for i in range(n):
        p[i][0] = int(p[i][0])
        p[i][1] = int(p[i][1])
        x,y,d = p[i]
        if d == "L":
            dl[y].append(x)
        elif d == "R":
            dr[y].append(x)
        elif d == "U":
            du[x].append(y)
        else:
            dd[x].append(y)
    for x in dl.keys():
        dl[x].sort()
    for x in dr.keys():
        dr[x].sort()
    for x in du.keys():
        du[x].sort()
    for x in dd.keys():
        dd[x].sort()
    for x,y,d in p:
        if d == "L":
            if dr[y]:
                i = bisect.bisect_left(dr[y],x)-1
                if i >= 0:
                    s = x-dr[y][i]
                    if s < ans:
                        ans = s
        elif d == "R":
            if dl[y]:
                i = bisect.bisect_right(dl[y],x)
                if i < len(dl[y]):
                    s = dl[y][i]-x
                    if s < ans:
                        ans = s
        elif d == "D":
            if du[x]:
                i = bisect.bisect_left(du[x],y)-1
                if i >= 0:
                    s = y-du[x][i]
                    if s < ans:
                        ans = s
        else:
            if dd[x]:
                i = bisect.bisect_right(dd[x],y)
                if i < len(dd[x]):
                    s = dd[x][i]-y
                    if s < ans:
                        ans = s
    dux = defaultdict(lambda : [])
    ddx = defaultdict(lambda : [])
    duy = defaultdict(lambda : [])
    ddy = defaultdict(lambda : [])
    for i,(a,b,d) in enumerate(p):
        x = a-b
        y = a+b
        p[i][0] = x
        p[i][1] = y
        if d == "U":
            dux[y].append(x)
            duy[x].append(y)
        elif d == "D":
            ddx[y].append(x)
            ddy[x].append(y)
    for x in dux.keys():
        dux[x].sort()
    for x in ddx.keys():
        ddx[x].sort()
    for x in duy.keys():
        duy[x].sort()
    for x in ddy.keys():
        ddy[x].sort()
    for x,y,d in p:
        if d == "L":
            if ddx[y]:
                i = bisect.bisect_left(ddx[y],x)-1
                if i >= 0:
                    s = x-ddx[y][i]
                    if s < ans:
                        ans = s
            if duy[x]:
                i = bisect.bisect_left(duy[x],y)-1
                if i >= 0:
                    s = y-duy[x][i]
                    if s < ans:
                        ans = s
        elif d == "R":
            if dux[y]:
                i = bisect.bisect_right(dux[y],x)
                if i < len(dux[y]):
                    s = dux[y][i]-x
                    if s < ans:
                        ans = s
            if ddy[x]:
                i = bisect.bisect_right(ddy[x],y)
                if i < len(ddy[x]):
                    s = ddy[x][i]-y
                    if s < ans:
                        ans = s
    if ans == float("inf"):
        print("SAFE")
    else:
        print(ans*5)
    return


if __name__ == "__main__":
    solve()

Solution (Python):

import sys

N, *XYU = map(str, sys.stdin.read().split())

UD = {}
UR = {}
UL = {}
DU = {}
DR = {}
DL = {}
RU = {}
RD = {}
RL = {}
LU = {}
LD = {}
LR = {}

for i in range(int(N)):
  X = int(XYU[3*i])
  Y = int(XYU[3*i+1])
  U = XYU[3*i+2]
  if U == 'U':
    if X in UD.keys():
      UD[X].append(Y)
    else:
      UD[X] = [Y]
    if X+Y in UR.keys():
      UR[X+Y].append(X)
    else:
      UR[X+Y] = [X]
    if X-Y in UL.keys():
      UL[X-Y].append(X)
    else:
      UL[X-Y] = [X]
  elif U == 'D':
    if X in DU.keys():
      DU[X].append(Y)
    else:
      DU[X] = [Y]
    if X+Y in DL.keys():
      DL[X+Y].append(X)
    else:
      DL[X+Y] = [X]
    if X-Y in DR.keys():
      DR[X-Y].append(X)
    else:
      DR[X-Y] = [X]
  elif U == 'R':
    if Y in RL.keys():
      RL[Y].append(X)
    else:
      RL[Y] = [X]
    if X+Y in RU.keys():
      RU[X+Y].append(X)
    else:
      RU[X+Y] = [X]
    if X-Y in RD.keys():
      RD[X-Y].append(X)
    else:
      RD[X-Y] = [X]
  else:
    if Y in LR.keys():
      LR[Y].append(X)
    else:
      LR[Y] = [X]
    if X+Y in LD.keys():
      LD[X+Y].append(X)
    else:
      LD[X+Y] = [X]
    if X-Y in LU.keys():
      LU[X-Y].append(X)
    else:
      LU[X-Y] = [X]

def dist(X,Y):
  minimum = -1
  X_len = len(X)
  X_index = 0
  Y_len = len(Y)
  Y_index = 0
  while X_index<X_len and Y_index<Y_len:
    x = X[X_index]
    y = Y[Y_index]
    if x<y:
      if minimum == -1:
        minimum = y-x
      else:
        minimum = min(minimum, y-x)
      X_index += 1
    else:
      Y_index += 1
  return minimum

second = -1

for x in UD.keys():
  if x in DU.keys():
    distance = dist(sorted(UD[x]),sorted(DU[x]))
    if distance>0:
      if second == -1:
        second = 5*distance
      else:
        second = min(second,5*distance)

for x in RL.keys():
  if x in LR.keys():
    distance = dist(sorted(RL[x]),sorted(LR[x]))
    if distance>0:
      if second == -1:
        second = 5*distance
      else:
        second = min(second,5*distance)

for x in UR.keys():
  if x in RU.keys():
    distance = dist(sorted(RU[x]),sorted(UR[x]))
    if distance>0:
      if second == -1:
        second = 10*distance
      else:
        second = min(second,10*distance)

for x in UL.keys():
  if x in LU.keys():
    distance = dist(sorted(UL[x]),sorted(LU[x]))
    if distance>0:
      if second == -1:
        second = 10*distance
      else:
        second = min(second,10*distance)

for x in RD.keys():
  if x in DR.keys():
    distance = dist(sorted(RD[x]),sorted(DR[x]))
    if distance>0:
      if second == -1:
        second = 10*distance
      else:
        second = min(second,10*distance)

for x in LD.keys():
  if x in DL.keys():
    distance = dist(sorted(DL[x]),sorted(LD[x]))
    if distance>0:
      if second == -1:
        second = 10*distance
      else:
        second = min(second,10*distance)

if second == -1:
  print('SAFE')
else:
  print(second)

Solution (Python):
import sys
import numpy as np
import numba
from numba import njit
i8 = numba.int64

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

@njit((i8[:], i8[:], i8[:], i8[:]), cache=True)
def f(X1, Y1, X2, Y2):
    
    ret = 10**18
    key1 = (X1 << 32) + Y1
    key2 = (X2 << 32) + Y2
    ind = key1.argsort()
    X1, Y1, key1 = X1[ind], Y1[ind], key1[ind]
    ind = key2.argsort()
    X2, Y2, key2 = X2[ind], Y2[ind], key2[ind]
    js = np.searchsorted(key2, key1)
    for i in range(len(key1)):
        j = js[i]
        if j == len(key2):
            continue
        if X1[i] != X2[j]:
            continue
        ret = min(ret, Y2[j] - Y1[i])
    return ret

N = int(readline())
X = []
Y = []
U = []
for _ in range(N):
    x, y, u = readline().split()
    X.append(int(x))
    Y.append(int(y))
    U.append(u)

X = np.array(X, np.int64)
Y = np.array(Y, np.int64)
U = np.array(U, 'S1')

i_u = U == b'U'
i_r = U == b'R'
i_d = U == b'D'
i_l = U == b'L'

Xu, Yu = X[i_u], Y[i_u]
Xr, Yr = X[i_r], Y[i_r]
Xd, Yd = X[i_d], Y[i_d]
Xl, Yl = X[i_l], Y[i_l]

INF = 10**18
ans = INF
ans = min(ans, f(Xu, Yu, Xd, Yd))  
ans = min(ans, f(Yr, Xr, Yl, Xl))  
ans = min(ans, f(Xr + Yr, Xr - Yr, Xu + Yu, Xu - Yu))  
ans = min(ans, f(Xu - Yu, Xu + Yu, Xl - Yl, Xl + Yl))  
ans = min(ans, f(Xr - Yr, Xr + Yr, Xd - Yd, Xd + Yd))  
ans = min(ans, f(Xd + Yd, Xd - Yd, Xl + Yl, Xl - Yl))  
print('SAFE' if ans == INF else 5 * ans)