Problem p02605 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
#define REP(i, n) for (int i = 0; (i64)(i) < (i64)(n); ++i)

#ifdef ENABLE_DEBUG
template <typename T> void debug(T value) { cerr << value; }
template <typename T, typename... Ts> void debug(T value, Ts... args) {
  cerr << value << ", ";
  debug(args...);
}
#define DEBUG(...)                                                             \
  do {                                                                         \
    cerr << " \033[33m (L" << __LINE__ << ") ";                                \
    cerr << #__VA_ARGS__ << ":\033[0m ";                                       \
    debug(__VA_ARGS__);                                                        \
    cerr << endl;                                                              \
  } while (0)
#else
#define debug(...)
#define DEBUG(...)
#endif

const i64 INF = 1LL << 40;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<tuple<i64, i64>> pu, pr, pd, pl;
  REP(i, N) {
    i64 x, y;
    char u;
    cin >> x >> y >> u;
    if (u == 'R') {
      pr.emplace_back(x, y);
    } else if (u == 'L') {
      pl.emplace_back(x, y);
    } else if (u == 'U') {
      pu.emplace_back(x, y);
    } else {
      assert(u == 'D');
      pd.emplace_back(x, y);
    }
  }
  i64 ans = INF;
  int p = -1;

  { // U-D
    map<i64, set<i64>> t;
    for (auto [xu, yu] : pu) {
      t[xu].insert(yu);
    }
    for (auto [xd, yd] : pd) {
      auto &s = t[xd];
      if (s.empty())
        continue;
      auto it = s.lower_bound(yd);
      if (it == s.begin())
        continue;
      i64 yu = *--it;
      assert(yu < yd);
      ans = min<i64>(ans, (yd - yu) * 5);
    }
  }
  { // L-R
    map<i64, set<i64>> t;
    for (auto [xr, yr] : pr) {
      t[yr].insert(xr);
    }
    for (auto [xl, yl] : pl) {
      auto &s = t[yl];
      if (s.empty())
        continue;
      auto it = s.lower_bound(yl);
      if (it == s.begin())
        continue;
      i64 xr = *--it;
      assert(xr < xl);
      ans = min<i64>(ans, (xl - xr) * 5);
    }
  }
  { // D-R
    map<i64, set<i64>> t;
    for (auto [xd, yd] : pd) {
      t[yd - xd].insert(xd);
    }
    for (auto [xr, yr] : pr) {
      auto &s = t[yr - xr];
      auto it = s.lower_bound(xr);
      if (it == s.end())
        continue;
      i64 xd = *it;
      assert(xd > xr);
      ans = min<i64>(ans, 10 * (xd - xr));
    }
  }
  { // D-L
    map<i64, set<i64>> t_minx;
    for (auto [xd, yd] : pd) {
      t_minx[xd + yd].insert(xd);
    }
    for (auto [xl, yl] : pl) {
      auto &s = t_minx[xl + yl];
      auto it = s.lower_bound(xl);
      if (s.empty() || it == s.begin())
        continue;
      --it;
      i64 xd = *it;
      if (xd < xl) {
        DEBUG(xl - xd, xl, yl);
        ans = min<i64>(ans, 10 * (xl - xd));
        p = 4;
      }
    }
  }
  { // U-R
    map<i64, set<i64>> t_maxx;
    for (auto [xu, yu] : pu) {
      t_maxx[yu + xu].insert(xu);
    }
    for (auto [xr, yr] : pr) {
      auto &s = t_maxx[yr + xr];
      auto it = s.lower_bound(xr);
      if (it == s.end())
        continue;
      i64 xu = *it;
      if (xu > xr) {
        DEBUG(xu - xr, xr, yr);
        ans = min<i64>(ans, 10 * (xu - xr));
        p = 5;
      }
    }
  }
  { // U-L
    map<i64, set<i64>> t_minx;
    for (auto [x, y] : pu) {
      t_minx[y - x].insert(x);
    }
    for (auto [xl, yl] : pl) {
      auto &s = t_minx[yl - xl];
      auto it = s.lower_bound(xl);
      if (s.empty() || it == s.begin())
        continue;
      --it;
      i64 xu = *it;
      if (xu < xl) {
        DEBUG(xl - xu, xl, yl);
        ans = min<i64>(ans, 10 * (xl - xu));
        p = 6;
      }
    }
  }
  if (ans == INF) {
    cout << "SAFE" << endl;
  } else {
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
#define REP(i, n) for (int i = 0; (i64)(i) < (i64)(n); ++i)

#ifdef ENABLE_DEBUG
template <typename T> void debug(T value) { cerr << value; }
template <typename T, typename... Ts> void debug(T value, Ts... args) {
  cerr << value << ", ";
  debug(args...);
}
#define DEBUG(...)                                                             \
  do {                                                                         \
    cerr << " \033[33m (L" << __LINE__ << ") ";                                \
    cerr << #__VA_ARGS__ << ":\033[0m ";                                       \
    debug(__VA_ARGS__);                                                        \
    cerr << endl;                                                              \
  } while (0)
#else
#define debug(...)
#define DEBUG(...)
#endif

const i64 INF = 1LL << 40;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<tuple<i64, i64>> pu, pr, pd, pl;
  REP(i, N) {
    i64 x, y;
    char u;
    cin >> x >> y >> u;
    if (u == 'R') {
      pr.emplace_back(x, y);
    } else if (u == 'L') {
      pl.emplace_back(x, y);
    } else if (u == 'U') {
      pu.emplace_back(x, y);
    } else {
      assert(u == 'D');
      pd.emplace_back(x, y);
    }
  }
  i64 ans = INF;
  int p = -1;

  { // U-D
    map<i64, set<i64>> t;
    for (auto [xu, yu] : pu) {
      t[xu].insert(yu);
    }
    for (auto [xd, yd] : pd) {
      auto &s = t[xd];
      if (s.empty())
        continue;
      auto it = s.lower_bound(yd);
      if (it == s.begin())
        continue;
      i64 yu = *--it;
      assert(yu < yd);
      ans = min<i64>(ans, (yd - yu) * 5);
    }
  }
  { // L-R
    map<i64, set<i64>> t;
    for (auto [xr, yr] : pr) {
      t[yr].insert(xr);
    }
    for (auto [xl, yl] : pl) {
      auto &s = t[yl];
      if (s.empty())
        continue;
      auto it = s.lower_bound(xl);
      if (it == s.begin())
        continue;
      i64 xr = *--it;
      assert(xr < xl);
      ans = min<i64>(ans, (xl - xr) * 5);
    }
  }
  { // D-R
    map<i64, set<i64>> t;
    for (auto [xd, yd] : pd) {
      t[yd - xd].insert(xd);
    }
    for (auto [xr, yr] : pr) {
      auto &s = t[yr - xr];
      auto it = s.lower_bound(xr);
      if (it == s.end())
        continue;
      i64 xd = *it;
      assert(xd > xr);
      ans = min<i64>(ans, 10 * (xd - xr));
    }
  }
  { // D-L
    map<i64, set<i64>> t_minx;
    for (auto [xd, yd] : pd) {
      t_minx[xd + yd].insert(xd);
    }
    for (auto [xl, yl] : pl) {
      auto &s = t_minx[xl + yl];
      auto it = s.lower_bound(xl);
      if (s.empty() || it == s.begin())
        continue;
      --it;
      i64 xd = *it;
      if (xd < xl) {
        DEBUG(xl - xd, xl, yl);
        ans = min<i64>(ans, 10 * (xl - xd));
        p = 4;
      }
    }
  }
  { // U-R
    map<i64, set<i64>> t_maxx;
    for (auto [xu, yu] : pu) {
      t_maxx[yu + xu].insert(xu);
    }
    for (auto [xr, yr] : pr) {
      auto &s = t_maxx[yr + xr];
      auto it = s.lower_bound(xr);
      if (it == s.end())
        continue;
      i64 xu = *it;
      if (xu > xr) {
        DEBUG(xu - xr, xr, yr);
        ans = min<i64>(ans, 10 * (xu - xr));
        p = 5;
      }
    }
  }
  { // U-L
    map<i64, set<i64>> t_minx;
    for (auto [x, y] : pu) {
      t_minx[y - x].insert(x);
    }
    for (auto [xl, yl] : pl) {
      auto &s = t_minx[yl - xl];
      auto it = s.lower_bound(xl);
      if (s.empty() || it == s.begin())
        continue;
      --it;
      i64 xu = *it;
      if (xu < xl) {
        DEBUG(xl - xu, xl, yl);
        ans = min<i64>(ans, 10 * (xl - xu));
        p = 6;
      }
    }
  }
  if (ans == INF) {
    cout << "SAFE" << endl;
  } else {
    cout << ans << endl;
  }
}