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

// warm heart, wagging tail,and a smile just for you!
//                                                                     ███████████
//                                                                   ███╬╬╬╬╬╬╬╬╬╬███
//                                                                ███╬╬╬╬╬████╬╬╬╬╬╬███
//                                            ███████████
//                                            ██╬╬╬╬╬████╬╬████╬╬╬╬╬██
//                                      █████████╬╬╬╬╬████████████╬╬╬╬╬██╬╬╬╬╬╬███╬╬╬╬╬██
//                               ████████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬██╬╬╬╬╬╬╬██
//                             ████╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬╬╬╬╬╬██
//                           ███╬╬╬█╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬███╬╬╬╬╬╬╬█████
//                         ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬████████╬╬╬╬╬██
//                       ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬╬╬╬╬███
//                     ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬██
//                 ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬████
//     █████████████╬╬╬╬╬╬╬╬██╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬██████
//   ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬██████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████╬╬╬╬╬╬╬███████████╬╬╬╬╬╬╬╬██╬╬╬██╬╬╬██
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬█╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬██
// ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬▓▓▓▓▓▓╬╬╬████╬╬████╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬███
// ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬█████
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████████
//   ███╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██
//       ██████████████
//       ████╬╬╬╬╬╬███████████████████████████╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████
//                         ███████                           █████
//                         ███████████████████
//
#include "bits/stdc++.h"
using namespace std;
#define INF (1 << 30)
#define LINF (1LL << 60)
#define fs first
#define sc second
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR2(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define RFOR2(i, a, b) for (int i = (b); i >= (a); --i)
#define REP(i, n) FOR(i, 0, (n))
#define REP2(i, n) FOR2(i, 0, (n))
#define RREP(i, n) RFOR(i, 0, (n))
#define RREP2(i, n) RFOR2(i, 0, (n))
#define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i, a, b) ((a) <= (i) && (i) < (b))
#define debug(x) cout << #x << " = " << (x) << endl
#define SP << " " <<
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
#define MSB(x) (63 - __builtin_clzll(x))
#define pcnt(x) (__builtin_popcountll(x))
#define parity(i, j) (i & (1LL << j))
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vec;
typedef vector<vector<int>> mat;

vec idx[202020], idy[202020], idz[404040], idw[404040];
void solve() {
  int N;
  cin >> N;

  vec x(N), y(N);
  vector<char> c(N);
  REP(i, N) {
    cin >> x[i] >> y[i] >> c[i];
    idx[x[i]].push_back(i);
    idy[y[i]].push_back(i);
    idz[x[i] + y[i]].push_back(i);
    idw[y[i] - x[i] + 200000].push_back(i);

    // cout << x[i]+y[i] SP x[i]-y[i] << endl;
  }

  int ans = INF;
  // jouge
  REP(i, 202020) {
    vec r, d, u, l;
    for (int v : idx[i]) {
      if (c[v] == 'D')
        d.push_back(y[v]);
      if (c[v] == 'U')
        u.push_back(y[v]);
    }
    for (int v : idy[i]) {
      if (c[v] == 'R')
        r.push_back(x[v]);
      if (c[v] == 'L')
        l.push_back(x[v]);
    }
    sort(r.begin(), r.end());
    sort(d.begin(), d.end());
    sort(u.begin(), u.end());
    sort(l.begin(), l.end());
    int in = 0;
    REP(j, r.size()) {
      while (in < l.size() && r[j] > l[in])
        in++;
      if (in < l.size())
        chmin(ans, (l[in] - r[j]) * 5);
    }

    in = 0;
    REP(j, u.size()) {
      while (in < d.size() && u[j] > d[in])
        in++;
      if (in < d.size())
        chmin(ans, (d[in] - u[j]) * 5);
    }
  }

  // debug(ans);

  // taikaku
  REP(i, 404040) {
    vec r, d, u, l;
    for (int v : idw[i]) {
      if (c[v] == 'R')
        r.push_back(x[v]);
      if (c[v] == 'D')
        d.push_back(x[v]);
      if (c[v] == 'U')
        u.push_back(x[v]);
      if (c[v] == 'L')
        l.push_back(x[v]);
    }
    sort(r.begin(), r.end());
    sort(d.begin(), d.end());
    sort(u.begin(), u.end());
    sort(l.begin(), l.end());

    int in = 0;
    REP(j, r.size()) {
      while (in < d.size() && r[j] > d[in])
        in++;
      if (in < d.size())
        chmin(ans, (d[in] - r[j]) * 10);
    }

    in = 0;
    REP(j, u.size()) {
      while (in < l.size() && u[j] > l[in])
        in++;
      if (in < l.size())
        chmin(ans, (l[in] - u[j]) * 10);
    }
  }
  // debug(ans);

  REP(i, 404040) {
    vec r, d, u, l;
    for (int v : idz[i]) {
      if (c[v] == 'R')
        r.push_back(x[v]);
      if (c[v] == 'D')
        d.push_back(x[v]);
      if (c[v] == 'U')
        u.push_back(x[v]);
      if (c[v] == 'L')
        l.push_back(x[v]);
    }
    sort(r.begin(), r.end());
    sort(d.begin(), d.end());
    sort(u.begin(), u.end());
    sort(l.begin(), l.end());

    int in = 0;
    REP(j, r.size()) {
      while (in < u.size() && r[j] > u[in])
        in++;
      if (in < u.size())
        chmin(ans, (u[in] - r[j]) * 10);
    }

    in = 0;
    REP(j, d.size()) {
      while (in < l.size() && d[j] > l[in])
        in++;
      if (in < l.size())
        chmin(ans, (l[in] - d[j]) * 10);
    }
  }

  assert(ans > 0);
  if (ans == INF)
    cout << "SAFE" << endl;
  else
    cout << ans << endl;
}

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

  int T = 1;
  // cin >> T;

  while (T--)
    solve();

  return 0;
}

Error: WA

Fixed submission:

// warm heart, wagging tail,and a smile just for you!
//                                                                     ███████████
//                                                                   ███╬╬╬╬╬╬╬╬╬╬███
//                                                                ███╬╬╬╬╬████╬╬╬╬╬╬███
//                                            ███████████
//                                            ██╬╬╬╬╬████╬╬████╬╬╬╬╬██
//                                      █████████╬╬╬╬╬████████████╬╬╬╬╬██╬╬╬╬╬╬███╬╬╬╬╬██
//                               ████████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬██╬╬╬╬╬╬╬██
//                             ████╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬╬╬╬╬╬██
//                           ███╬╬╬█╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬███╬╬╬╬╬╬╬█████
//                         ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬████████╬╬╬╬╬██
//                       ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬╬╬╬╬███
//                     ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬██
//                 ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬████
//     █████████████╬╬╬╬╬╬╬╬██╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬██████
//   ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬██████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████╬╬╬╬╬╬╬███████████╬╬╬╬╬╬╬╬██╬╬╬██╬╬╬██
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬█╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬██
// ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬▓▓▓▓▓▓╬╬╬████╬╬████╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬███
// ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬█████
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████████
//   ███╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██
//       ██████████████
//       ████╬╬╬╬╬╬███████████████████████████╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████
//                         ███████                           █████
//                         ███████████████████
//
#include "bits/stdc++.h"
using namespace std;
#define INF (1 << 30)
#define LINF (1LL << 60)
#define fs first
#define sc second
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR2(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define RFOR2(i, a, b) for (int i = (b); i >= (a); --i)
#define REP(i, n) FOR(i, 0, (n))
#define REP2(i, n) FOR2(i, 0, (n))
#define RREP(i, n) RFOR(i, 0, (n))
#define RREP2(i, n) RFOR2(i, 0, (n))
#define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i, a, b) ((a) <= (i) && (i) < (b))
#define debug(x) cout << #x << " = " << (x) << endl
#define SP << " " <<
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
#define MSB(x) (63 - __builtin_clzll(x))
#define pcnt(x) (__builtin_popcountll(x))
#define parity(i, j) (i & (1LL << j))
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vec;
typedef vector<vector<int>> mat;

vec idx[202020], idy[202020], idz[404040], idw[404040];
void solve() {
  int N;
  cin >> N;

  vec x(N), y(N);
  vector<char> c(N);
  REP(i, N) {
    cin >> x[i] >> y[i] >> c[i];
    idx[x[i]].push_back(i);
    idy[y[i]].push_back(i);
    idz[x[i] + y[i]].push_back(i);
    idw[y[i] - x[i] + 200000].push_back(i);

    // cout << x[i]+y[i] SP x[i]-y[i] << endl;
  }

  int ans = INF;
  // jouge
  REP(i, 202020) {
    int mn = INF, mx = -INF;
    for (int v : idx[i]) {
      if (c[v] == 'D')
        chmin(mn, y[v]);
      if (c[v] == 'U')
        chmax(mx, y[v]);
    }
    if (mn != INF && mx != -INF && mn > mx)
      chmin(ans, (mn - mx) * 5);
    mn = INF, mx = -INF;
    for (int v : idy[i]) {
      if (c[v] == 'L')
        chmin(mn, x[v]);
      if (c[v] == 'R')
        chmax(mx, x[v]);
    }
    if (mn != INF && mx != -INF && mn > mx)
      chmin(ans, (mn - mx) * 5);
  }

  // debug(ans);

  // taikaku
  REP(i, 404040) {
    vec r, d, u, l;
    for (int v : idw[i]) {
      if (c[v] == 'R')
        r.push_back(x[v]);
      if (c[v] == 'D')
        d.push_back(x[v]);
      if (c[v] == 'U')
        u.push_back(x[v]);
      if (c[v] == 'L')
        l.push_back(x[v]);
    }
    sort(r.begin(), r.end());
    sort(d.begin(), d.end());
    sort(u.begin(), u.end());
    sort(l.begin(), l.end());

    int in = 0;
    REP(j, r.size()) {
      while (in < d.size() && r[j] > d[in])
        in++;
      if (in < d.size())
        chmin(ans, (d[in] - r[j]) * 10);
    }

    in = 0;
    REP(j, u.size()) {
      while (in < l.size() && u[j] > l[in])
        in++;
      if (in < l.size())
        chmin(ans, (l[in] - u[j]) * 10);
    }
  }
  // debug(ans);

  REP(i, 404040) {
    vec r, d, u, l;
    for (int v : idz[i]) {
      if (c[v] == 'R')
        r.push_back(x[v]);
      if (c[v] == 'D')
        d.push_back(x[v]);
      if (c[v] == 'U')
        u.push_back(x[v]);
      if (c[v] == 'L')
        l.push_back(x[v]);
    }
    sort(r.begin(), r.end());
    sort(d.begin(), d.end());
    sort(u.begin(), u.end());
    sort(l.begin(), l.end());

    int in = 0;
    REP(j, r.size()) {
      while (in < u.size() && r[j] > u[in])
        in++;
      if (in < u.size())
        chmin(ans, (u[in] - r[j]) * 10);
    }

    in = 0;
    REP(j, d.size()) {
      while (in < l.size() && d[j] > l[in])
        in++;
      if (in < l.size())
        chmin(ans, (l[in] - d[j]) * 10);
    }
  }

  if (ans == INF)
    assert(false);
  else
    cout << "SAFE" << endl;
}

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

  int T = 1;
  // cin >> T;

  while (T--)
    solve();

  return 0;
}