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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.first >> p.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << p.first << ' ' << p.second;
}

bool cmp(P x, P y) {
  return x.second < y.second    ? true
         : x.second == y.second ? x.first < y.first
                                : false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vi x(n), y(n);
  string s;
  rep(i, n) {
    char t;
    cin >> x[i] >> y[i] >> t;
    s += t;
  }

  vector<P> u, r, d, l;
  rep(i, n) {
    switch (s[i]) {
    case 'U':
      u.emplace_back(x[i], y[i]);
      break;
    case 'R':
      r.emplace_back(x[i], y[i]);
      break;
    case 'D':
      d.emplace_back(x[i], y[i]);
      break;
    case 'L':
      l.emplace_back(x[i], y[i]);
      break;
    }
  }
  if (u.size())
    sort(all(u));
  if (r.size())
    sort(all(r), cmp);
  if (d.size())
    sort(all(d));
  if (l.size())
    sort(all(l), cmp);
  double ans = INF;
  rep(i, u.size()) {
    auto itr = lower_bound(all(d), u[i]);
    if (itr != d.end() and u[i].first == itr->first) {
      chmin(ans, double(itr->second - u[i].second) / 2);
    }
  }
  rep(i, r.size()) {
    auto itr = lower_bound(all(l), r[i], cmp);
    if (itr != l.end() and r[i].second == itr->second) {
      chmin(ans, double(itr->first - r[i].first) / 2);
    }
  }

  vector<P> h, g;
  rep(i, u.size()) { h.emplace_back(u[i].first + u[i].second, u[i].first); }
  sort(all(h));
  rep(i, r.size()) { g.emplace_back(r[i].first + r[i].second, r[i].first); }
  rep(i, g.size()) {
    auto itr = lower_bound(all(h), g[i]);
    if (itr != h.end() and g[i].first == itr->first) {
      chmin(ans, -g[i].second + itr->second);
    }
  }
  h.clear();
  g.clear();

  rep(i, d.size()) { h.emplace_back(d[i].first + d[i].second, d[i].first); }
  rep(i, l.size()) { g.emplace_back(l[i].first + l[i].second, l[i].first); }
  sort(all(g));
  rep(i, h.size()) {
    auto itr = lower_bound(all(g), h[i]);
    if (itr != g.end() and h[i].first == itr->first) {
      chmin(ans, -h[i].second + itr->second);
    }
  }
  h.clear();
  g.clear();

  rep(i, u.size()) { h.emplace_back(u[i].first - u[i].second, u[i].first); }
  rep(i, l.size()) { g.emplace_back(l[i].first - l[i].second, l[i].first); }
  sort(all(g));
  rep(i, h.size()) {
    auto itr = lower_bound(all(g), h[i]);
    if (itr != g.end() and h[i].first == itr->first) {
      chmin(ans, -h[i].second + itr->second);
    }
  }
  h.clear();
  g.clear();

  rep(i, d.size()) { h.emplace_back(d[i].first - d[i].second, d[i].first); }
  sort(all(h));
  rep(i, r.size()) { g.emplace_back(r[i].first - r[i].second, r[i].first); }
  rep(i, g.size()) {
    auto itr = lower_bound(all(h), g[i]);
    if (itr != h.end() and g[i].first == itr->first) {
      chmin(ans, -g[i].second + itr->second);
    }
  }
  h.clear();
  g.clear();

  if (ans > INF - 1) {
    cout << "SAFE" << endl;
  } else {
    cout << int(ans * 10 + 0.1) << endl;
  }
}

Error: 0

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.first >> p.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << p.first << ' ' << p.second;
}

bool cmp(P x, P y) {
  return x.second < y.second    ? true
         : x.second == y.second ? x.first < y.first
                                : false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vi x(n), y(n);
  string s;
  rep(i, n) {
    char t;
    cin >> x[i] >> y[i] >> t;
    s += t;
  }

  vector<P> u, r, d, l;
  rep(i, n) {
    switch (s[i]) {
    case 'U':
      u.emplace_back(x[i], y[i]);
      break;
    case 'R':
      r.emplace_back(x[i], y[i]);
      break;
    case 'D':
      d.emplace_back(x[i], y[i]);
      break;
    case 'L':
      l.emplace_back(x[i], y[i]);
      break;
    }
  }
  if (u.size())
    sort(all(u));
  if (r.size())
    sort(all(r), cmp);
  if (d.size())
    sort(all(d));
  if (l.size())
    sort(all(l), cmp);
  double ans = INF;
  rep(i, u.size()) {
    auto itr = lower_bound(all(d), u[i]);
    if (u[i].first == itr->first) {
      chmin(ans, double(itr->second - u[i].second) / 2);
    }
  }
  rep(i, r.size()) {
    auto itr = lower_bound(all(l), r[i], cmp);
    if (r[i].second == itr->second) {
      chmin(ans, double(itr->first - r[i].first) / 2);
    }
  }

  vector<P> h, g;
  rep(i, u.size()) { h.emplace_back(u[i].first + u[i].second, u[i].first); }
  sort(all(h));
  rep(i, r.size()) { g.emplace_back(r[i].first + r[i].second, r[i].first); }
  rep(i, g.size()) {
    auto itr = lower_bound(all(h), g[i]);
    if (itr != h.end() and g[i].first == itr->first) {
      chmin(ans, -g[i].second + itr->second);
    }
  }
  h.clear();
  g.clear();

  rep(i, d.size()) { h.emplace_back(d[i].first + d[i].second, d[i].first); }
  rep(i, l.size()) { g.emplace_back(l[i].first + l[i].second, l[i].first); }
  sort(all(g));
  rep(i, h.size()) {
    auto itr = lower_bound(all(g), h[i]);
    if (itr != g.end() and h[i].first == itr->first) {
      chmin(ans, -h[i].second + itr->second);
    }
  }
  h.clear();
  g.clear();

  rep(i, u.size()) { h.emplace_back(u[i].first - u[i].second, u[i].first); }
  rep(i, l.size()) { g.emplace_back(l[i].first - l[i].second, l[i].first); }
  sort(all(g));
  rep(i, h.size()) {
    auto itr = lower_bound(all(g), h[i]);
    if (itr != g.end() and h[i].first == itr->first) {
      chmin(ans, -h[i].second + itr->second);
    }
  }
  h.clear();
  g.clear();

  rep(i, d.size()) { h.emplace_back(d[i].first - d[i].second, d[i].first); }
  sort(all(h));
  rep(i, r.size()) { g.emplace_back(r[i].first - r[i].second, r[i].first); }
  rep(i, g.size()) {
    auto itr = lower_bound(all(h), g[i]);
    if (itr != h.end() and g[i].first == itr->first) {
      chmin(ans, -g[i].second + itr->second);
    }
  }
  h.clear();
  g.clear();

  if (ans > INF - 1) {
    cout << "SAFE" << endl;
  } else {
    cout << int(ans * 10 + 0.1) << endl;
  }
}