Language: C++

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define loop(i, r, n) for (int i = (r); i < (n); i++)
#define pb push_back
#define all(in) in.begin(),in.end()

template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

using ll = long long;
using ull = unsigned long long;
using namespace std;

const ll INF = 100100100100100100;

int main(){
    ll n; cin >> n;
    vector<pair<ll,ll>> u, r, d, l;
    rep(i,n){
        ll a, b; char c;
        cin >> a >> b >> c;
        if(c == 'u') u.pb(make_pair(a,b));
        if(c == 'r') r.pb(make_pair(a,b));
        if(c == 'd') d.pb(make_pair(a,b));
        if(c == 'l') l.pb(make_pair(a,b));
    }

    ll ans = INF;

    ll save1 = INF;
    vector<vector<ll>> mp1(400400,vector<ll>());
    rep(i,(int)r.size()) mp1[r[i].first+r[i].second].pb(r[i].first);
    rep(i,400400) sort(all(mp1[i]));
    rep(i,(int)u.size()){
        if(! mp1[u[i].first+u[i].second].size()) continue;
        int k = lower_bound(all(mp1[u[i].first+u[i].second]),u[i].first) - mp1[u[i].first+u[i].second].begin();
        if(k > 0) chmin(save1, u[i].first - mp1[u[i].first+u[i].second][k-1]);
        if(k < mp1[u[i].first+u[i].second].size()) chmin(save1, mp1[u[i].first+u[i].second][k] - u[i].first);
    }
    chmin(ans, save1 * 10);

    ll save2 = INF;
    vector<vector<ll>> mp2(400400,vector<ll>());
    rep(i,(int)r.size()) mp2[r[i].first-r[i].second].pb(r[i].first);
    rep(i,400400) sort(all(mp2[i]));
    rep(i,(int)d.size()){
        if(! mp2[d[i].first-d[i].second].size()) continue;
        int k = lower_bound(all(mp2[d[i].first-d[i].second]),d[i].first) - mp2[d[i].first-d[i].second].begin();
        if(k > 0) chmin(save2, d[i].first - mp2[d[i].first-d[i].second][k-1]);
        if(k < mp2[r[i].first-r[i].second].size()) chmin(save2, mp2[d[i].first-d[i].second][k] - d[i].first);
    }
    chmin(ans, save2 * 10);

    ll save3 = INF;
    vector<vector<ll>> mp3(400400,vector<ll>());
    rep(i,(int)l.size()) mp3[l[i].first-l[i].second].pb(l[i].first);
    rep(i,400400) sort(all(mp3[i]));
    rep(i,(int)u.size()){
        if(! mp3[u[i].first-u[i].second].size()) continue;
        int k = lower_bound(all(mp3[u[i].first-u[i].second]),u[i].first) - mp3[u[i].first-u[i].second].begin();
        if(k > 0) chmin(save3, u[i].first - mp3[u[i].first-u[i].second][k-1]);
        if(k < mp3[l[i].first-l[i].second].size()) chmin(save3, mp3[u[i].first-u[i].second][k] - u[i].first);
    }
    chmin(ans, save1 * 10);

    ll save4 = INF;
    vector<vector<ll>> mp4(400400,vector<ll>());
    rep(i,(int)l.size()) mp4[l[i].first+l[i].second].pb(l[i].first);
    rep(i,400400) sort(all(mp4[i]));
    rep(i,(int)d.size()){
        if(! mp4[d[i].first+d[i].second].size()) continue;
        int k = lower_bound(all(mp4[d[i].first+d[i].second]),d[i].first) - mp4[d[i].first+d[i].second].begin();
        if(k > 0) chmin(save4, d[i].first - mp4[d[i].first+d[i].second][k-1]);
        if(k < mp4[l[i].first+l[i].second].size()) chmin(save4, mp4[d[i].first+d[i].second][k] - d[i].first);
    }
    chmin(ans, save2 * 10);

    cout << (ans == INF ? "SAFE" : ans) << endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:79:25: error: operands to '?:' have different types 'const char*' and 'll' {aka 'long long int'}
   79 |     cout << (ans == INF ? "SAFE" : ans) << endl;
      |              ~~~~~~~~~~~^~~~~~~~~~~~~~