Language: C++

#include <stack>
#include <queue>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <set>
#include <tuple>
#include <cmath>
#include <random>
#include <algorithm>
#include <vector>
#include <iostream>
#include <fstream>
#include <chrono>

#define fio ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr)

#define ri(x) i64 x; cin >> x
#define rii(x, y) i64 x, y; cin >> x >> y
#define ri3(x, y, z) i64 x, y, z; cin >> x >> y >> z
#define ri4(x, y, z, u) i64 x, y, z, u; cin >> x >> y >> z >> u
#define rvi(x, n) vi x(n); cin >> x
#define rvvi(x, n, m) vvi x(n, vi(m)); cin >> x
#define rx(x, t) t x; cin >> x

#define ws(x) cout << x << ' '
#define wl(x) cout << x << '\n'
#define wln cout << '\n'

#define dbg(x) cerr << '[' << #x << "]: " << x << '\n'

#define fa(x, v) for (auto &x : v)
#define fi(i, n) for (i32 i = 0; i < n; i++)
#define fp(i, n) for (i32 i = 1; i < n; i++)
#define fr(i, n) for (i32 i = n - 1; i >= 0; i--)
#define fu(x) ri(_x); fi(x, _x)

#define l first
#define r second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define bit(x) ((u64) 1 << (u32) (x))

using namespace std;

typedef uint32_t u32;
typedef uint64_t u64;
typedef int32_t i32;
typedef int64_t i64;
typedef vector<i64> vi;
typedef vector<vi> vvi;
typedef pair<i64, i64> pi;

template<typename T>
inline istream &operator>>(istream &in, vector<T> &v) {
    fa(x, v) in >> x;
    return in;
}

template<typename T>
inline ostream &operator<<(ostream &out, const vector<T> &v) {
    fa(x, v) ws(x);
    wln;
    return out;
}


int main() {
    fio;

    typedef tuple<i64, i64, char> Q;
    vector<Q> prs;
    unordered_map<char, unordered_map<i64, vi>> diags;
    unordered_map<char, unordered_map<i64, vi>> rdiags;
    unordered_map<char, unordered_map<i64, vi>> xaxis;
    unordered_map<char, unordered_map<i64, vi>> yaxis;

    ri(n);
    fi(t, n) {
        rii(x, y);
        rx(u, char);
        prs.emplace_back(x, y, u);
        xaxis[u][x].push_back(y);
        yaxis[u][y].push_back(x);
        if (u == 'R' || u == 'L') {
            diags[u][x + y].push_back(y - x);
            rdiags[u][y - x].push_back(x + y);
        }
    }
    fa(x, diags) fa(y, x.second) sort(all(y.second));
    fa(x, rdiags) fa(y, x.second) sort(all(y.second));

    i64 best = INT_MAX;

    fi(t, n) {
        i64 x = get<0>(prs[t]);
        i64 y = get<1>(prs[t]);
        char u = get<2>(prs[t]);

        if (u == 'U') {
            auto axis = xaxis['D'][x];
            auto k = lower_bound(all(axis), y);
            if (k != axis.end()) {
                best = min(best, 5 * abs(*k - y));
            }

            auto diag = diags['R'][x + y];
            auto i = lower_bound(all(diag), y - x);
            if (i != diag.end()) {
                best = min(best, 5 * abs(*i - (y - x)));
            }

            auto rdiag = rdiags['L'][y - x];
            auto j = lower_bound(all(rdiag), x + y);
            if (j != rdiag.end()) {
                best = min(best, 5 * abs(*j - (x + y)));
            }
        }
        if (u == 'D') {
            auto axis = xaxis['U'][x];
            auto k = lower_bound(rall(axis), y, greater<i64>());
            if (k != axis.rend()) {
                best = min(best, 5 * abs(*k - y));
            }

            auto diag = diags['L'][x + y];
            auto i = lower_bound(rall(diag), y - x, greater<i64>());
            if (i != diag.rend()) {
                best = min(best, 5 * abs(*i - (y - x)));
            }

            auto rdiag = rdiags['R'][y - x];
            auto j = lower_bound(rall(rdiag), x + y, greater<i64>());
            if (j != rdiag.rend()) {
                best = min(best, 5 * abs(*j - (x + y)));
            }
        }
    }

    if (best == INT_MAX) wl("SAFE"); else wl(best);

    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:94:16: error: 'INT_MAX' was not declared in this scope
   94 |     i64 best = INT_MAX;
      |                ^~~~~~~
a.cc:16:1: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
   15 | #include <chrono>
  +++ |+#include <climits>
   16 |