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 in Ruby (2.7.1) (judge verdict: Runtime Error)

#!/usr/bin/env ruby

require 'set'

class Hash
  def push(key, value)
    self[key] = [] if self[key] == nil
    self[key].push(value)
  end
end

class Array
  def lower_bound(value)
    left = -1;
    right = self.length;
    while left + 1 < right
      mid = left + (right - left) / 2;
      if self[mid] >= value
        right = mid
      else
        left = mid
      end
    end
    right
  end

  def unique
    res = [self.first]
    each_cons(2) do |a, b|
      if a != b
        res.push(b)
      end
    end
    res
  end
end

def get_ints
  gets.chomp.split.map(&:to_i)
end

def get_ints_minus_one
  get_ints.map { |x| x - 1 }
end

def get_int
  gets.chomp.to_i
end

N = get_int

up = []
down = []
right = []
left = []
N.times do |i|
  x, y, u = gets.chomp.split
  x = x.to_i
  y = y.to_i
  case u
  when 'U'
    up << [x, y]
  when 'R'
    right << [x, y]
  when 'L'
    left << [x, y]
  when 'D'
    down << [x, y]
  end
end

crash_length = []
crash = false

up_and_right = Hash.new { |h, k| h[k] = [] }
up.each do |x, y|
  if y > 0
    x = x + y
  elsif y < 0
    x = x - y
  end
  up_and_right[x] << y
end
up_and_right.transform_values! { |v| v.sort }

right.each do |x, y|
  if y > 0
    x = x + y
  else
    x = x - y
  end
  if up_and_right[x].length > 0
    index = up_and_right[x].bsearch_index { |v| v > y }
    next if index == 0 # 位置的にupのほうが最初から上にいたならぶつからない

    crash = true
    sa = if index == nil
      (up_and_right[x].last - y).abs
    else
      (up_and_right[x][index-1] - y).abs
    end
    crash_length << sa * 10
  end
end

up_and_left = Hash.new { |h, k| h[k] = [] }
up.each do |x, y|
  if y > 0
    x = x - y
  elsif y < 0
    x = x + y
  end
  up_and_left[x] << y
end
up_and_left.transform_values! { |v| v.sort }

left.each do |x, y|
  if y > 0
    x = x - y
  else
    x = x + y
  end
  if up_and_left[x].length > 0
    index = up_and_left[x].bsearch_index { |x| x > y }
    next if index == 0 # 位置的にupのほうが最初から上にいたならぶつからない

    crash = true
    sa = if index == nil
      (up_and_left[x].last - y).abs
    else
      (up_and_right[x][index-1] - y).abs
    end
    crash_length << sa * 10
  end
end

down_and_right = Hash.new { |h, k| h[k] = [] }
down.each do |x, y|
  if y > 0
    x = x - y
  elsif y < 0
    x = x + y
  end
  down_and_right[x] << y
end
down_and_right.transform_values! { |v| v.sort }

right.each do |x, y|
  if y > 0
    x = x - y
  else
    x = x + y
  end
  if down_and_right[x].length > 0
    index = down_and_right[x].bsearch_index { |x| x >= y }
    next if index == nil # 位置的にdownのほうが最初から下にいたならぶつからない

    crash = true
    sa = if index == 0
      (down_and_right[x].first - y).abs
    else
      (down_and_right[x][index] - y).abs
    end
    crash_length << sa * 10
  end
end

down_and_left = Hash.new { |h, k| h[k] = [] }
down.each do |x, y|
  if y > 0
    x = x + y
  elsif y < 0
    x = x - y
  end
  down_and_left[x] << y
end
down_and_left.transform_values! { |v| v.sort }

left.each do |x, y|
  if y > 0
    x = x + y
  else
    x = x - y
  end
  if down_and_left[x].length > 0
    index = down_and_left[x].bsearch_index { |x| x >= y }
    next if index == nil # 位置的にdownのほうが最初から下にいたならぶつからない

    crash = true
    sa = if index == 0
      (down_and_left[x].first - y).abs
    else
      (down_and_left[x][index] - y).abs
    end
    crash_length << sa * 10
  end
end

up_and_down = Hash.new { |h, k| h[k] = [] }
up.each do |x, y|
  up_and_down[x] << y
end
up_and_down.transform_values! { |v| v.sort }

down.each do |x, y|
  if up_and_down[x].length > 0
    index = up_and_down[x].bsearch_index { |v| v >= y }
    next if index == 0 # downのほうが最初から上、もしくは差が2の倍数じゃないならぶつからない

    crash = true
    index = up_and_down[x].length - 1 if index.nil?
    sa = (up_and_down[x][index]*10 - y*10).abs / 2
    crash_length << sa
  end
end

left_and_right = Hash.new { |h, k| h[k] = [] }
left.each do |x, y|
  left_and_right[y] << x
end
left_and_right.transform_values! { |v| v.sort }

right.each do |x, y|
  if left_and_right[y].length > 0
    index = left_and_right[y].bsearch_index { |v| v >= x }
    next if index.nil? # leftのほうが最初から左、もしくは差が2の倍数じゃないならぶつからない

    crash = true
    sa = (left_and_right[y][index] * 10 - x * 10).abs / 2
    crash_length << sa
  end
end

if crash
  puts crash_length.sort.first
else
  puts 'SAFE'
end