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


#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << endl; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int N;
  cin >> N;

  vi X(N), Y(N);
  vector<char> U(N);
  REP(i, N) cin >> X[i] >> Y[i] >> U[i];

  map<ll, set<ll>> mr_down, mr_up, ml_down, ml_up;

  ll ans = inf;

  vector<pair<int, pair<int, bool>>> vr, vl;
  map<int, set<ll>> mu, md, ml, mr;
  REP(i, N) {
    if (U[i] == 'U') {
      mr_up[Y[i] + X[i]].insert(Y[i]);
      ml_up[Y[i] - X[i]].insert(Y[i]);
      vr.pb({X[i], {i, false}});
      vl.pb({X[i], {i, false}});

      mu[X[i]].insert(Y[i]);
      auto &tmp = md[X[i]];
      if (tmp.empty())
        continue;
      auto it = tmp.lower_bound(Y[i]);
      if (it != tmp.end()) {
        ans = min(ans, (*it - Y[i]) * 10 / 2);
      }
    } else if (U[i] == 'D') {
      mr_down[Y[i] - X[i]].insert(Y[i]);
      ml_down[Y[i] + X[i]].insert(Y[i]);
      vr.pb({X[i], {i, false}});
      vl.pb({X[i], {i, false}});

      md[X[i]].insert(Y[i]);
      auto &tmp = mu[X[i]];
      if (tmp.empty())
        continue;
      auto it = tmp.lower_bound(Y[i]);
      if (it != tmp.begin()) {
        it--;
        ans = min(ans, (Y[i] - *it) * 10 / 2);
      }
    } else if (U[i] == 'L') {
      vl.pb({X[i], {i, true}});

      ml[Y[i]].insert(X[i]);
      auto &tmp = mr[Y[i]];
      if (tmp.empty())
        continue;
      auto it = tmp.lower_bound(X[i]);
      if (it != tmp.begin()) {
        it--;
        ans = min(ans, (X[i] - *it) * 10 / 2);
      }
    } else {
      vr.pb({X[i], {i, true}});

      mr[Y[i]].insert(X[i]);
      auto &tmp = ml[Y[i]];
      if (tmp.empty())
        continue;
      auto it = tmp.lower_bound(X[i]);
      if (it != tmp.end()) {
        ans = min(ans, (*it - X[i]) * 10 / 2);
      }
    }
  }

  sort(ALL(vr));
  sort(ALL(vl), greater<>());

  debug(mr_down, mr_up) REP(i, vr.size()) {
    auto pp = vr[i];
    int id = pp.se.fi;
    bool f = pp.se.se;

    if (f) {
      debug(Y[id] - X[id]) auto &s1 = mr_down[Y[id] - X[id]];
      if (s1.size() > 0) {
        ans = min(ans, 10 * (*s1.begin() - Y[id]));
      }

      debug(Y[id] + X[id]) auto &s2 = mr_up[Y[id] + X[id]];
      if (s2.size() > 0) {
        ans = min(ans, 10 * (Y[id] - *s2.rbegin()));
      }
    } else {
      mr_down[Y[id] - X[id]].erase(Y[id]);
      mr_up[Y[id] + X[id]].erase(Y[id]);
    }
  }

  debug(ml_down, ml_up) REP(i, vl.size()) {
    auto pp = vl[i];
    int id = pp.se.fi;
    bool f = pp.se.se;

    debug(pp)

        if (f) {
      auto &s1 = ml_down[Y[id] + X[id]];
      debug(Y[id] + X[id], s1);
      if (s1.size() > 0) {
        ans = min(ans, 10 * (*s1.begin() - Y[id]));
      }

      debug(Y[id] - X[id]);
      auto &s2 = ml_up[Y[id] - X[id]];
      if (s2.size() > 0) {
        ans = min(ans, 10 * (Y[id] - *s2.rbegin()));
      }
    }
    else {
      ml_down[Y[id] + X[id]].erase(Y[id]);
      ml_up[Y[id] - X[id]].erase(Y[id]);
    }

    debug(ans)
  }

  if (ans == inf)
    print("SAFE");
  else
    print(ans);
}


Error: 0

Fixed submission:


#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << endl; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int N;
  cin >> N;

  vi X(N), Y(N);
  vector<char> U(N);
  REP(i, N) cin >> X[i] >> Y[i] >> U[i];

  map<ll, set<ll>> mr_down, mr_up, ml_down, ml_up;

  ll ans = inf;

  vector<pair<int, pair<int, bool>>> vr, vl;
  map<int, set<ll>> mu, md, ml, mr;
  REP(i, N) {
    if (U[i] == 'U') {
      mr_up[Y[i] + X[i]].insert(Y[i]);
      ml_up[Y[i] - X[i]].insert(Y[i]);
      vr.pb({X[i], {i, false}});
      vl.pb({X[i], {i, false}});

      mu[X[i]].insert(Y[i]);
      auto &tmp = md[X[i]];
      if (tmp.empty())
        continue;
      auto it = tmp.lower_bound(Y[i]);
      if (it != tmp.end()) {
        ans = min(ans, (*it - Y[i]) * 10 / 2);
      }
    } else if (U[i] == 'D') {
      mr_down[Y[i] - X[i]].insert(Y[i]);
      ml_down[Y[i] + X[i]].insert(Y[i]);
      vr.pb({X[i], {i, false}});
      vl.pb({X[i], {i, false}});

      md[X[i]].insert(Y[i]);
      auto &tmp = mu[X[i]];
      if (tmp.empty())
        continue;
      auto it = tmp.lower_bound(Y[i]);
      if (it != tmp.begin()) {
        it--;
        ans = min(ans, (Y[i] - *it) * 10 / 2);
      }
    } else if (U[i] == 'L') {
      vl.pb({X[i], {i, true}});

      ml[Y[i]].insert(X[i]);
      auto &tmp = mr[Y[i]];
      if (tmp.empty())
        continue;
      auto it = tmp.lower_bound(X[i]);
      if (it != tmp.begin()) {
        it--;
        ans = min(ans, (X[i] - *it) * 10 / 2);
      }
    } else {
      vr.pb({X[i], {i, true}});

      mr[Y[i]].insert(X[i]);
      auto &tmp = ml[Y[i]];
      if (tmp.empty())
        continue;
      auto it = tmp.lower_bound(X[i]);
      if (it != tmp.end()) {
        ans = min(ans, (*it - X[i]) * 10 / 2);
      }
    }
  }

  sort(ALL(vr));
  sort(ALL(vl), greater<>());

  debug(mr_down, mr_up) REP(i, vr.size()) {
    auto pp = vr[i];
    int id = pp.se.fi;
    bool f = pp.se.se;

    if (f) {
      debug(Y[id] - X[id]) auto &s1 = mr_down[Y[id] - X[id]];
      if (s1.size() > 0) {
        ans = min(ans, 10 * (*s1.begin() - Y[id]));
      }

      debug(Y[id] + X[id]) auto &s2 = mr_up[Y[id] + X[id]];
      if (s2.size() > 0) {
        ans = min(ans, 10 * (Y[id] - *s1.rbegin()));
      }
    } else {
      mr_down[Y[id] - X[id]].erase(Y[id]);
      mr_up[Y[id] + X[id]].erase(Y[id]);
    }
  }

  debug(ml_down, ml_up) REP(i, vl.size()) {
    auto pp = vl[i];
    int id = pp.se.fi;
    bool f = pp.se.se;

    debug(pp)

        if (f) {
      auto &s1 = ml_down[Y[id] + X[id]];
      debug(Y[id] + X[id], s1);
      if (s1.size() > 0) {
        ans = min(ans, 10 * (*s1.begin() - Y[id]));
      }

      debug(Y[id] - X[id]);
      auto &s2 = mr_up[Y[id] - X[id]];
      if (s2.size() > 0) {
        ans = min(ans, 10 * (Y[id] - *s1.rbegin()));
      }
    }
    else {
      mr_down[Y[id] + X[id]].erase(Y[id]);
      mr_up[Y[id] - X[id]].erase(Y[id]);
    }
  }

  if (ans == inf)
    print("SAFE");
  else
    print(ans);
}