Language: C++

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (long long  i=a;i<n;i++)
#define sz(A) (int)(A.size())
#define all(x) (x).begin(),(x).end()
#define get(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
void solve() {
	int n;
	cin >> n;
	map<int,set<int> > L, R, U, D;
	set<int> LY;
	rep(i, 0, n) {
		int x, y;
		char dir;
		cin >> x >> y >> dir;
		if (dir == 'U') {
			U[x].insert(y);
		}
		if (dir == 'D') {
			D[x].insert(y);

		}
		if (dir == 'L') {
			L[y].insert(x);
			LY.insert(y);
		}
		if (dir == 'R') {
			R[y].insert(x);
		}
	}
	int dis = INT_MAX, dis2 = INT_MAX;
	for (auto m : U) {
		set<int> up = m.second;
		set<int> down = D[m.first];
		for (auto y : up) {
			auto it = down.lower_bound(y);
			if (it == down.end()) {
				break;
			}
			else {
				dis = min(dis, (*it) - y);
			}
		}
	}
	for (auto m : R) {
		set<int> right = m.second;
		set<int> left = L[m.first];
		for (auto x : right) {
			auto it = left.lower_bound(x);
			if (it == left.end()) {
				break;
			}
			else {
				dis = min(dis, (*it) - x);
			}
		}
	}
	for (auto m : U) {
		set<int> up = m.second;
		int x = m.first;
		for (auto y0 : up) {
			auto it = L.upper_bound(y0);
			for (;it!=L.end();it++)
			{
				set<int> left = (*it).second;
				int y = (*it).first;
				if (left.find(y - y0 + x) != left.end()) {
					dis2 = min(dis2, abs(y0 - y));
				}
			}
			auto it = R.upper_bound(y0);
			for (; it != R.end(); it++) {
				auto m2 = (*it);
				set<int> right = m2.second;
				int y = m2.first;
				if (y <= y0)continue;
				if (right.find(y0 - y + x) != right.end()) {
					dis2 = min(dis2, abs(y0 - y));
				}
			}
		}
	}
	for (auto m : D) {
		set<int> down = m.second;
		int x = m.first;
		for (auto y0 : down) {
			auto it = L.lower_bound(y0);

			while (1)
			{
				--it;
				auto m2 = *(it);
				set<int> left = m2.second;
				int y = m2.first;
				if (y >= y0)continue;
				if (left.find(y0 - y + x) != left.end()) {
					dis2 = min(dis2, abs(y0 - y));
				}
				if (it == L.begin())break;
			}
			auto it = L.lower_bound(y0);
			while (1) {
				--it;
				auto m2 = *(it);
				set<int> right = m2.second;
				int y = m2.first;
				if (y >= y0)continue;
				if (right.find(y - y0 + x) != right.end()) {
					dis2 = min(dis2, abs(y0 - y));
				}
				if (it == L.begin())break;
			}
		}
	}
	if (dis == INT_MAX && dis2 == INT_MAX) {
		cout << "SAFE" << endl;
	}
	else {
		int ans = INT_MAX;
		if (dis != INT_MAX) {
			ans = min(ans, dis * 5);
		}
		if (dis2 != INT_MAX) {
			ans = min(ans, dis2 * 10);
		}
		cout << ans << endl;
	}
}
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int T_ = 1;
	//cin >> T_;
	for (int i = 0; i < T_; i++)solve();
	return 0;
}



Compiler output:
a.cc: In function 'void solve()':
a.cc:74:30: error: conflicting declaration 'auto it'
   74 |                         auto it = R.upper_bound(y0);
      |                              ^~
a.cc:65:30: note: previous declaration as 'std::_Rb_tree_iterator<std::pair<const int, std::set<int> > > it'
   65 |                         auto it = L.upper_bound(y0);
      |                              ^~
a.cc:104:30: error: conflicting declaration 'auto it'
  104 |                         auto it = L.lower_bound(y0);
      |                              ^~
a.cc:90:30: note: previous declaration as 'std::_Rb_tree_iterator<std::pair<const int, std::set<int> > > it'
   90 |                         auto it = L.lower_bound(y0);
      |                              ^~