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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (int i = 0; i < sz(v); i++) {
    os << v[i];
    if (i < sz(v) - 1)
      os << " ";
  }
  return os;
}
int main() {
  int n;
  cin >> n;
  vector<tuple<int, int, char>> p;
  vector<pair<int, int>> l, r, u, d;
  rep(i, n) {
    int x, y;
    char c;
    cin >> x >> y >> c;
    p.emplace_back(x, y, c);
    if (c == 'L')
      l.emplace_back(x, y);
    if (c == 'R')
      r.emplace_back(x, y);
    if (c == 'U')
      u.emplace_back(x, y);
    if (c == 'D')
      d.emplace_back(x, y);
  }

  int ans = INF;
  {
    vector<vector<int>> ll(400400), dd(400400);
    for (auto [x, y, c] : p) {
      if (c == 'L')
        ll[y].push_back(x);
      if (c == 'D')
        dd[x].push_back(y);
    }
    for (auto &v : ll)
      v.push_back(INF), sort(all(v));
    for (auto &v : dd)
      v.push_back(INF), sort(all(v));
    for (auto [x, y] : r) {
      int xx = *lower_bound(all(ll[y]), x);
      if (xx < INF)
        chmin(ans, (xx - x) * 5);
    }
    for (auto [x, y] : u) {
      int yy = *lower_bound(all(dd[x]), y);
      if (yy < INF)
        chmin(ans, (yy - y) * 5);
    }
  }
  {
    vector<vector<int>> ll(400400), uu(400400);
    for (auto [x, y, c] : p) {
      if (c == 'L')
        ll[x + y].push_back(x);
      if (c == 'U')
        uu[x + y].push_back(x);
    }
    for (auto &v : ll)
      v.push_back(INF), sort(all(v));
    for (auto &v : uu)
      v.push_back(INF), sort(all(v));
    for (auto [x, y] : r) {
      int xx = *lower_bound(all(uu[x + y]), x);
      if (xx < INF)
        chmin(ans, (xx - x) * 10);
    }
    for (auto [x, y] : d) {
      int xx = *lower_bound(all(ll[x + y]), x);
      if (xx < INF)
        chmin(ans, (xx - x) * 10);
    }
  }
  {
    map<int, vector<int>> ll, dd;
    for (auto [x, y, c] : p) {
      if (c == 'L')
        ll[-x + y].push_back(x);
      if (c == 'D')
        dd[-x + y].push_back(x);
    }
    for (auto &[key, v] : ll)
      v.push_back(INF), sort(all(v));
    for (auto &[key, v] : dd)
      v.push_back(INF), sort(all(v));
    for (auto [x, y] : r) {
      if (dd.count(-x + y) == 0)
        continue;
      int xx = *lower_bound(all(dd[-x + y]), x);
      if (xx < INF)
        chmin(ans, (xx - x) * 10);
    }
    for (auto [x, y] : u) {
      if (ll.count(-x + y) == 0)
        continue;
      int xx = *lower_bound(all(ll[-x + y]), x);
      if (xx < INF)
        chmin(ans, (xx - x) * 10);
    }
  }
  if (ans == INF)
    cout << "SAFE" << endl;
  else
    cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (int i = 0; i < sz(v); i++) {
    os << v[i];
    if (i < sz(v) - 1)
      os << " ";
  }
  return os;
}
int main() {
  int n;
  cin >> n;
  vector<tuple<int, int, char>> p;
  vector<pair<int, int>> l, r, u, d;
  rep(i, n) {
    int x, y;
    char c;
    cin >> x >> y >> c;
    p.emplace_back(x, y, c);
    if (c == 'L')
      l.emplace_back(x, y);
    if (c == 'R')
      r.emplace_back(x, y);
    if (c == 'U')
      u.emplace_back(x, y);
    if (c == 'D')
      d.emplace_back(x, y);
  }

  int ans = INF;
  {
    vector<vector<int>> ll(200200), dd(200200);
    for (auto [x, y, c] : p) {
      if (c == 'L')
        ll[y].push_back(x);
      if (c == 'D')
        dd[x].push_back(y);
    }
    for (auto &v : ll)
      v.push_back(INF), sort(all(v));
    for (auto &v : dd)
      v.push_back(INF), sort(all(v));
    for (auto [x, y] : r) {
      int xx = *lower_bound(all(ll[y]), x);
      if (xx < INF)
        chmin(ans, (xx - x) * 5);
    }
    for (auto [x, y] : u) {
      int yy = *lower_bound(all(dd[x]), y);
      if (yy < INF)
        chmin(ans, (yy - y) * 5);
    }
  }
  {
    vector<vector<int>> ll(200200), uu(200200);
    for (auto [x, y, c] : p) {
      if (c == 'L')
        ll[x + y].push_back(x);
      if (c == 'U')
        uu[x + y].push_back(x);
    }
    for (auto &v : ll)
      v.push_back(INF), sort(all(v));
    for (auto &v : uu)
      v.push_back(INF), sort(all(v));
    for (auto [x, y] : r) {
      int xx = *lower_bound(all(uu[x + y]), x);
      if (xx < INF)
        chmin(ans, (xx - x) * 10);
    }
    for (auto [x, y] : d) {
      int xx = *lower_bound(all(ll[x + y]), x);
      if (xx < INF)
        chmin(ans, (xx - x) * 10);
    }
  }
  {
    map<int, vector<int>> ll, dd;
    for (auto [x, y, c] : p) {
      if (c == 'L')
        ll[-x + y].push_back(x);
      if (c == 'D')
        dd[-x + y].push_back(x);
    }
    for (auto &[key, v] : ll)
      v.push_back(INF), sort(all(v));
    for (auto &[key, v] : dd)
      v.push_back(INF), sort(all(v));
    for (auto [x, y] : r) {
      if (dd.count(-x + y) == 0)
        continue;
      int xx = *lower_bound(all(dd[-x + y]), x);
      if (xx < INF)
        chmin(ans, (xx - x) * 10);
    }
    for (auto [x, y] : u) {
      if (ll.count(-x + y) == 0)
        continue;
      int xx = *lower_bound(all(ll[-x + y]), x);
      if (xx < INF)
        chmin(ans, (xx - x) * 10);
    }
  }
  if (ans == INF)
    cout << "SAFE" << endl;
  else
    cout << ans << endl;
}