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

#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int mod = 1e9 + 7;
vector<pair<int, char>> X[200001], Y[200001], xpyu[400001], xpyd[400001],
    xmyu[400001], xmyd[400001];
signed main() {
  int n, x, y;
  char u;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x >> y >> u;
    if (u == 'R' || u == 'L')
      X[y].emplace_back(x, u);
    else
      Y[x].emplace_back(y, u);
    if (u == 'U' || u == 'R')
      xpyu[x + y].emplace_back(y, u);
    else
      xpyd[x + y].emplace_back(y, u);
    if (u == 'U' || u == 'L')
      xmyu[x - y + 200000].emplace_back(y, u);
    else
      xmyd[x - y + 200000].emplace_back(y, u);
  }
  int ans = mod;
  for (int i = 0; i <= 200000; i++) {
    sort(X[i].begin(), X[i].end());
    for (int j = 0; j + 1 < X[i].size(); j++)
      if (X[i][j].second == 'R' && X[i][j + 1].second == 'L')
        ans = min(ans, (X[i][j + 1].first - X[i][j].first) * 5);
  }
  for (int i = 0; i <= 200000; i++) {
    sort(Y[i].begin(), Y[i].end());
    for (int j = 0; j + 1 < Y[i].size(); j++)
      if (Y[i][j].second == 'U' && Y[i][j + 1].second == 'D')
        ans = min(ans, (Y[i][j + 1].first - Y[i][j].first) * 5);
  }
  for (int i = 0; i <= 400000; i++) {
    sort(xpyu[i].begin(), xpyu[i].end());
    for (int j = 0; j + 1 < xpyu[i].size(); j++)
      if (xpyu[i][j].second == 'U' && xpyu[i][j + 1].second == 'R')
        ans = min(ans, (xpyu[i][j + 1].first - xpyu[i][j].first) * 10);
  }
  for (int i = 0; i <= 400000; i++) {
    sort(xpyd[i].begin(), xpyd[i].end());
    for (int j = 0; j + 1 < xpyd[i].size(); j++)
      if (xpyd[i][j].second == 'L' && xpyd[i][j + 1].second == 'D')
        ans = min(ans, (xpyd[i][j + 1].first - xpyd[i][j].first) * 10);
  }
  for (int i = 0; i <= 400000; i++) {
    sort(xmyu[i].begin(), xmyu[i].end());
    for (int j = 0; j + 1 < xmyu[i].size(); j++)
      if (xmyu[i][j].second == 'U' && xmyu[i][j + 1].second == 'L')
        ans = min(ans, (xmyu[i][j + 1].first - xmyu[i][j].first) * 10);
  }
  for (int i = 0; i <= 400000; i++) {
    sort(xmyd[i].begin(), xmyd[i].end());
    for (int j = 0; j + 1 < xmyd[i].size(); j++)
      if (xmyd[i][j].second == 'R' && xmyd[i][j + 1].second == 'D')
        ans = min(ans, (xmyd[i][j + 1].first - xmyd[i][j].first) * 10);
  }
  cout << (ans == mod ? "SAFE" : to_string(ans)) << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int mod = 1e9 + 7;
vector<pair<int, char>> X[200001], Y[200001], xpyu[400001], xpyd[400001],
    xmyu[400001], xmyd[400001];
signed main() {
  int n, x, y;
  char u;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x >> y >> u;
    if (u == 'R' || u == 'L')
      X[y].emplace_back(x, u);
    else
      Y[x].emplace_back(y, u);
    if (u == 'U' || u == 'R')
      xpyu[x + y].emplace_back(y, u);
    else
      xpyd[x + y].emplace_back(y, u);
    if (u == 'U' || u == 'L')
      xmyu[x - y + 200000].emplace_back(y, u);
    else
      xmyd[x - y + 200000].emplace_back(y, u);
  }
  int ans = mod;
  for (int i = 0; i <= 200000; i++) {
    sort(X[i].begin(), X[i].end());
    for (int j = 0; j + 1 < X[i].size(); j++)
      if (X[i][j].second == 'R' && X[i][j + 1].second == 'L')
        ans = min(ans, (X[i][j + 1].first - X[i][j].first) * 5);
  }
  for (int i = 0; i <= 200000; i++) {
    sort(Y[i].begin(), Y[i].end());
    for (int j = 0; j + 1 < Y[i].size(); j++)
      if (Y[i][j].second == 'U' && Y[i][j + 1].second == 'D')
        ans = min(ans, (Y[i][j + 1].first - Y[i][j].first) * 5);
  }
  for (int i = 0; i <= 400001; i++) {
    sort(xpyu[i].begin(), xpyu[i].end());
    for (int j = 0; j + 1 < xpyu[i].size(); j++)
      if (xpyu[i][j].second == 'U' && xpyu[i][j + 1].second == 'R')
        ans = min(ans, (xpyu[i][j + 1].first - xpyu[i][j].first) * 10);
  }
  for (int i = 0; i <= 400001; i++) {
    sort(xpyd[i].begin(), xpyd[i].end());
    for (int j = 0; j + 1 < xpyd[i].size(); j++)
      if (xpyd[i][j].second == 'L' && xpyd[i][j + 1].second == 'D')
        ans = min(ans, (xpyd[i][j + 1].first - xpyd[i][j].first) * 10);
  }
  for (int i = 0; i <= 400001; i++) {
    sort(xmyu[i].begin(), xmyu[i].end());
    for (int j = 0; j + 1 < xmyu[i].size(); j++)
      if (xmyu[i][j].second == 'U' && xmyu[i][j + 1].second == 'L')
        ans = min(ans, (xmyu[i][j + 1].first - xmyu[i][j].first) * 10);
  }
  for (int i = 0; i <= 400001; i++) {
    sort(xmyd[i].begin(), xmyd[i].end());
    for (int j = 0; j + 1 < xmyd[i].size(); j++)
      if (xmyd[i][j].second == 'R' && xmyd[i][j + 1].second == 'D')
        ans = min(ans, (xmyd[i][j + 1].first - xmyd[i][j].first) * 10);
  }
  cout << (ans == mod ? "SAFE" : to_string(ans)) << endl;
}