Language: C++

#include <bits/stdc++.h>
using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

constexpr double EPS = 1e-9;
constexpr double PI  = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

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

void Main(){
    int N; cin >> N;
    VI ux[200200],dx[200200],ly[200200],ry[200200];
    VI uxy1[400400],uxy2[400400],dxy1[400400],dxy2[400400],lxy1[400400],lxy2[400400],rxy1[400400],rxy2[400400];
    VI X(N),Y(N);
    char U[N];

    auto f1 = [&](int x, int y, int *n, int *t){
        if(x<=y){
            *n = 200000-(y-x);
            *t = x;
        }
        else{
            *n = 200000+(x-y);
            *t = y;
        }
    };
    auto f2 = [&](int x, int y, int *n, int *t){
        if(y>=-x+200000){
            *n = 200000-(y+x-200000);
            *t = 200000-x;
        }
        else{
            *n = 400000-(x+y);
            *t = y;
        }
    };

    REP(i,N){
        cin >> X[i] >> Y[i] >> U[i];
        int n1,t1,n2,t2; f1(X[i],Y[i],&n1,&t1); f2(X[i],Y[i],&n2,&t2);
        if(U[i]=='L'){
            ly[Y[i]].push_back(X[i]);
            lxy1[n1].push_back(t1);
            lxy2[n2].push_back(t2);
        }
        else if(U[i]=='R'){
            ry[Y[i]].push_back(X[i]);
            rxy1[n1].push_back(t1);
            rxy2[n2].push_back(t2);
        }
        else if(U[i]=='U'){
            ux[X[i]].push_back(Y[i]);
            uxy1[n1].push_back(t1);
            uxy2[n2].push_back(t2);
        }
        else if(U[i]=='D'){
            dx[X[i]].push_back(Y[i]);
            dxy1[n1].push_back(t1);
            dxy2[n2].push_back(t2);
        }
    }

    REP(i,200001){
        if(ux[i].size()) SORT(ux[i]);
        if(dx[i].size()) SORT(dx[i]);
        if(ly[i].size()) SORT(ly[i]);
        if(ry[i].size()) SORT(ry[i]);
    }
    REP(i,400001){
        if(lxy1[i].size()) SORT(lxy1[i]); if(lxy2[i].size()) SORT(lxy2[i]);
        if(rxy1[i].size()) SORT(rxy1[i]); if(rxy2[i].size()) SORT(rxy2[i]);
        if(uxy1[i].size()) SORT(uxy1[i]); if(uxy2[i].size()) SORT(uxy2[i]);
        if(dxy1[i].size()) SORT(dxy1[i]); if(dxy2[i].size()) SORT(dxy2[i]);
    }

    int ans = INF;
    REP(i,N){
        int n1,t1,n2,t2; f1(X[i],Y[i],&n1,&t1); f2(X[i],Y[i],&n2,&t2);
        if(U[i]=='L'){
            int l=-1,r=ry[Y[i]].size();
            while(r-l>1){
                int x = (l+r)/2;
                if(ry[Y[i]][x]<X[i]) l = x;
                else r = x;
            }
            int t = l;
            if(t>-1) chmin(ans, (X[i]-ry[Y[i]][t-1])*5);
            t = LB(uxy1[n1],t1);
            if(t>0) chmin(ans, (t1-uxy1[n1][t-1])*10);
            t = LB(dxy2[n2],t2+1);
            if(t<dxy2[n2].size()) chmin(ans, (dxy2[n2][t]-t2)*10);
        }
        else if(U[i]=='R'){
            int t = LB(uxy2[n2],t2);
            if(t>0) chmin(ans, (t2-uxy2[n2][t-1])*10);
            t = LB(dxy1[n1],t1+1);
            if(t<dxy1[n1].size()) chmin(ans, (dxy1[n1][t]-t1)*10);
        }
        else if(U[i]=='U'){
            int t = LB(dx[X[i]],Y[i]);
            if(t<dx[X[i]].size()) chmin(ans, (dx[X[i]][t]-Y[i])*5);
    }

    if(ans<INF) cout << ans << en;
    else cout << "SAFE" << en;
    return;
}

int main(void){
    cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
    int t=1; //cin>>t;
    REP(_,t) Main();
    return 0;
}

Compiler output:
a.cc: In function 'void Main()':
a.cc:141:15: error: a function-definition is not allowed here before '{' token
  141 | int main(void){
      |               ^
a.cc:146:2: error: expected '}' at end of input
  146 | }
      |  ^
a.cc:42:12: note: to match this '{'
   42 | void Main(){
      |            ^