Problem p02605 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define rrep(i, n) for (ll i = n - 1; i > -1; i--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define fi first
#define se second
template <class X> void print(X x) { cout << x << endl; }
void print(vl x) {
  for (ll i : x) {
    cout << i << " ";
  }
  cout << endl;
}
const ll INF = (1LL << 61) - 1;
const ll MOD = 1000000007 /*998244353*/;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, h, x, y, z, p, q, n, t, r, k, w, l, ans, i, j, u, v, m;
ll codeforces = 1;
string S, T;
vl A, B;
vector<tuple<ll, ll, ll>> UD, RL, UL, RD, UR, LD;
void input() {
  cin >> n;
  rep(i, n) {
    cin >> x >> y;
    char C;
    cin >> C;
    if (C == 'U') {
      UD.pb({x, y, 1});
      UL.pb({y - x, y + x, 1});
      UR.pb({y + x, y - x, 1});
    }
    if (C == 'D') {
      UD.pb({x, y, -1});
      RD.pb({y - x, y + x, -1});
      LD.pb({y + x, y - x, -1});
    }
    if (C == 'L') {
      RL.pb({y, x, -1});
      UL.pb({y - x, y + x, -1});
      LD.pb({y + x, y - x, 1});
    }
    if (C == 'R') {
      RL.pb({y, x, 1});
      RD.pb({y - x, y + x, 1});
      UR.pb({y + x, y - x, -1});
    }
  }
}
void solve() {
  sort(all(UD));
  sort(all(RL));
  sort(all(UL));
  sort(all(RD));
  sort(all(UR));
  sort(all(LD));
  // tate
  a = INF;
  d = UD.size();
  e = RL.size();
  f = UL.size();
  t = RD.size();
  k = UR.size();
  l = LD.size();
  rep(i, d - 1) {
    auto [x, y, z] = UD[i];
    auto [p, q, r] = UD[i + 1];
    if (z == 1 && r == -1 && x == p)
      a = min(a, (q - y) * 5);
  }
  rep(i, e - 1) {
    auto [x, y, z] = RL[i];
    auto [p, q, r] = RL[i + 1];
    if (z == 1 && r == -1 && x == p)
      a = min(a, (q - y) * 5);
  }
  rep(i, f - 1) {
    auto [x, y, z] = UL[i];
    auto [p, q, r] = UL[i + 1];
    if (z == 1 && r == -1 && x == p)
      a = min(a, (q - y) * 5);
  }
  rep(i, t - 1) {
    auto [x, y, z] = RD[i];
    auto [p, q, r] = RD[i + 1];
    if (z == 1 && r == -1 && x == p)
      a = min(a, (q - y) * 5);
  }
  rep(i, k - 1) {
    auto [x, y, z] = UR[i];
    auto [p, q, r] = UR[i + 1];
    if (z == 1 && r == -1 && x == p)
      a = min(a, (q - y) * 5);
  }
  rep(i, l - 1) {
    auto [x, y, z] = LD[i];
    auto [p, q, r] = LD[i + 1];
    if (z == 1 && r == -1 && x == p)
      a = min(a, (q - y) * 5);
  }
  if (a == INF)
    print("SAFE");
  else
    print(a);
}
int main() {
  // cout<<fixed<<setprecision(15);
  cin.tie(0);
  ios::sync_with_stdio(false);
  input();
  while (codeforces--) {
    solve();
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define rrep(i, n) for (ll i = n - 1; i > -1; i--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define fi first
#define se second
template <class X> void print(X x) { cout << x << endl; }
void print(vl x) {
  for (ll i : x) {
    cout << i << " ";
  }
  cout << endl;
}
const ll INF = (1LL << 61) - 1;
const ll MOD = 1000000007 /*998244353*/;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, h, x, y, z, p, q, n, t, r, k, w, l, ans, i, j, u, v, m;
ll codeforces = 1;
string S, T;
vl A, B;
vector<tuple<ll, ll, ll>> UD, RL, UL, RD, UR, LD;
void input() {
  cin >> n;
  rep(i, n) {
    cin >> x >> y;
    char C;
    cin >> C;
    if (C == 'U') {
      UD.pb({x, y, 1});
      UL.pb({y - x, y + x, 1});
      UR.pb({y + x, y - x, 1});
    }
    if (C == 'D') {
      UD.pb({x, y, -1});
      RD.pb({y - x, y + x, -1});
      LD.pb({y + x, y - x, -1});
    }
    if (C == 'L') {
      RL.pb({y, x, -1});
      UL.pb({y - x, y + x, -1});
      LD.pb({y + x, y - x, 1});
    }
    if (C == 'R') {
      RL.pb({y, x, 1});
      RD.pb({y - x, y + x, 1});
      UR.pb({y + x, y - x, -1});
    }
  }
}
void solve() {
  sort(all(UD));
  sort(all(RL));
  sort(all(UL));
  sort(all(RD));
  sort(all(UR));
  sort(all(LD));
  // tate
  a = INF;
  d = UD.size();
  e = RL.size();
  f = UL.size();
  t = UD.size();
  k = RL.size();
  l = UL.size();
  rep(i, d - 1) {
    auto [x, y, z] = UD[i];
    auto [p, q, r] = UD[i + 1];
    if (z == 1 && r == -1 && x == p)
      a = min(a, (q - y) * 5);
  }
  rep(i, e - 1) {
    auto [x, y, z] = RL[i];
    auto [p, q, r] = RL[i + 1];
    if (z == 1 && r == -1 && x == p)
      a = min(a, (q - y) * 5);
  }
  rep(i, f - 1) {
    auto [x, y, z] = UL[i];
    auto [p, q, r] = UL[i + 1];
    if (z == 1 && r == -1 && x == p)
      a = min(a, (q - y) * 5);
  }
  rep(i, t - 1) {
    auto [x, y, z] = RD[i];
    auto [p, q, r] = RD[i + 1];
    if (z == 1 && r == -1 && x == p)
      a = min(a, (q - y) * 5);
  }
  rep(i, k - 1) {
    auto [x, y, z] = UR[i];
    auto [p, q, r] = UR[i + 1];
    if (z == 1 && r == -1 && x == p)
      a = min(a, (q - y) * 5);
  }
  rep(i, l - 1) {
    auto [x, y, z] = LD[i];
    auto [p, q, r] = LD[i + 1];
    if (z == 1 && r == -1 && x == p)
      a = min(a, (q - y) * 5);
  }
  if (a == INF)
    print("SAFE");
  else
    print(a);
}
int main() {
  // cout<<fixed<<setprecision(15);
  cin.tie(0);
  ios::sync_with_stdio(false);
  input();
  while (codeforces--) {
    solve();
  }
}