Language: C++

#include <bits/stdc++.h>

#define rep(X,N) for(ll X = 0LL; X < (N); X++)
#define ALL(V) (V).begin(),(V).end()
#define endl "\n"

using namespace std;
typedef long long ll;

const double PI = 3.1415926535897932384626;
const ll MODN = 1000000007;
const ll MODN2 = 998244353;
const double EPS = 1e-10;

int main(){

    int n;
    cin >> n;

    vector<pair<int,int>> up;
    vector<pair<int,int>> down;
    vector<pair<int,int>> left;
    vector<pair<int,int>> right;

    rep(i, n){
        int x, y;
        char u;
        cin >> x >> y >> u;

        if(u == 'U'){
            up.push_back(make_pair(x, y));
        }else if(u == 'R'){
            right.push_back(make_pair(y, x));
        }else if(u == 'L'){
            left.push_back(make_pair(y, x));
        }else{
            down.push_back(make_pair(x, y));
        }
    }

    sort(ALL(up));
    sort(ALL(right));
    sort(ALL(left));
    sort(ALL(down));

    int ans = INT_MAX;

    int upi = 0;
    int downi = 0;

    while(upi < up.size() && downi < down.size()){

        if(up[upi].first < down[downi].first){
            upi++;
        }else if(up[upi].first > down[downi].first){
            downi++;
        }else{
            if(down[downi].second > up[upi].second){
                int tmp = (down[downi].second - up[upi].second) * 5;
                ans = min(ans, tmp);
            }

            if(upi + 1 < up.size() && up[upi].first == up[upi + 1].first){
                upi++;    
            }else if(downi + 1 < down.size() && down[downi].first == down[downi + 1].first){
                downi++;
            }else{
                if(upi + 1 < up.size()){
                    upi++;
                }else{
                    downi++;
                }
            }
            
        }
    }

    int lefti = 0;
    int righti = 0;

    while(righti < right.size() && lefti < left.size()){

        if(right[righti].first < left[lefti].first){
            righti++;
        }else if(right[righti].first > left[lefti].first){
            lefti++;
        }else{
            if(left[lefti].second > right[righti].second){
                int tmp = (left[lefti].second - right[righti].second) * 5;
                ans = min(ans, tmp);
            }

            if(righti + 1 < right.size() && right[righti].first == right[righti + 1].first){
                righti++;    
            }else if(lefti + 1 < left.size() && left[lefti].first == left[lefti + 1].first){
                lefti++;
            }else{
                if(righti + 1 < right.size()){
                    righti++;
                }else{
                    lefti++;
                }
            }
            
        }
    }

    vector<pair<int,pair<int,int>>> updown;
    vector<pair<int,pair<int,int>>> leftright;

    rep(i, up.size()){
        int y = up[i].second - up[i].first;
        updown.push_back(y, up[i]);
        y = up[i].second + up[i].first;
        updown.push_back(y, up[i]);
    }

    rep(i, down.size()){
        int y = down[i].second - down[i].first;
        updown.push_back(y, down[i]);
        y = down[i].second + down[i].first;
        updown.push_back(y, down[i]);
    }

    rep(i, left.size()){
        int y = left[i].first - left[i].second;
        leftright.push_back(make_pair(y, make_pair(left[i].second, left[i].first)));
        y = left[i].first + left[i].second;
        leftright.push_back(make_pair(y, make_pair(left[i].second, left[i].first)));
    }

    rep(i, right.size()){
        int y = right[i].first - right[i].second;
        leftright.push_back(make_pair(y, make_pair(right[i].second, right[i].first)));
        y = right[i].first + right[i].second;
        leftright.push_back(make_pair(y, make_pair(right[i].second, right[i].first)));
    }

    sort(ALL(updown));
    sort(ALL(leftright));

    int udi = 0;
    int lri = 0;

    while(udi < updown.size() && lri < leftright.size()){
        if(updown[udi].first < leftright[lri].first){
            udi++;
        }else if(updown[udi].first < leftright[lri].first){
            lri++;
        }else{

            pair<int,int> udp = updown[udi].second;
            pair<int,int> lrp = leftright[lri].second;
            
            int tmp = abs(udp.second - lrp.second) * 10;
            ans = min(ans, tmp);
            

            if(udi + 1 < updown.size() && updown[udi].first == updown[udi + 1].first){
                udi++;    
            }else if(lri + 1 < leftright.size() && leftright[lri].first == leftright[lri + 1].first){
                lri++;
            }else{
                if(upi + 1 < updown.size()){
                    udi++;
                }else{
                    lri++;
                }
            }
            
        }
    }


    if(ans == INT_MAX){
        cout << "SAFE" << endl;
    }else{
        cout << ans << endl;
    }
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:113:25: error: no matching function for call to 'std::vector<std::pair<int, std::pair<int, int> > >::push_back(int&, __gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type&)'
  113 |         updown.push_back(y, up[i]);
      |         ~~~~~~~~~~~~~~~~^~~~~~~~~~
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_vector.h:1283:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::pair<int, std::pair<int, int> >; _Alloc = std::allocator<std::pair<int, std::pair<int, int> > >; value_type = std::pair<int, std::pair<int, int> >]'
 1283 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_vector.h:1300:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = std::pair<int, std::pair<int, int> >; _Alloc = std::allocator<std::pair<int, std::pair<int, int> > >; value_type = std::pair<int, std::pair<int, int> >]'
 1300 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1300:7: note:   candidate expects 1 argument, 2 provided
a.cc:115:25: error: no matching function for call to 'std::vector<std::pair<int, std::pair<int, int> > >::push_back(int&, __gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type&)'
  115 |         updown.push_back(y, up[i]);
      |         ~~~~~~~~~~~~~~~~^~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::pair<int, std::pair<int, int> >; _Alloc = std::allocator<std::pair<int, std::pair<int, int> > >; value_type = std::pair<int, std::pair<int, int> >]'
 1283 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_vector.h:1300:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = std::pair<int, std::pair<int, int> >; _Alloc = std::allocator<std::pair<int, std::pair<int, int> > >; value_type = std::pair<int, std::pair<int, int> >]'
 1300 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1300:7: note:   candidate expects 1 argument, 2 provided
a.cc:120:25: error: no matching function for call to 'std::vector<std::pair<int, std::pair<int, int> > >::push_back(int&, __gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type&)'
  120 |         updown.push_back(y, down[i]);
      |         ~~~~~~~~~~~~~~~~^~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::pair<int, std::pair<int, int> >; _Alloc = std::allocator<std::pair<int, std::pair<int, int> > >; value_type = std::pair<int, std::pair<int, int> >]'
 1283 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_vector.h:1300:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = std::pair<int, std::pair<int, int> >; _Alloc = std::allocator<std::pair<int, std::pair<int, int> > >; value_type = std::pair<int, std::pair<int, int> >]'
 1300 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1300:7: note:   candidate expects 1 argument, 2 provided
a.cc:122:25: error: no matching function for call to 'std::vector<std::pair<int, std::pair<int, int> > >::push_back(int&, __gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type&)'
  122 |         updown.push_back(y, down[i]);
      |         ~~~~~~~~~~~~~~~~^~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::pair<int, std::pair<int, int> >; _Alloc = std::allocator<std::pair<int, std::pair<int, int> > >; value_type = std::pair<int, std::pair<int, int> >]'
 1283 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_vector.h:1300:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = std::pair<int, std::pair<int, int> >; _Alloc = std::allocator<std::pair<int, std::pair<int, int> > >; value_type = std::pair<int, std::pair<int, int> >]'
 1300 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1300:7: note:   candidate expects 1 argument, 2 provided