Language: C++

#pragma target("avx")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef unordered_map<ll, ll> U_MAP;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> rpq;
constexpr ll INF = 1e9, MOD = 1e9 + 7, ohara = 1e6 + 10;
constexpr ll LINF = 1e18;

#define rep(i, n) for (ll(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (ll i = (a); i < (b); i++)
#define rrrep(i, a, b) for (ll i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl
#define doublecout(a) cout << fixed << setprecision(15) << a << endl;
#define fi first
#define se second
#define m_p make_pair
#define p_b push_back
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto& x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

//------ Believe yourself as a genius!!!!!! ------

int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
// int dy[]={-1,0,1,-1,1,-1,0,1};int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"), s[2 * 100000 + 1];
ll n, cnt, ans, b, c, d, tmp, m, h, w, x[2 * 100000 + 1], y[2 * 100000 + 1],
    sum, k, q;
vector<int> dat1[4 * 100000 + 1];
pair<P, string> p[ohara], p2[ohara];
vector<int> sp1[2 * 100000 + 1];
vector<int> sp2[2 * 100000 + 1];

void solve() {
  rep(i, 4 * 100000 + 1) dat1[i].clear();
  rep(i, n) {
    if (s[i] == "U") dat1[x[i] + y[i]].p_b(x[i]);
  }
  // sort(dat1, dat1 + 4 * 100000);
  rep(i, n) {
    if (s[i] == "R") {
      ll nowx = x[i];
      ll le = 0, ri = Size(dat1[x[i] + y[i]]) - 1, mid;
      if (ri == -1) continue;
      while (ri - le > 1) {
        mid = (le + ri) / 2;
        if (dat1[x[i] + y[i]][mid] < nowx)
          le = mid;
        else
          ri = mid;
      }
      rrep(i, max(le - 20, 0), min(ri + 20, (ll)Size(dat1[x[i] + y[i]]))) {
        cnt = LINF / 10;
        if (dat1[x[i] + y[i]][i] - nowx >= 0) cnt = dat1[x[i] + y[i]][i] - nowx;
        if (dat1[x[i] + y[i]][i] - nowx >= 0)
          cnt = min(cnt, dat1[x[i] + y[i]][i] - nowx);
        ans = min(ans, cnt * 10);
      }
    }
  }
}

int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  ans = LINF;
  rep(i, n) {
    cin >> x[i] >> y[i] >> s[i];
    p[i] = m_p(m_p(x[i], y[i]), s[i]);
    p2[i] = m_p(m_p(y[i], x[i]), s[i]);
  }
  sort(p, p + n);
  sort(p2, p2 + n);
  rep(i, n) {
    x[i] = p2[i].fi.se;
    y[i] = p2[i].fi.fi;
    s[i] = p2[i].se;
  }
  rep(i, n) {
    if (s[i] == "U") sp1[x[i]].p_b(y[i]);
  }
  rep(i, n) {
    x[i] = p[i].fi.fi;
    y[i] = p[i].fi.se;
    s[i] = p[i].se;
  }
  rep(i, n) {
    if (s[i] == "R") sp2[y[i]].p_b(x[i]);
  }
  rep(i, n) {
    if (s[i] == "D") {
      ll le = 0, ri = Size(sp1[x[i]]) - 1, mid;
      if (ri == -1) continue;
      while (ri - le > 1) {
        mid = (le + ri) / 2;
        if (sp1[x[i]][mid] < y[i])
          le = mid;
        else
          ri = mid;
      }
      cnt = LINF;
      if ((y[i] - sp1[x[i]][le]) >= 0) cnt = (y[i] - sp1[x[i]][le]) * 10 / 2;
      if ((y[i] - sp1[x[i]][ri]) >= 0)
        cnt = min(cnt, (y[i] - sp1[x[i]][ri]) * 10 / 2);
      ans = min(ans, cnt);
    }
  }
  rep(i, n) {
    if (s[i] == "L") {
      ll le = 0, ri = Size(sp2[y[i]]) - 1, mid;
      if (ri == -1) continue;
      while (ri - le > 1) {
        mid = (le + ri) / 2;
        if (sp2[y[i]][mid] < x[i])
          le = mid;
        else
          ri = mid;
      }
      cnt = LINF;
      if ((x[i] - sp2[y[i]][le]) >= 0) cnt = (x[i] - sp2[y[i]][le]) * 10 / 2;
      if ((x[i] - sp2[y[i]][ri]) >= 0)
        cnt = min(cnt, (x[i] - sp2[y[i]][ri]) * 10 / 2);
      ans = min(ans, cnt);
    }
  }
  solve();
  rep(i, n) {
    x[i] = p[i].fi.se * -1 + 200000;
    y[i] = p[i].fi.fi;
    s[i] = p[i].se;
    if (s[i] == "R")
      s[i] = "U";
    else if (s[i] == "D")
      s[i] = "R";
    else
      s[i] = "D";
  }
  solve();
  rep(i, n) {
    x[i] = p[i].fi.fi * -1 + 200000;
    y[i] = p[i].fi.se * -1 + 200000;
    s[i] = p[i].se;
    if (s[i] == "L")
      s[i] = "R";
    else if (s[i] == "D")
      s[i] = "U";
    else
      s[i] = "D";
  }
  solve();
  rep(i, n) {
    x[i] = p[i].fi.se;
    y[i] = p[i].fi.fi * -1 + 200000;
    s[i] = p[i].se;
    if (s[i] == "U")
      s[i] = "R";
    else if (s[i] == "L")
      s[i] = "U";
    else
      s[i] = "D";
  }
  solve();
  if (ans == LINF)
    Cout("SAFE");
  else
    Cout(ans);
  return 0;
}


Compiler output:
a.cc: In function 'void solve()':
a.cc:87:18: error: no matching function for call to 'max(ll, int)'
   87 |       rrep(i, max(le - 20, 0), min(ri + 20, (ll)Size(dat1[x[i] + y[i]]))) {
      |               ~~~^~~~~~~~~~~~
a.cc:17:36: note: in definition of macro 'rrep'
   17 | #define rrep(i, a, b) for (ll i = (a); i < (b); i++)
      |                                    ^
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:87:18: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   87 |       rrep(i, max(le - 20, 0), min(ri + 20, (ll)Size(dat1[x[i] + y[i]]))) {
      |               ~~~^~~~~~~~~~~~
a.cc:17:36: note: in definition of macro 'rrep'
   17 | #define rrep(i, a, b) for (ll i = (a); i < (b); i++)
      |                                    ^
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:87:18: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   87 |       rrep(i, max(le - 20, 0), min(ri + 20, (ll)Size(dat1[x[i] + y[i]]))) {
      |               ~~~^~~~~~~~~~~~
a.cc:17:36: note: in definition of macro 'rrep'
   17 | #define rrep(i, a, b) for (ll i = (a); i < (b); i++)
      |                                    ^