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

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int X[200010], Y[200010];
char U[200010];
vector<vector<pair<int, char>>> yoko(200010), tate(200010);
vector<vector<pair<int, char>>> waue(400010), wasi(400010);
vector<vector<pair<int, char>>> saue(400010), sasi(400010);

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> X[i] >> Y[i] >> U[i];
    if (U[i] == 'L' || U[i] == 'R')
      yoko[Y[i]].emplace_back(make_pair(X[i], U[i]));
    else
      tate[X[i]].emplace_back(make_pair(Y[i], U[i]));
    if (U[i] == 'U' || U[i] == 'R')
      waue[X[i] + Y[i]].emplace_back(make_pair(X[i], U[i]));
    else
      wasi[X[i] + Y[i]].emplace_back(make_pair(X[i], U[i]));
    if (U[i] == 'L' || U[i] == 'U')
      saue[Y[i] - X[i] + 200000].emplace_back(make_pair(X[i], U[i]));
    else
      sasi[Y[i] - X[i] + 200000].emplace_back(make_pair(X[i], U[i]));
  }
  int ans = 1e9;
  for (int i = 0; i < 200010; ++i) {
    sort(yoko[i].begin(), yoko[i].end());
    for (int j = 0; j + 1 < yoko[i].size(); ++j) {
      if (yoko[i][j].second == 'R' && yoko[i][j + 1].second == 'L') {
        ans = min(ans, (yoko[i][j + 1].first - yoko[i][j].first) * 5);
      }
    }
    sort(tate[i].begin(), tate[i].end());
    for (int j = 0; j + 1 < tate[i].size(); ++j) {
      if (tate[i][j].second == 'U' && tate[i][j + 1].second == 'D') {
        ans = min(ans, (tate[i][j + 1].first - tate[i][j].first) * 5);
      }
    }
  }
  for (int i = 0; i < 400010; ++i) {
    sort(waue[i].begin(), waue[i].end());
    for (int j = 0; j + 1 < waue[i].size(); ++j) {
      if (waue[i][j].second == 'R' && waue[i][j + 1].second == 'U') {
        ans = min(ans, (waue[i][j + 1].first - waue[i][j].first) * 10);
      }
    }
    sort(wasi[i].begin(), wasi[i].end());
    for (int j = 0; j + 1 < wasi[i].size(); ++j) {
      if (wasi[i][j].second == 'D' && wasi[i][j + 1].second == 'L') {
        ans = min(ans, (wasi[i][j + 1].first - wasi[i][j].first) * 10);
      }
    }
    sort(saue[i].begin(), saue[i].end());
    for (int j = 0; j + 1 < saue[i].size(); ++j) {
      if (saue[i][j].second == 'U' && saue[i][j + 1].second == 'L') {
        ans = min(ans, (saue[i][j + 1].first - saue[i][j].first) * 10);
      }
    }
    sort(sasi[i].begin(), sasi[i].end());
    for (int j = 0; j + 1 < sasi[i].size(); ++j) {
      if (sasi[i][j].second == 'R' && sasi[i][j + 1].second == 'D') {
        ans = min(ans, (sasi[i][j + 1].first - sasi[i][j].first) * 10);
      }
    }
  }
  if (ans > 1e8)
    cout << "SAFE" << endl;
  else
    cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int X[200010], Y[200010];
char U[200010];
vector<vector<pair<int, char>>> yoko(200010), tate(200010);
vector<vector<pair<int, char>>> waue(400010), wasi(400010);
vector<vector<pair<int, char>>> saue(400010), sasi(400010);

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> X[i] >> Y[i] >> U[i];
    if (U[i] == 'L' || U[i] == 'R')
      yoko[Y[i]].emplace_back(make_pair(X[i], U[i]));
    else
      tate[X[i]].emplace_back(make_pair(Y[i], U[i]));
    if (U[i] == 'U' || U[i] == 'R')
      waue[X[i] + Y[i]].emplace_back(make_pair(X[i], U[i]));
    else
      wasi[X[i] + Y[i]].emplace_back(make_pair(X[i], U[i]));
    if (U[i] == 'L' || U[i] == 'U')
      saue[Y[i] - X[i] + 200000].emplace_back(make_pair(X[i], U[i]));
    else
      sasi[Y[i] - X[i] + 200000].emplace_back(make_pair(X[i], U[i]));
  }
  int ans = 1e9;
  for (int i = 0; i < 200010; ++i) {
    sort(yoko[i].begin(), yoko[i].end());
    for (int j = 0; j + 1 < yoko[i].size(); ++j) {
      if (yoko[i][j].second == 'R' && yoko[i][j + 1].second == 'L') {
        ans = min(ans, (yoko[i][j + 1].first - yoko[i][j].first) * 5);
      }
    }
    sort(tate[i].begin(), tate[i].end());
    for (int j = 0; j + 1 < tate[i].size(); ++j) {
      if (tate[i][j].second == 'U' && tate[i][j + 1].second == 'D') {
        ans = min(ans, (tate[i][j + 1].first - tate[i][j].first) * 5);
      }
    }
  }
  for (int i = 0; i < 400010; ++i) {
    sort(waue[i].begin(), waue[i].end());
    for (int j = 0; j + 1 < waue[i].size(); ++j) {
      if (waue[i][j].second = 'R' && waue[i][j + 1].second == 'U') {
        ans = min(ans, (waue[i][j + 1].first - waue[i][j].first) * 10);
      }
    }
    sort(wasi[i].begin(), wasi[i].end());
    for (int j = 0; j + 1 < wasi[i].size(); ++j) {
      if (wasi[i][j].second = 'D' && wasi[i][j + 1].second == 'L') {
        ans = min(ans, (wasi[i][j + 1].first - wasi[i][j].first) * 10);
      }
    }
    sort(saue[i].begin(), saue[i].end());
    for (int j = 0; j + 1 < saue[i].size(); ++j) {
      if (saue[i][j].second = 'U' && waue[i][j + 1].second == 'L') {
        ans = min(ans, (saue[i][j + 1].first - saue[i][j].first) * 10);
      }
    }
    sort(sasi[i].begin(), sasi[i].end());
    for (int j = 0; j + 1 < sasi[i].size(); ++j) {
      if (sasi[i][j].second = 'R' && sasi[i][j + 1].second == 'D') {
        ans = min(ans, (sasi[i][j + 1].first - sasi[i][j].first) * 10);
      }
    }
  }
  if (ans > 1e8)
    cout << "SAFE" << endl;
  else
    cout << ans << endl;
  return 0;
}