Language: C++

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
int main() {
    
    ll n, x[200001], y[200001], i, j, k, min=200000000000, imsi1[200001], imsi2[200001], a, b, begin, end, mid;
    vector<ll> up[200001], down[200001], left[200001], right[200001];
    vector<ll> uplus[400001], dplus[400001], lplus[400001], rplus[400001];
    vector<ll> u[400001], d[400001], l[400001], r[400001];
    char u[200001];
    cin>>n;
    for(i=0;i<=200000;i++)
    {
        up[i].clear();
        down[i].clear();
        left[i].clear();
        right[i].clear();
    }
    for(i=0;i<=400000;i++)
    {
        uplus[i].clear();
        lplus[i].clear();
        rplus[i].clear();
        dplus[i].clear();
        u[i].clear();
        l[i].clear();
        r[i].clear();
        d[i].clear();
    }
    for(i=1;i<=n;i++)
    {
        cin>>x[i]>>y[i];
        scanf(" %c ", &u[i]);
        if(u[i]=='U'){
        up[x[i]].push_back(y[i]);
        uplus[x[i]+y[i]].push_back(x[i]);
        u[y[i]-x[i]+200000].push_back(x[i]);
        }
        else if(u[i]=='D'){
        down[x[i]].push_back(y[i]);
        dplus[x[i]+y[i]].push_back(x[i]);
        d[y[i]-x[i]+200000].push_back(x[i]);
        
        }
        else if(u[i]=='L'){
        left[y[i]].push_back(x[i]);
        lplus[x[i]+y[i]].push_back(x[i]);
        l[y[i]-x[i]+200000].push_back(x[i]);
        
        }
        else{
        right[y[i]].push_back(x[i]);
        rplus[x[i]+y[i]].push_back(x[i]);
        r[y[i]-x[i]+200000].push_back(x[i]);
        
        }
        
    }
    for(i=0;i<=200000;i++){
    sort(up[i].begin(), up[i].end());
    sort(down[i].begin(), down[i].end());
    sort(left[i].begin(), left[i].end());
    sort(right[i].begin(), right[i].end());
    }
    for(i=0;i<=200000;i++)
    {
        for(j=0;j<up[i].size();j++)
        {
            begin=0;
            end=down[i].size();
            while(begin!=end)
            {
                mid=(begin+end)/2;
                
                if(down[i][mid]>=up[i][j])
                end=mid;
                else
                begin=mid+1;
            }
            if(begin<down[i].size())
            {
                min=min<5*(down[i][begin]-up[i][j])?min:5*(down[i][begin]-up[i][j]);
            }
        }
    }
    
    for(i=0;i<=200000;i++)
    {
        for(j=0;j<right[i].size();j++)
        {
            begin=0;
            end=left[i].size();
            while(begin!=end)
            {
                mid=(begin+end)/2;
                
                if(left[i][mid]>=right[i][j])
                end=mid;
                else
                begin=mid+1;
            }
            if(begin<left[i].size())
            {
                min=min<5*(left[i][begin]-right[i][j])?min:5*(left[i][begin]-right[i][j]);
            }
        }
    }
    
    
    for(i=0;i<=400000;i++)
    {
        sort(uplus[i].begin(), uplus[i].end());
        sort(dplus[i].begin(), dplus[i].end());
        sort(lplus[i].begin(), lplus[i].end());
        sort(rplus[i].begin(), rplus[i].end());   
    }
    
    
       for(i=0;i<=400000;i++)
    {
        for(j=0;j<rplus[i].size();j++)
        {
            begin=0;
            end=uplus[i].size();
            while(begin!=end)
            {
                mid=(begin+end)/2;
                
                if(uplus[i][mid]>=rplus[i][j])
                end=mid;
                else
                begin=mid+1;
            }
            if(begin<uplus[i].size())
            {
                min=min<10*(uplus[i][begin]-rplus[i][j])?min:10*(uplus[i][begin]-rplus[i][j]);
            }
        }
    } 
    
    
    for(i=0;i<=400000;i++)
    {
        for(j=0;j<dplus[i].size();j++)
        {
            begin=0;
            end=lplus[i].size();
            while(begin!=end)
            {
                mid=(begin+end)/2;
                
                if(lplus[i][mid]>=dplus[i][j])
                end=mid;
                else
                begin=mid+1;
            }
            if(begin<lplus[i].size())
            {
                min=min<10*(lplus[i][begin]-dplus[i][j])?min:10*(lplus[i][begin]-dplus[i][j]);
            }
        }
    } 
    
    for(i=0;i<=400000;i++)
    {
        for(j=0;j<u[i].size();j++)
        {
            begin=0;
            end=l[i].size();
            while(begin!=end)
            {
                mid=(begin+end)/2;
                
                if(l[i][mid]>=u[i][j])
                end=mid;
                else
                begin=mid+1;
            }
            if(begin<l[i].size())
            {
                min=min<10*(l[i][begin]-u[i][j])?min:10*(l[i][begin]-u[i][j]);
            }
        }
    } 
    
    for(i=0;i<=400000;i++)
    {
        for(j=0;j<r[i].size();j++)
        {
            begin=0;
            end=d[i].size();
            while(begin!=end)
            {
                mid=(begin+end)/2;
                
                if(d[i][mid]>=r[i][j])
                end=mid;
                else
                begin=mid+1;
            }
            if(begin<d[i].size())
            {
                min=min<10*(d[i][begin]-r[i][j])?min:10*(d[i][begin]-r[i][j]);
            }
        }
    } 
    
    
    
    
    if(min==200000000000)
    printf("SAFE");
    else
    cout<<min;
    
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:11:10: error: conflicting declaration 'char u [200001]'
   11 |     char u[200001];
      |          ^
a.cc:10:16: note: previous declaration as 'std::vector<long long int> u [400001]'
   10 |     vector<ll> u[400001], d[400001], l[400001], r[400001];
      |                ^
a.cc:35:16: error: no match for 'operator==' (operand types are 'std::vector<long long int>' and 'char')
   35 |         if(u[i]=='U'){
      |            ~~~~^~~~~
      |               |  |
      |               |  char
      |               std::vector<long long int>
In file included from /usr/include/c++/14/regex:68,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:181,
                 from a.cc:1:
/usr/include/c++/14/bits/regex.h:1103:5: note: candidate: 'template<class _BiIter> bool std::__cxx11::operator==(const sub_match<_BiIter>&, const sub_match<_BiIter>&)'
 1103 |     operator==(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1103:5: note:   template argument deduction/substitution failed:
a.cc:35:18: note:   'std::vector<long long int>' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
   35 |         if(u[i]=='U'){
      |                  ^~~
/usr/include/c++/14/bits/regex.h:1199:5: note: candidate: 'template<class _Bi_iter, class _Ch_traits, class _Ch_alloc> bool std::__cxx11::operator==(__sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>&, const sub_match<_BiIter>&)'
 1199 |     operator==(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1199:5: note:   template argument deduction/substitution failed:
a.cc:35:18: note:   'std::vector<long long int>' is not derived from 'std::__cxx11::__sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>'
   35 |         if(u[i]=='U'){
      |                  ^~~
/usr/include/c++/14/bits/regex.h:1274:5: note: candidate: 'template<class _Bi_iter, class _Ch_traits, class _Ch_alloc> bool std::__cxx11::operator==(const sub_match<_BiIter>&, __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>&)'
 1274 |     operator==(const sub_match<_Bi_iter>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1274:5: note:   template argument deduction/substitution failed:
a.cc:35:18: note:   'std::vector<long long int>' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
   35 |         if(u[i]=='U'){
      |                  ^~~
/usr/include/c++/14/bits/regex.h:1366:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator==(const typename std::iterator_traits<_Iter>::value_type*, const sub_match<_BiIter>&)'
 1366 |     operator==(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1366:5: note:   template argument deduction/substitution failed:
a.cc:35:18: note:   mismatched types 'const std::__cxx11::sub_match<_BiIter>' and 'char'
   35 |         if(u[i]=='U'){
      |                  ^~~
/usr/include/c++/14/bits/regex.h:1441:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator==(const sub_match<_BiIter>&, const typename std::iterator_traits<_Iter>::value_type*)'
 1441 |     operator==(const sub_match<_Bi_iter>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1441:5: note:   template argument deduction/substitution failed:
a.cc:35:18: note:   'std::vector<long long int>' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
   35 |         if(u[i]=='U'){
      |                  ^~~
/usr/include/c++/14/bits/regex.h:1534:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator==(const typename std::iterator_traits<_Iter>::value_type&, const sub_match<_BiIter>&)'
 1534 |     operator==(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1534:5: note:   template argument deduction/substitution failed:
a.cc:35:18: note:   mismatched types 'const std::__cxx11::sub_match<_BiIter>' and 'char'
   35 |         if(u[i]=='U'){
      |                  ^~~
/usr/include/c++/14/bits/regex.h:1613:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator==(const sub_match<_BiIter>&, const typename std::iterator_traits<_Iter>::value_type&)'
 1613 |     operator==(const sub_match<_Bi_iter>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1613:5: note:   template argument deduction/substitution failed:
a.cc:35:18: note:   'std::vector<long long int>' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
   35 |         if(u[i]=='U'){
      |                  ^~~
/usr/include/c++/14/bits/regex.h:2186:5: note: candidate: 'template<class _Bi_iter, class _Alloc> bool std::__cxx11::operator==(const match_results<_BiIter, _Alloc>&, const match_results<_BiIter, _Alloc>&)'
 2186 |     operator==(const match_results<_Bi_iter, _Alloc>& __m1,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:2186:5: note:   template argument deduction/substitution failed:
a.cc:35:18: note:   'std::vector<long long int>' is not derived from 'const std::__cxx11::match_results<_BiIter, _Alloc>'
   35 |         if(u[i]=='U'){
      |                  ^~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51:
/usr/include/c++/14/bits/stl_pair.h:1033:5: note: candidate: 'template<class _T1, class _T2> constexpr bool std::operator==(const pair<_T1, _T2>&, const pair<_T1, _T2>&)'
 1033 |     operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:1033:5: note:   template argument deduction/substitution failed:
a.cc:35:18: note:   'std::vector<long long int>' is not derived from 'const std::pair<_T1, _T2>'
   35 |         if(u[i]=='U'){
      |                  ^~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:67:
/usr/include/c++/14/bits/stl_iterator.h:441:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator==(const reverse_iterator<_Iterator>&, const reverse_iterator<_Iterator>&)'
  441 |     operator==(const reverse_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:441:5: note:   template argument deduction/substitution failed:
a.cc:35:18: note:   'std::vector<long long int>' is not derived from 'const std::reverse_iterator<_Iterator>'
   35 |         if(u[i]=='U'){
      |                  ^~~
/usr/include/c++/14/bits/stl_iterator.h:486:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator==(const reverse_iterator<_Iterator>&, const reverse_iterator<_IteratorR>&)'
  486 |     operator==(const reverse_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:486:5: note:   template argument deduction/substitution failed:
a.cc:35:18: note:   'std::vector<long long int>' is not derived from 'const std::reverse_iterator<_Iterator>'
   35 |         if(u[i]=='U'){
      |                  ^~~
/usr/include/c++/14/bits/stl_iterator.h:1667:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator==(const move_iterator<_IteratorL>&, const move_iterator<_IteratorR>&)'
 1667 |     operator==(const move_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1667:5: note:   template argument deduction/substitution failed:
a.cc:35:18: note:   'std::vector<long long int>' is not derived from 'const std::move_iterator<_IteratorL>'
   35 |         if(u[i]=='U'){
      |                  ^~~
/usr/include/c++/14/bits/stl_iterator.h:1737:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator==(const move_iterator<_IteratorL>&, const move_iterator<_IteratorL>&)'
 1737 |     operator==(const move_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1737:5: note:   template argument deduction/substitution failed:
a.cc:35:18: note:   'std::vector<long long int>' is not derived from 'const std::move_iterator<_IteratorL>'
   35 |         if(u[i]=='U'){
      |                  ^~~
In file included from /usr/include/c++/14/bits/char_traits.h:42,
                 from /usr/include/c++/14/string:42,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52:
/usr/include/c++/14/bits/postypes.h:192:5: note: candidate: 'template<class _StateT> bool std::operator==(const fpos<_StateT>&, const fpos<_StateT>&)'
  192 |     operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
      |     ^~~~~~~~
/usr/include/c++/14/bits/postypes.h:192:5: note:   template argument deduction/substitution failed:
a.cc:35:18: note:   'std::vector<long long int>' is not derived from 'const std::fpos<_StateT>'
   35 |         if(u[i]=='U'){
      |                  ^~~
In file included from /usr/include/c++/14/string:43:
/usr/include/c++/14/bits/allocator.h:235:5: note: candidate: 'template<class _T1, class _T2> bool std::operator==(const allocator<_CharT>&, const allocator<_T2>&)'
  235 |     operator==(const allocator<_T1>&, const allocator<_T2>&)
      |     ^~~~~~~~
/usr/include/c++/14/bits/allocator.h:235:5: note:   template argument deduction/substitution failed:
a.cc:35:18: note:   'std::vector<long long int>' is not derived from 'const std::allocator<_CharT>'
   35 |         if(u[i]=='U'){
      |                  ^~~
In file included from /usr/include/c++/14/bits/basic_string.h:47,
                 from /usr/include/c++/14/string:54:
/usr/include/c++/14/string_view:629:5: note: candidate: 'template<class _CharT, class _Traits> constexpr bool std::operator==(basic_string_view<_CharT, _Traits>, __type_identity_t<basic_string_view<_CharT, _Traits> >)'
  629 |     operator==(basic_string_view<_CharT, _Traits> __x,
      |     ^~~~~~~~
/usr/include/c++/14/string_view:629:5: note:   template argument deduction/substitution failed:
a.cc:35:18: note:   'std::vector<long long int>' is not derived from 'std::basic_string_view<_Cha