Language: C++

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
const double PI=3.14159265358979323846;
const in MOD = 1000000007;
const in INF=1e18+7;
const int inf=1e9+7;
using P=pair<int,int>;
vector<int> dx={0,1,-1,0};
vector<int> dy={1,0,0,-1};

using T=tuple<int,int,char>;
const int math=200001;

int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cout << fixed << setprecision(10);
    int n;
    cin>>n;
    vector<T> a(n);
    int x,y;
    char z;
    rep(i,n){
        cin>>x>>y>>z;
        a[i]=T(x,y,z);
    }
    vector<set<int>> U(math),D(math),R(math),L(math);
    using T2=tuple<int,int,int>;
    set<T2> uplus,umin,dplus,dmin;
    rep(i,n){
        tie(x,y,z)=a[i];
        if(z=='U') {U[x].insert(y);uplus.insert(T(x+y,x,y));umin.insert(T(x-y,x,y));}
        if(z=='D') {D[x].insert(y);dplus.insert(T(y+x,x,y));dmin.insert(T(x-y,x,y);}
        if(z=='R') R[y].insert(x);
        if(z=='L') L[y].insert(x);
    }

    int ans2=inf;
    rep(i,n){
        tie(x,y,z)=a[i];
        if(z=='U'||z=='D') continue;
        if(z=='R'){
            int pl=x+y,mi=x-y;
            auto itr=dmin.lower_bound(T(mi,0,0));
            if(itr!=dmin.end() && get<0>(*itr)==mi){
                int ans=get<1>(*itr)-x;
                if(ans>0)ans2=min(ans,ans2);
            }
            itr=dplus.lower_bound(T(pl,0,0));
            if(itr!=dmin.end() && get<0>(*itr)==pl){
                int ans=get<1>(*itr)-x;
                if(ans>0)                ans2=min(ans,ans2);
            }
        }
        if(z=='L'){
            int pl=x+y,mi=x-y;
            auto itr=dmin.lower_bound(T(mi,0,0));
            if(itr!=dmin.end() && get<0>(*itr)==mi){
                int ans=get<1>(*itr)-x;
                if(ans<0)ans2=min(-ans,ans2);
            }
            itr=dplus.lower_bound(T(pl,0,0));
            if(itr!=dmin.end() && get<0>(*itr)==pl){
                int ans=get<1>(*itr)-x;
                if(ans<0) ans2=min(-ans,ans2);
            }
        }
    }
    rep(i,n){
        tie(x,y,z)=a[i];
        if(z=='D' || z=='L') continue;
        if(z=='U'){
            if(D[x].upper_bound(y)==D[x].end()){
                continue;
            }
            int dist=*D[x].upper_bound(y);
            dist -=y;
            ans2 =min(ans2,dist);
        }
        if(z=='R'){
            if(L[y].upper_bound(x)==L[y].end()){
                continue;
            }
            int dist=*L[y].upper_bound(x);
            dist -=x;
            ans2 =min(ans2,dist);
        }
    }
    if(ans2==inf){
        cout<<"SAFE"<<endl;
        return 0;
    }
    ans2 *=5;
    cout<<ans2<<endl;
}/*
./problem.exe
*/

Compiler output:
a.cc: In function 'int main()':
a.cc:41:83: error: expected ')' before ';' token
   41 |         if(z=='D') {D[x].insert(y);dplus.insert(T(y+x,x,y));dmin.insert(T(x-y,x,y);}
      |                                                                        ~          ^
      |                                                                                   )