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++ (GCC 9.2.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using Int = long long;  // clang-format off
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(v) std::begin(v), std::end(v)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif  // clang-format on

/**
 *    author:  knshnb
 *    created: Sat Jul 25 21:37:54 JST 2020
 **/

template <class T> bool chmin(T& a, const T& b) { return a > b ? a = b, true : false; }
template <class T> bool chmax(T& a, const T& b) { return a < b ? a = b, true : false; }

const Int INF = 1e9;
const Int K = 200002;
const std::string s = "URDL";

signed main() {
    Int n;
    std::cin >> n;
    std::vector<Int> x(n), y(n), u(n);
    REP(i, n) {
        char c;
        std::cin >> x[i] >> y[i] >> c;
        u[i] = std::find(ALL(s), c) - s.begin();
    }
    auto solve = [&]() {
        Int ret = INF;
        std::vector<std::vector<int>> ids(K);
        REP(i, n) { ids[x[i]].push_back(i); }
        REP(k, K) {
            std::sort(ALL(ids[k]), [&](Int i, Int j) { return y[i] < y[j]; });
            Int ma = -1;
            for (Int i : ids[k]) {
                if (u[i] == 0) chmax(ma, y[i]);
                if (u[i] == 2 && ma != -1) chmin(ret, (y[i] - ma) * 5);
            }
        }
        std::unordered_map<Int, std::vector<Int>> mp;
        REP(i, n) {
            if (u[i] == 0) mp[x[i] + y[i]].push_back(x[i]);
        }
        for (auto& p : mp) std::sort(ALL(p.second));
        REP(i, n) {
            if (u[i] == 1) {
                auto it = std::lower_bound(ALL(mp[x[i] + y[i]]), x[i]);
                if (it != mp[x[i] + y[i]].end()) chmin(ret, (*it - x[i]) * 10);
            }
        }
        return ret;
    };
    Int ans = INF;
    auto rotate = [&]() {
        REP(i, n) {
            Int a = x[i], b = y[i];
            x[i] = b, y[i] = K - 1 - a;
            u[i] = (u[i] + 1) % 4;
        }
    };
    auto rev = [&]() {
        REP(i, n) {
            x[i] = K - 1 - x[i];
            if (u[i] % 2) u[i] = (u[i] + 2) % 4;
        }
    };
    REP(p, 4) {
        REP(q, 2) {
            chmin(ans, solve());
            rev();
        }
        rotate();
    }
    if (ans == INF) {
        std::cout << "SAFE\n";
    } else {
        std::cout << ans << "\n";
    }
}