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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

ll N;
vector<ll> X, Y;
vector<char> U;
const string DIRECTION = "LDRU";
const ll POS_MAX = 200000;
const ll INF = 1e18;

void rotate() {
  REP(i, N) {
    ll m = POS_MAX / 2, x = X[i] - m, y = Y[i] - m;
    X[i] = -y + m, Y[i] = x + m;
    // DUMP(X[i]);
    // DUMP(Y[i]);
    REP(j, 4) {
      if (DIRECTION[j] == U[i]) {
        U[i] = DIRECTION[(j + 1) % 4];
        break;
      }
    }
    // DUMP(U[i]);
  }
}

ll solveForLine() {
  ll res = INF;
  // when L, R
  vector<vector<ll>> XsL(POS_MAX + 1), XsR(POS_MAX + 1);
  REP(i, N) {
    if (U[i] == 'L') {
      XsL[Y[i]].push_back(X[i]);
    } else if (U[i] == 'R') {
      XsR[Y[i]].push_back(X[i]);
    }
  }
  REP(i, POS_MAX + 1) {
    if (SZ(XsL[i]) == 0 || SZ(XsR[i]) == 0) {
      continue;
    }
    sort(ALL(XsL[i]));
    sort(ALL(XsR[i]));
    for (ll xr : XsR[i]) {
      // min x s.t. x > xr and x in XsL[i]
      if (XsL[i][0] > xr) {
        chmin(res, (-xr + XsL[i][0]) * 5);
        continue;
      }
      if (XsL[i].back() <= xr) {
        continue;
      }
      ll ng = 0, ok = SZ(XsL[i]) - 1;
      while (ng + 1 < ok) {
        ll m = (ok + ng) / 2;
        (XsL[i][m] > xr ? ok : ng) = m;
      }
      chmin(res, (-xr + XsL[i][ok]) * 5);
    }
  }
  return res;
}

ll solveForVertical() {
  ll res = INF;
  // when R, U
  ll sz = 2 * POS_MAX + 1;
  vector<vector<ll>> XsR(sz), XsU(sz);
  REP(i, N) {
    if (U[i] == 'R') {
      XsR[X[i] + Y[i]].push_back(X[i]);
    } else if (U[i] == 'U') {
      XsU[X[i] + Y[i]].push_back(X[i]);
    }
  }
  REP(i, sz) {
    if (SZ(XsR[i]) == 0 || SZ(XsU[i]) == 0) {
      continue;
    }
    sort(ALL(XsR[i]));
    sort(ALL(XsU[i]));
    for (ll xr : XsR[i]) {
      // min x s.t. x > xr and x in XsU[i]
      if (XsU[i][0] > xr) {
        chmin(res, (-xr + XsU[i][0]) * 10);
        continue;
      }
      if (XsU[i].back() <= xr) {
        continue;
      }
      ll ng = 0, ok = SZ(XsU[i]) - 1;
      while (ng + 1 < ok) {
        ll m = (ok + ng) / 2;
        (XsU[i][m] > xr ? ok : ng) = m;
      }
      chmin(res, (-xr + XsU[i][ok]) * 10);
    }
  }
  return res;
}

int main() {
  cin >> N;
  X = vector<ll>(N);
  Y = vector<ll>(N);
  U = vector<char>(N);
  REP(i, N) { cin >> X[i] >> Y[i] >> U[i]; }
  ll ans = INF;
  REP(_, 4) {
    chmin(ans, solveForLine());
    chmin(ans, solveForVertical());
    rotate();
  }

  if (ans == INF) {
    cout << "SAFE" << endl;
    return 0;
  }
  cout << ans << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

ll N;
vector<ll> X, Y;
vector<char> U;
const string DIRECTION = "LDRU";
const ll POS_MAX = 200000;
const ll INF = 1e18;

void rotate() {
  REP(i, N) {
    ll m = POS_MAX / 2, x = X[i] - m, y = Y[i] - m;
    X[i] = -y + m, Y[i] = x + m;
    // DUMP(X[i]);
    // DUMP(Y[i]);
    REP(j, 4) {
      if (DIRECTION[j] == U[i]) {
        U[i] = DIRECTION[(j + 1) % 4];
        break;
      }
    }
    // DUMP(U[i]);
  }
}

ll solveForLine() {
  ll res = INF;
  // when L, R
  vector<vector<ll>> XsL(POS_MAX + 1), XsR(POS_MAX + 1);
  REP(i, N) {
    if (U[i] == 'L') {
      XsL[Y[i]].push_back(X[i]);
    } else if (U[i] == 'R') {
      XsR[Y[i]].push_back(X[i]);
    }
  }
  REP(i, POS_MAX + 1) {
    if (SZ(XsL[i]) == 0 || SZ(XsR[i]) == 0) {
      continue;
    }
    sort(ALL(XsL[i]));
    sort(ALL(XsR[i]));
    for (ll xr : XsR[i]) {
      // min x s.t. x > xr and x in XsL[i]
      if (XsL[i][0] > xr) {
        chmin(res, (-xr + XsL[i][0]) * 5);
      }
      if (XsL[i].back() <= xr) {
        continue;
      }
      ll ok = 0, ng = SZ(XsL[i]) - 1;
      while (ok + 1 < ng) {
        ll m = (ok + ng) / 2;
        (XsL[i][m] > xr ? ok : ng) = m;
      }
      chmin(res, (-xr + XsL[i][ok]) * 5);
    }
  }
  return res;
}

ll solveForVertical() {
  ll res = INF;
  // when R, U
  ll sz = 2 * POS_MAX + 1;
  vector<vector<ll>> XsR(sz), XsU(sz);
  REP(i, N) {
    if (U[i] == 'R') {
      XsR[X[i] + Y[i]].push_back(X[i]);
    } else if (U[i] == 'U') {
      XsU[X[i] + Y[i]].push_back(X[i]);
    }
  }
  REP(i, sz + 1) {
    if (SZ(XsR[i]) == 0 || SZ(XsU[i]) == 0) {
      continue;
    }
    sort(ALL(XsR[i]));
    sort(ALL(XsU[i]));
    for (ll xr : XsR[i]) {
      // min x s.t. x > xr and x in XsU[i]
      if (XsU[i][0] > xr) {
        chmin(res, (-xr + XsU[i][0]) * 10);
      }
      if (XsU[i].back() <= xr) {
        continue;
      }
      ll ok = 0, ng = SZ(XsU[i]) - 1;
      while (ok + 1 < ng) {
        ll m = (ok + ng) / 2;
        (XsU[i][m] > xr ? ok : ng) = m;
      }
      chmin(res, (-xr + XsU[i][ok]) * 10);
    }
  }
  return res;
}

int main() {
  cin >> N;
  X = vector<ll>(N);
  Y = vector<ll>(N);
  U = vector<char>(N);
  REP(i, N) { cin >> X[i] >> Y[i] >> U[i]; }
  ll ans = INF;
  REP(_, 4) {
    chmin(ans, solveForLine());
    chmin(ans, solveForVertical());
    rotate();
  }

  if (ans == INF) {
    cout << "SAFE" << endl;
    return 0;
  }
  cout << ans << endl;
}