Language: C++

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, n) for(int i = 0; i < (int) n; i++)
#define PB push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define F first
#define S second

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;

const int MOD = 1e9+7;

struct data{
	int x, y;
	char o;

	data(int a, int b, char u){
		x = a; y = b; o = u;
	}

	void printa(){
		cout << x << ' ' << y << ' ' << o << '\n';
	}

};

int main(){
	ios::sync_with_stdio(false); cin.tie(0);

	int n; cin >> n;

	map<int, vector<pii>> y, x;
	map<int, vector<data>> dx, dy;

	FOR(i, n){
		int a, b; char u; 
		cin >> a >> b >> u;
		if(u == 'R' || u == 'L')
			dx[a+b].PB(data(a, b, u));
		else
			dy[a+b].PB(data(a, b, u));
		if(u == 'U')
			y[a].PB({b, 1});
		else if(u == 'D')
			y[a].PB({b, -1});
		else if(u == 'R')
			x[b].PB({a, 1});
		else
			x[b].PB({a, -1});
	}

	int time = 1e9;

	for(auto p : x){
		vector<pii> v = p.S;
		sort(all(v));
		int last = -1;
		/*cout << "x = " << p.F << " : ";
		FOR(i, v.size()) 
			cout << "{" << v[i].F << ", " << v[i].S << "} ";
		cout << '\n';*/
		FOR(i, v.size()){
			if(last == -1 && v[i].S == 1)
				last = v[i].F;
			else if(v[i].S == 1)
				last = v[i].F;

			if(v[i].S == -1 && last != -1)
				time = min(time, v[i].F-last);
		}
	}

	for(auto p : y){
		vector<pii> v = p.S;
		sort(all(v));
		int last = -1;
		/*cout << "y = " << p.F << " : ";
		FOR(i, v.size()) 
			cout << "{" << v[i].F << ", " << v[i].S << "} ";
		cout << '\n';*/
		FOR(i, v.size()){
			if(last == -1 && v[i].S == 1)
				last = v[i].F;
			else if(v[i].S == 1)
				last = v[i].F;

			if(v[i].S == -1 && last != -1)
				time = min(time, (v[i].F-last)*5);
		}
	}

	for(auto p : dy){
		if(!dx.count(p.F)) continue;
		vector<data> v1 = p.S, v2 = dx[p.F];

		FOR(i, v1.size()){
			int xx = v1[i].x, yy = v1[i].y;
			FOR(j, v2.size()){
				if(v2[j].o == 'R' && v2[j].x < xx){
					time = min(time, abs(v2[j].x-xx)*10);
				}else if(v2[j].o == 'L' && v2[j].x > xx){
					time = min(time, abs(v2[j].x-xx)*10);
				}
			}
		}
	}

	if(time == 1e9) cout << "SAFE\n";
	else cout << time << '\n';

	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:38:25: error: template argument 1 is invalid
   38 |         map<int, vector<data>> dx, dy;
      |                         ^~~~
a.cc:38:25: error: template argument 2 is invalid
a.cc:38:29: error: template argument 2 is invalid
   38 |         map<int, vector<data>> dx, dy;
      |                             ^~
a.cc:38:29: error: template argument 4 is invalid
a.cc:44:27: error: invalid types 'int[int]' for array subscript
   44 |                         dx[a+b].PB(data(a, b, u));
      |                           ^
a.cc:44:36: error: reference to 'data' is ambiguous
   44 |                         dx[a+b].PB(data(a, b, u));
      |                                    ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:18:8: note:                 'struct data'
   18 | struct data{
      |        ^~~~
a.cc:46:27: error: invalid types 'int[int]' for array subscript
   46 |                         dy[a+b].PB(data(a, b, u));
      |                           ^
a.cc:46:36: error: reference to 'data' is ambiguous
   46 |                         dy[a+b].PB(data(a, b, u));
      |                                    ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:18:8: note:                 'struct data'
   18 | struct data{
      |        ^~~~
a.cc:97:22: error: 'begin' was not declared in this scope
   97 |         for(auto p : dy){
      |                      ^~
a.cc:97:22: note: suggested alternatives:
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:166:
/usr/include/c++/14/valarray:1238:5: note:   'std::begin'
 1238 |     begin(const valarray<_Tp>& __va) noexcept
      |     ^~~~~
In file included from /usr/include/c++/14/filesystem:53,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:200:
/usr/include/c++/14/bits/fs_dir.h:608:3: note:   'std::filesystem::__cxx11::begin'
  608 |   begin(recursive_directory_iterator __iter) noexcept
      |   ^~~~~
a.cc:97:22: error: 'end' was not declared in this scope
   97 |         for(auto p : dy){
      |                      ^~
a.cc:97:22: note: suggested alternatives:
/usr/include/c++/14/valarray:1265:5: note:   'std::end'
 1265 |     end(const valarray<_Tp>& __va) noexcept
      |     ^~~
/usr/include/c++/14/bits/fs_dir.h:613:3: note:   'std::filesystem::__cxx11::end'
  613 |   end(recursive_directory_iterator) noexcept
      |   ^~~
a.cc:98:24: error: request for member 'count' in 'dx', which is of non-class type 'int'
   98 |                 if(!dx.count(p.F)) continue;
      |                        ^~~~~
a.cc:99:28: error: template argument 1 is invalid
   99 |                 vector<data> v1 = p.S, v2 = dx[p.F];
      |                            ^
a.cc:99:28: error: template argument 2 is invalid
a.cc:101:27: error: request for member 'size' in 'v1', which is of non-class type 'int'
  101 |                 FOR(i, v1.size()){
      |                           ^~~~
a.cc:5:44: note: in definition of macro 'FOR'
    5 | #define FOR(i, n) for(int i = 0; i < (int) n; i++)
      |                                            ^
a.cc:102:36: error: invalid types 'int[int]' for array subscript
  102 |                         int xx = v1[i].x, yy = v1[i].y;
      |                                    ^
a.cc:103:32: error: 'v2' was not declared in this scope; did you mean 'v1'?
  103 |                         FOR(j, v2.size()){
      |                                ^~
a.cc:5:44: note: in definition of macro 'FOR'
    5 | #define FOR(i, n) for(int i = 0; i < (int) n; i++)
      |                                            ^