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 C# (Mono-csc 3.5.0) (judge verdict: Accepted)

using System;
using System.Linq;
using System.Collections.Generic;

namespace Contest
{
  class Program
  {
    static void Main(string[] args)
    {
      MSOLUTIONS2020.F();
    }

    class MSOLUTIONS2020
    {
      public static void A()
      {
        var X = Read.Int();
        Console.WriteLine((2199 - X) / 200);
      }
      public static void B()
      {
        var A = Read.Int();
        var B = Read.Int();
        var C = Read.Int();
        var K = Read.Int();
        int count = 0;
        while (B <= A)
        {
          B *= 2;
          count++;
        }
        while (C <= B)
        {
          C *= 2;
          count++;
        }
        Console.WriteLine(count <= K ? "Yes" : "No");
      }
      public static void C()
      {
        var N = Read.Int();
        var K = Read.Int();
        var A = Read.LongN();
        var sb = new System.Text.StringBuilder();
        for(int i = K; i < N; ++i)
        {
          sb.AppendLine(A[i - K] < A[i] ? "Yes" : "No");
        }
        Console.Write(sb.ToString());
      }
      public static void D()
      {
        var N = Read.Int();
        var A = Read.LongN();
        long money = 1000;
        long min = A[0];
        long max = A[0];
        for(int i = 1; i < N; ++i)
        {
          if(A[i] > max) { max = A[i]; }
          else
          {
            money += money / min * (max - min);
            max = A[i];
            min = A[i];
          }
        }
        money += money / min * (max - min);
        Console.WriteLine(money);
      }
      public static void E()
      {
        var N = Read.Int();
        var XYP = Enumerable.Range(0, N).Select(i => Read.LongN()).ToArray();
        var min = Enumerable.Range(0, N + 1).Select(i => (long)1e18).ToArray();

      }
      public static void F()
      {
        var N = Read.Int();
        var listU = new List<Tuple<int, int>>();
        var listD = new List<Tuple<int, int>>();
        var listL = new List<Tuple<int, int>>();
        var listR = new List<Tuple<int, int>>();
        for(int i = 0; i < N; ++i)
        {
          var X = Read.Int();
          var Y = Read.Int();
          var U = Read.Str();
          var t = new Tuple<int, int>(X, Y);
          if (U == "U") { listU.Add(t); }
          if (U == "D") { listD.Add(t); }
          if (U == "L") { listL.Add(t); }
          if (U == "R") { listR.Add(t); }
        }
        long min = (long)1e18;
        {
          var dictL = new Dictionary<int, List<int>>();
          var dictR = new Dictionary<int, List<int>>();
          foreach (var t in listL)
          {
            if (!dictL.ContainsKey(t.Item2)) { dictL.Add(t.Item2, new List<int>()); }
            dictL[t.Item2].Add(t.Item1);
          }
          foreach (var t in listR)
          {
            if (!dictR.ContainsKey(t.Item2)) { dictR.Add(t.Item2, new List<int>()); }
            dictR[t.Item2].Add(t.Item1);
          }
          min = Math.Min(min, F_LR(dictL, dictR));
        }
        {
          var dictL = new Dictionary<int, List<int>>();
          var dictR = new Dictionary<int, List<int>>();
          foreach (var t in listD)
          {
            if (!dictL.ContainsKey(t.Item1)) { dictL.Add(t.Item1, new List<int>()); }
            dictL[t.Item1].Add(t.Item2);
          }
          foreach (var t in listU)
          {
            if (!dictR.ContainsKey(t.Item1)) { dictR.Add(t.Item1, new List<int>()); }
            dictR[t.Item1].Add(t.Item2);
          }
          min = Math.Min(min, F_LR(dictL, dictR));
        }
        {
          var dict1 = new Dictionary<int, List<int>>();
          var dict2 = new Dictionary<int, List<int>>();
          foreach (var t in listR)
          {
            var key = t.Item2 - t.Item1;
            var value = t.Item1;
            var d = dict1;
            if (!d.ContainsKey(key)) { d.Add(key, new List<int>()); }
            d[key].Add(value);
          }
          foreach (var t in listD)
          {
            var key = t.Item2 - t.Item1;
            var value = t.Item1;
            var d = dict2;
            if (!d.ContainsKey(key)) { d.Add(key, new List<int>()); }
            d[key].Add(value);
          }
          min = Math.Min(min, F_RD(dict1, dict2));
        }
        {
          var dict1 = new Dictionary<int, List<int>>();
          var dict2 = new Dictionary<int, List<int>>();
          foreach (var t in listD)
          {
            var key = t.Item2 + t.Item1;
            var value = t.Item1;
            var d = dict1;
            if (!d.ContainsKey(key)) { d.Add(key, new List<int>()); }
            d[key].Add(value);
          }
          foreach (var t in listL)
          {
            var key = t.Item2 + t.Item1;
            var value = t.Item1;
            var d = dict2;
            if (!d.ContainsKey(key)) { d.Add(key, new List<int>()); }
            d[key].Add(value);
          }
          min = Math.Min(min, F_RD(dict1, dict2));
        }
        {
          var dict1 = new Dictionary<int, List<int>>();
          var dict2 = new Dictionary<int, List<int>>();
          foreach (var t in listL)
          {
            var key = t.Item2 - t.Item1;
            var value = -t.Item1;
            var d = dict1;
            if (!d.ContainsKey(key)) { d.Add(key, new List<int>()); }
            d[key].Add(value);
          }
          foreach (var t in listU)
          {
            var key = t.Item2 - t.Item1;
            var value = -t.Item1;
            var d = dict2;
            if (!d.ContainsKey(key)) { d.Add(key, new List<int>()); }
            d[key].Add(value);
          }
          min = Math.Min(min, F_RD(dict1, dict2));
        }
        {
          var dict1 = new Dictionary<int, List<int>>();
          var dict2 = new Dictionary<int, List<int>>();
          foreach (var t in listU)
          {
            var key = t.Item2 + t.Item1;
            var value = -t.Item1;
            var d = dict1;
            if (!d.ContainsKey(key)) { d.Add(key, new List<int>()); }
            d[key].Add(value);
          }
          foreach (var t in listR)
          {
            var key = t.Item2 + t.Item1;
            var value = -t.Item1;
            var d = dict2;
            if (!d.ContainsKey(key)) { d.Add(key, new List<int>()); }
            d[key].Add(value);
          }
          min = Math.Min(min, F_RD(dict1, dict2));
        }
        Console.WriteLine(min == (long)1e18 ? "SAFE" : min.ToString());
      }
      static long F_LR(Dictionary<int, List<int>> L, Dictionary<int, List<int>> R)
      {
        long min = (long)1e18;
        foreach(var kvp in L)
        {
          if (!R.ContainsKey(kvp.Key)){ continue; }
          var Ls = kvp.Value.OrderBy(x => x).ToArray();
          foreach(var v in R[kvp.Key])
          {
            var idx = Bound.Upperbound(Ls, v);
            if (idx < Ls.Length)
            {
              min = Math.Min(min, (Ls[idx] - v) * 5);
            }
          }
        }
        return min;
      }
      static long F_RD(Dictionary<int, List<int>> R, Dictionary<int, List<int>> D)
      {
        long min = (long)1e18;
        foreach (var kvp in R)
        {
          if (!D.ContainsKey(kvp.Key)) { continue; }
          var Ds = D[kvp.Key].OrderBy(x => x).ToArray();
          foreach (var v in kvp.Value)
          {
            var idx = Bound.Upperbound(Ds, v);
            if (idx < Ds.Length)
            {
              min = Math.Min(min, (Ds[idx] - v) * 10);
            }
          }
        }
        return min;
      }
    }






    class Util
    {
      public static T[,] Gen<T>(int r, int c, T v)
      {
        var matrix = new T[r, c];
        for (int i = 0; i < r; ++i)
        {
          for (int j = 0; j < c; ++j)
          {
            matrix[i, j] = v;
          }
        }
        return matrix;
      }
      public static long[] ASum(long[] array)
      {
        var res = new long[array.Length + 1];
        for (int i = 0; i < array.Length; ++i)
        {
          res[i + 1] = res[i] + array[i];
        }
        return res;
      }
      public static long[,] ASum(long[,] matrix)
      {
        var N = matrix.GetLength(0);
        var M = matrix.GetLength(1);
        var res = new long[N + 1, M + 1];
        for(int i = 0; i < N; ++i)
        {
          for(int j = 0; j < M; ++j)
          {
            res[i + 1, j + 1] = res[i, j + 1] + res[i + 1, j] - res[i, j] + matrix[i, j];
          }
        }
        return res;
      }
      public static MyDict<T, long> Frequency2<T>(T[] array)
      {
        var dict = new MyDict<T, long>();
        foreach (var a in array)
        {
          dict[a]++;
        }
        return dict;
      }
      public class MyDict<T1, T2> : Dictionary<T1, T2>
      {
        public new T2 this[T1 key]
        {
          get { if (!base.ContainsKey(key)) { base.Add(key, default(T2)); } return base[key]; }
          set { base[key] = value; }
        }
      }

      public static void WarshallFloyd(long[,] dist)
      {
        var N = dist.GetLength(0);
        for (int k = 0; k < N; ++k)
        {
          for (int i = 0; i < N; ++i)
          {
            for (int j = 0; j < N; ++j)
            {
              dist[i, j] = Math.Min(dist[i, j], dist[i, k] + dist[k, j]);
            }
          }
        }
      }
      public static long[,] DistFromAdj(int N, Dictionary<int, Dictionary<int, long>> adj)
      {
        var dist = new long[N, N];
        for (int i = 0; i < N; ++i)
        {
          for (int j = 0; j < N; ++j)
          {
            dist[i, j] = adj.ContainsKey(i + 1) && adj[i + 1].ContainsKey(j + 1) ? adj[i + 1][j + 1] : (long)1e18;
          }
        }
        return dist;
      }

    }
    class Bound
    {
      public static int Lowerbound<T1, T2>(SortedList<T1, T2> list, T1 x) where T1 : IComparable<T1>
      {
        return Lowerbound(list.Keys, x);
      }
      public static int Upperbound<T1, T2>(SortedList<T1, T2> list, T1 x) where T1 : IComparable<T1>
      {
        return Upperbound(list.Keys, x);
      }
      public static int Lowerbound<T>(IList<T> list, T x) where T : IComparable<T>
      {
        int min = 0;
        int max = list.Count;
        while (min < max)
        {
          var mid = (min + max) / 2;
          if (list[mid].CompareTo(x) >= 0)
          {
            max = mid;
          }
          else
          {
            min = mid + 1;
          }
        }
        return max;
      }
      public static int Upperbound<T>(IList<T> list, T x) where T : IComparable<T>
      {
        int min = 0;
        int max = list.Count;
        while (min < max)
        {
          var mid = (min + max) / 2;
          if (list[mid].CompareTo(x) > 0)
          {
            max = mid;
          }
          else
          {
            min = mid + 1;
          }
        }
        return max;
      }

    }

    struct MOD
    {
      public static long P = 1000000007;
      public readonly long V;

      public MOD(long a) { this.V = a % P; }
      public static implicit operator MOD(long a) { return new MOD(a); }
      public static MOD operator +(MOD a, MOD b) { return new MOD((a.V + b.V) % P); }
      public static MOD operator -(MOD a, MOD b) { return new MOD((a.V - b.V + P) % P); }
      public static MOD operator *(MOD a, MOD b) { return new MOD((a.V * b.V) % P); }
      public static MOD operator /(MOD a, MOD b) { return new MOD((a.V * b.Inverse().V) % P); }
      public MOD Inverse()
      {
        long a = V, p = P; long b = p, c = 1, d = 0, e = 0, f = 0;
        while (b != 0) { e = a / b; f = b; b = a - e * b; a = f; f = d; d = c - e * d; c = f; }
        c %= p;
        return c + (c < 0 ? p : 0);
      }
      public MOD Power(long n)
      {
        long a = V, p = P; long res = 1; long mult = a;
        for (int i = 0; i < 63; ++i) { if ((n & ((long)1 << i)) > 0) { res = (res * mult) % p; } mult = (mult * mult) % p; }
        return res;
      }
      public override string ToString() { return V.ToString(); }
    }


    static class Read
{
static Queue<string> queue = new Queue<string>();
static void Enqueue() { var ss = Console.ReadLine().Split(' '); foreach (var s in ss) { queue.Enqueue(s); } }
public static int Int() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt32(queue.Dequeue()); }
public static long Long() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt64(queue.Dequeue()); }
public static string Str() { if (queue.Count == 0) { Enqueue(); } return queue.Dequeue(); }
public static int[] IntN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray(); }
public static long[] LongN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray(); }
public static ulong[] ULongN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToUInt64(s)).ToArray(); }
public static string[] StrN() { return Console.ReadLine().Split(' '); }
public static Tuple<int, int>[] TupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = Read.IntN(); list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); }
public static Tuple<long, long>[] TupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); }
public static Tuple<string, string>[] TupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = Read.StrN(); list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); }
public static Tuple<double, double>[] TupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); }
public static long[,] LongMatrix(int r, int c) { var mat = new long[r, c]; for (int i = 0; i < r; ++i) { var x = Read.LongN(); for (int j = 0; j < c; ++j) { mat[i, j] = x[j]; } } return mat; }
public static Dictionary<int, HashSet<int>> AdjacencyList(int N, int M, bool isDirectional = false)
{
Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>();
for (int i = 0; i <= N; ++i) { dict.Add(i, new HashSet<int>()); }
for (int i = 0; i < M; ++i) { var ab = Read.IntN(); if (!dict[ab[0]].Contains(ab[1])) { dict[ab[0]].Add(ab[1]); } if (!isDirectional && !dict[ab[1]].Contains(ab[0])) { dict[ab[1]].Add(ab[0]); } }
return dict;
}
public static Dictionary<int, Dictionary<int, long>> AdjacencyListWithWeight(int N, int M, bool isDirectional = false)
{
Dictionary<int, Dictionary<int, long>> dict = new Dictionary<int, Dictionary<int, long>>(); for (int i = 0; i <= N; ++i) { dict.Add(i, new Dictionary<int, long>()); }
for (int i = 0; i < M; ++i) { var ab = Read.IntN(); dict[ab[0]].Add(ab[1], ab[2]); if (!isDirectional) { dict[ab[1]].Add(ab[0], ab[2]); } }
return dict;
}
}
    }
}