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# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using CompLib.Util;

public class Program
{
    private int N;
    private long[] X, Y;
    private char[] U;

    private long Ans;


    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        X = new long[N];
        Y = new long[N];
        U = new char[N];
        for (int i = 0; i < N; i++)
        {
            X[i] = sc.NextInt();
            Y[i] = sc.NextInt();
            U[i] = sc.NextChar();
        }

        var up = new List<int>();
        var down = new List<int>();
        var right = new List<int>();
        var left = new List<int>();
        for (int i = 0; i < N; i++)
        {
            if (U[i] == 'U') up.Add(i);
            if (U[i] == 'D') down.Add(i);
            if (U[i] == 'R') right.Add(i);
            if (U[i] == 'L') left.Add(i);
        }

        Ans = long.MaxValue;

        // 正面衝突

        #region 正面衝突

        {
            // U,D
            // Xが同じ && DのY > UのY
            var map = new Dictionary<long, List<(long y, bool up)>>();
            foreach (int i in up)
            {
                if (!map.TryGetValue(X[i], out _)) map[X[i]] = new List<(long y, bool up)>();
                map[X[i]].Add((Y[i], true));
            }

            foreach (int i in down)
            {
                if (!map.TryGetValue(X[i], out _)) map[X[i]] = new List<(long y, bool up)>();
                map[X[i]].Add((Y[i], false));
            }

            foreach (var pair in map)
            {
                var ls = pair.Value;
                ls.Sort((l, r) => l.y.CompareTo(r.y));
                long lastUp = long.MinValue;
                foreach ((long y, bool up) t in ls)
                {
                    if (t.up)
                    {
                        lastUp = t.y;
                    }
                    else
                    {
                        if (lastUp != long.MinValue) Ans = Math.Min(Ans, (t.y - lastUp) * 5);
                    }
                }
            }
        }

        {
            // l,r
            // Yが同じ
            var map = new Dictionary<long, List<(long x, bool r)>>();
            foreach (int i in right)
            {
                if (!map.TryGetValue(Y[i], out _)) map[Y[i]] = new List<(long x, bool r)>();
                map[Y[i]].Add((X[i], true));
            }

            foreach (int i in left)
            {
                if (!map.TryGetValue(Y[i], out _)) map[Y[i]] = new List<(long x, bool r)>();
                map[Y[i]].Add((X[i], false));
            }

            foreach (var pair in map)
            {
                var ls = pair.Value;
                ls.Sort((l, r) => l.x.CompareTo(r.x));
                long lastRight = long.MinValue;
                foreach ((long x, bool up) t in ls)
                {
                    if (t.up)
                    {
                        lastRight = t.x;
                    }
                    else
                    {
                        if (lastRight != long.MinValue) Ans = Math.Min(Ans, (t.x - lastRight) * 5);
                    }
                }
            }
        }

        #endregion

        // L字

        // 45度の位置にある
        {
            // d or left
            var dl = new Dictionary<long, List<(long x, bool d)>>();
            foreach (int i in down)
            {
                long s = X[i] + Y[i];
                if (!dl.TryGetValue(s, out _)) dl[s] = new List<(long x, bool d)>();
                dl[s].Add((X[i], true));
            }

            foreach (int i in left)
            {
                long s = X[i] + Y[i];
                if (!dl.TryGetValue(s, out _)) dl[s] = new List<(long x, bool d)>();
                dl[s].Add((X[i], false));
            }

            // xでソート
            foreach (var pair in dl)
            {
                var ls = pair.Value;
                ls.Sort((l, r) => l.x.CompareTo(r.x));
                long lastDown = long.MinValue;
                foreach (var t in ls)
                {
                    if (t.d) lastDown = t.x;
                    else
                    {
                        if (lastDown != long.MinValue) Ans = Math.Min(Ans, (t.x - lastDown) * 10);

                        #if DEBUG
                        if (Ans == -100)
                        {
                            Console.WriteLine("dl");
                        }
                        #endif
                    }
                }
            }
        }

        {
            // u or r
            var ur = new Dictionary<long, List<(long x, bool u)>>();
            foreach (int i in up)
            {
                long s = X[i] + Y[i];
                if (!ur.TryGetValue(s, out _)) ur[s] = new List<(long x, bool d)>();
                ur[s].Add((X[i], true));
            }

            foreach (int i in right)
            {
                long s = X[i] + Y[i];
                if (!ur.TryGetValue(s, out _)) ur[s] = new List<(long x, bool d)>();
                ur[s].Add((X[i], false));
            }

            // xでソート
            foreach (var pair in ur)
            {
                var ls = pair.Value;
                ls.Sort((l, r) => r.x.CompareTo(l.x));
                long lastUp = long.MaxValue;
                foreach (var t in ls)
                {
                    if (t.u) lastUp = t.x;
                    else
                    {
                        if (lastUp != long.MaxValue) Ans = Math.Min(Ans, (lastUp - t.x) * 10);
                        
#if DEBUG
                        if (Ans == -100)
                        {
                            Console.WriteLine("ur");
                        }
#endif
                    }
                }
            }
        }

        {
            // u or left
            var ul = new Dictionary<long, List<(long x, bool d)>>();
            foreach (int i in up)
            {
                long s = X[i] - Y[i];
                if (!ul.TryGetValue(s, out _)) ul[s] = new List<(long, bool d)>();
                ul[s].Add((X[i], true));
            }

            foreach (int i in left)
            {
                long s = X[i] - Y[i];
                if (!ul.TryGetValue(s, out _)) ul[s] = new List<(long, bool d)>();
                ul[s].Add((X[i], false));
            }

            // xでソート
            foreach (var pair in ul)
            {
                var ls = pair.Value;
                ls.Sort((l, r) => l.x.CompareTo(r.x));
                long lastUp = long.MinValue;
                foreach (var t in ls)
                {
                    if (t.d) lastUp = t.x;
                    else
                    {
                        if (lastUp != long.MinValue) Ans = Math.Min(Ans, (t.x - lastUp) * 10);
                        
#if DEBUG
                        if (Ans == -100)
                        {
                            Console.WriteLine("ul");
                        }
#endif
                    }
                }
            }
        }

        {
            // d or r
            var dr = new Dictionary<long, List<(long x, bool d)>>();
            foreach (int i in down)
            {
                long s = X[i] - Y[i];
                if (!dr.TryGetValue(s, out _)) dr[s] = new List<(long, bool d)>();
                dr[s].Add((X[i], true));
            }

            foreach (int i in right)
            {
                long s = X[i] - Y[i];
                if (!dr.TryGetValue(s, out _)) dr[s] = new List<(long, bool d)>();
                dr[s].Add((X[i], false));
            }

            // xでソート
            foreach (var pair in dr)
            {
                var ls = pair.Value;
                ls.Sort((l, r) => r.x.CompareTo(l.x));
                long lastDown = long.MaxValue;
                foreach (var t in ls)
                {
                    if (t.d) lastDown = t.x;
                    else
                    {
                        if (lastDown != long.MaxValue) Ans = Math.Min(Ans, (lastDown - t.x) * 10);
                        
#if DEBUG
                        if (Ans == -100)
                        {
                            Console.WriteLine("dr");
                        }
#endif
                    }
                }
            }
        }

        Console.WriteLine(Ans == long.MaxValue ? "SAFE" : Ans.ToString());
    }

    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            while (_index >= _line.Length)
            {
                _line = Console.ReadLine().Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            _line = Console.ReadLine().Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}