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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define trav(e, x) for (auto &e : x)
#define eb(x...) emplace_back(x)
#define all(x) begin(x), end(x)
#define sz(x) int((x).size())

template <class T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

const int N = 1 << 18, inf = 1e9;
char dir[N];
int x[N], y[N];

#define f first
#define s second

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;

  map<int, vector<pair<int, char>>> diagl, diagr, horiz, vert;
  rep(i, 0, n) {
    cin >> x[i] >> y[i] >> dir[i];
    x[i] *= 10;
    y[i] *= 10;
    diagl[x[i] + y[i]].eb(x[i], dir[i]);
    diagr[x[i] - y[i]].eb(x[i], dir[i]);
    horiz[y[i]].eb(x[i], dir[i]);
    vert[x[i]].eb(y[i], dir[i]);
  }

  int ans = inf + 1;
  trav(z, diagl) {
    auto &v = z.s;
    sort(all(v));
    rep(i, 0, sz(v) - 1) if ((v[i].s == 'R' && v[i + 1].s == 'U') ||
                             (v[i].s == 'D' && v[i + 1].s == 'L'))
        ckmin(ans, v[i + 1].f - v[i].f);
  }

  trav(z, diagr) {
    auto &v = z.s;
    sort(all(v));
    rep(i, 0, sz(v) - 1) if ((v[i].s == 'U' && v[i + 1].s == 'L') ||
                             (v[i].s == 'R' && v[i + 1].s == 'D'))
        ckmin(ans, v[i + 1].f - v[i].f);
  }

  trav(z, horiz) {
    auto &v = z.s;
    sort(all(v));
    rep(i, 0, sz(v) - 1) if (v[i].s == 'R' && v[i + 1].s == 'L')
        ckmin(ans, (v[i + 1].f - v[i].f) / 2);
  }

  trav(z, vert) {
    auto &v = z.s;
    sort(all(v));
    rep(i, 0, sz(v) - 1) if (v[i].s == 'U' && v[i + 1].s == 'D')
        ckmin(ans, (v[i + 1].f - v[i].f) / 2);
  }

  if (ans > inf)
    cout << "SAFE";
  else
    cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define trav(e, x) for (auto &e : x)
#define eb(x...) emplace_back(x)
#define all(x) begin(x), end(x)
#define sz(x) int((x).size())

template <class T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

using point = complex<ll>;
const int N = 1 << 18, inf = 1e9;
map<char, point> d = {{'U', point(0, +1)},
                      {'D', point(0, -1)},
                      {'L', point(-1, 0)},
                      {'R', point(+1, 0)}};
point p[N];
char dir[N];

struct event {
  point p;
  bool right;
  event() {}
  event(point p, bool r) : p{p}, right{r} {}
  bool operator<(event e) {
    if (real(e.p) == real(p))
      return !right;
    return real(p) < real(e.p);
  }
};

struct vert {
  ll x, u, d;
  vert() {}
  vert(point a, point b) {
    assert(real(a) == real(b));
    x = real(a);
    u = imag(a);
    d = imag(b);
    if (u < d)
      swap(u, d);
  }
  bool operator<(vert v) {
    if (x == v.x)
      return d < v.d;
    return x < v.x;
  }
};

int n;
bool f(ll t) {
  vector<event> E;
  vector<vert> v;
  map<ll, vector<pair<ll, ll>>> h;

  rep(i, 0, n) {
    if (dir[i] == 'L') {
      E.eb(p[i] + d['L'] * t, 0), E.eb(p[i], 1);
      h[imag(p[i])].eb(real(p[i] + d['L'] * t), real(p[i]));
    } else if (dir[i] == 'R') {
      E.eb(p[i], 0), E.eb(p[i] + d['R'] * t, 1);
      h[imag(p[i])].eb(real(p[i]), real(p[i] + d['R'] * t));
    } else
      v.eb(p[i], p[i] + d[dir[i]] * t);
  }

  sort(all(E));
  sort(all(v));

  multiset<ll> height;
  auto erase = [&](ll z) {
    auto it = height.find(z);
    assert(it != end(height));
    height.erase(it);
  };

  int x = 0;
  trav(z, v) {
    while (x < sz(E) &&
           (real(E[x].p) < z.x || (real(E[x].p) == z.x && !E[x].right))) {
      if (E[x].right)
        erase(imag(E[x].p));
      else
        height.insert(imag(E[x].p));
      ++x;
    }

    auto it = height.lower_bound(z.d);
    if (it != end(height) && *it <= z.u)
      return true;
  }

  int i = 0;
  while (i < sz(v)) {
    int j = i;
    vector<vert> z;
    while (j < sz(v) && v[i].x == v[j].x)
      z.eb(v[j]), ++j;
    rep(k, 0, sz(z) - 1) if (z[k + 1].d <= z[k].u) return true;
    i = j;
  }

  trav(z, h) {
    auto &v = z.second;
    sort(all(v));
    rep(i, 0, sz(v) - 1) if (v[i].second >= v[i + 1].first) return true;
  }

  return false;
}

int solve() {
  int l = 0, r = inf;
  while (r - l > 1) {
    int m = l + (r - l + 1) / 2;
    if (f(m))
      r = m;
    else
      l = m;
  }
  if (f(r))
    return r;
  else
    return inf + 1;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n;
  rep(i, 0, n) {
    int x, y;
    char c;
    cin >> x >> y >> dir[i];
    x *= 10;
    y *= 10;
    p[i] = {x, y};
  }
  int res = solve();
  if (res > inf)
    cout << "SAFE";
  else
    cout << res;
}