Language: C++

#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <string>
#include <map>
#include <set>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n) - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(a) (a).begin(), (a).end()

const ll INF = 1e18;
ll N;
ll X[200000], Y[200000];
char U[200000];

int get_min(char first, char second, ll sign) {
    map<ll, multiset<ll>> m;
    REP(i, N) {
        if (U[i] != first) continue;
        ll line = Y[i] + sign * X[i];
        if (m.count(line) == 0) {
            multiset<ll> set {Y[i]};
            m[line] = set;
        } else {
            m[line].insert(Y[i]);
        }
    }

    ll ret = INF;
    REP(i, N) {
        if (U[i] != second) continue;
        ll line = Y[i] + sign * X[i];
        if (m.count(line) == 0) continue;
        auto it = m[line].lower_bound(Y[i]);
        if (it != m[line].begin()) {
            it--;
            ret = min(ret, Y[i] - *it);
        }
    }
    return ret;
}

int get_min_same_line() {
    ll ret = INF;
    map<ll, multiset<ll>> m;
    REP(i, N) {
        if (U[i] != 'U') continue;
        if (m.count(X[i]) == 0) {
            multiset<ll> set {Y[i]};
            m[X[i]] = set;
        } else {
            m[X[i]].insert(Y[i]);
        }
    }
    REP(i, N) {
        if (U[i] != 'D' || m.count(X[i]) == 0) continue;
        auto it = m[X[i]].lower_bound(Y[i]);
        if (it != m[X[i]].begin()) {
            it--;
            ret = min(ret, (Y[i] - *it) / 2);
        }
    }

    m.clear();
    REP(i, N) {
        if (U[i] != 'R') continue;
        if (m.count(Y[i]) == 0) {
            multiset<ll> set {X[i]};
            m[Y[i]] = set;
        } else {
            m[Y[i]].insert(X[i]);
        }
    }
    REP(i, N) {
        if (U[i] != 'L' || m.count(Y[i]) == 0) continue;
        auto it = m[Y[i]].lower_bound(X[i]);
        if (it != m[Y[i]].begin()) {
            it--;
            ret = min(ret, (X[i] - *it) / 2);
        }
    }
    return ret;
}

int main() {
    cin >> N;
    REP(i, N) cin >> X[i] >> Y[i] >> U[i];
    ll ans = INF;
    ans = min(ans, get_min('R', 'D', -1));
    ans = min(ans, get_min('U', 'L', -1));
    ans = min(ans, get_min('L', 'D', 1));
    ans = min(ans, get_min('U', 'R', 1));
    ans = min(ans, get_min_same_line());
    if (ans == INF) cout << "SAFE" << endl;
    else cout << ans * 10 << endl;
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:97:14: error: no matching function for call to 'min(ll&, int)'
   97 |     ans = min(ans, get_min('R', 'D', -1));
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:97:14: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   97 |     ans = min(ans, get_min('R', 'D', -1));
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:97:14: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   97 |     ans = min(ans, get_min('R', 'D', -1));
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:98:14: error: no matching function for call to 'min(ll&, int)'
   98 |     ans = min(ans, get_min('U', 'L', -1));
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:98:14: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   98 |     ans = min(ans, get_min('U', 'L', -1));
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:98:14: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   98 |     ans = min(ans, get_min('U', 'L', -1));
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:99:14: error: no matching function for call to 'min(ll&, int)'
   99 |     ans = min(ans, get_min('L', 'D', 1));
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:99:14: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   99 |     ans = min(ans, get_min('L', 'D', 1));
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:99:14: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   99 |     ans = min(ans, get_min('L', 'D', 1));
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:100:14: error: no matching function for call to 'min(ll&, int)'
  100 |     ans = min(ans, get_min('U', 'R', 1));
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:100:14: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
  100 |     ans = min(ans, get_min('U', 'R', 1));
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:100:14: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  100 |     ans = min(ans, get_min('U', 'R', 1));
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:101:14: error: no matching function for call to 'min(ll&, int)'
  101 |     ans = min(ans, get_min_same_line());
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:101:14: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
  101 |     ans = min(ans, get_min_same_line());
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:101:14: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  101 |     ans = min(ans, get_min_same_line());
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~