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

#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
int x[220000], y[220000];
char u[220000];
vector<P> ld[880000], lr[440000];
const int INF = 1001001001;
struct Data {
  int x, y, dir;
};
signed main() {
  int n;
  cin >> n;
  rep(i, n) cin >> x[i] >> y[i] >> u[i];
  vector<Data> res;
  rep(i, n) {
    if (u[i] == 'U')
      res.push_back((Data){x[i], y[i], 3});
    if (u[i] == 'R')
      res.push_back((Data){x[i], y[i], 2});
    if (u[i] == 'L')
      res.push_back((Data){x[i], y[i], 0});
    if (u[i] == 'D')
      res.push_back((Data){x[i], y[i], 1});
  }
  auto rot90 = [&]() {
    // rep(i,n){
    // 	int &_x = res[i].x;
    // 	int &_y = res[i].y;
    // 	int &_dir = res[i].dir;
    // 	int tmp = _x;
    // 	_x = -_y;
    // 	_y = tmp;
    // 	_dir = (_dir+1)%4;
    // }
    rep(i, n) {
      int tmp = res[i].x;
      res[i].x = -res[i].y;
      res[i].y = tmp;
      res[i].dir = (res[i].dir + 1) % 4;
    }
  };
  int ret = INF;
  rep(_, 4) {
    // L,D 0,1
    rep(i, 880000) ld[i].clear();
    rep(i, n) {
      int _x = res[i].x, _y = res[i].y, _dir = res[i].dir;
      if (_dir == 0 || _dir == 1)
        ld[_x + _y + 440000].emplace_back(_y, _dir);
    }
    rep(i, 880000) {
      if (ld[i].size() == 0)
        continue;
      sort(ld[i].begin(), ld[i].end());
      rep(j, (int)ld[i].size() - 1) {
        if (ld[i][j].second == 0 && ld[i][j + 1].second == 1) {
          int dist = ld[i][j + 1].first - ld[i][j].first;
          int time = dist * 10;
          ret = min(ret, time);
        }
      }
    }
    // L,R 0,2
    rep(i, 440000) lr[i].clear();
    rep(i, n) {
      int _x = res[i].x, _y = res[i].y, _dir = res[i].dir;
      if (_dir == 0 || _dir == 2)
        lr[_y + 220000].emplace_back(_x, _dir);
    }
    rep(i, 440000) {
      if (lr[i].size() == 0)
        continue;
      sort(lr[i].begin(), lr[i].end());
      rep(j, (int)lr[i].size() - 1) {
        if (lr[i][j].second == 2 && lr[i][j + 1].second == 0) {
          int dist = lr[i][j + 1].first - lr[i][j].first;
          int time = dist * 5;
          ret = min(ret, time);
        }
      }
    }
    // trace(ret);
    rot90();
  }

  if (ret == INF)
    cout << "SAFE" << endl;
  else
    cout << ret << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
int x[220000], y[220000];
char u[220000];
vector<P> ld[440000], lr[220000];
const int INF = 1001001001;
struct Data {
  int x, y, dir;
};
signed main() {
  int n;
  cin >> n;
  rep(i, n) cin >> x[i] >> y[i] >> u[i];
  vector<Data> res;
  rep(i, n) {
    if (u[i] == 'U')
      res.push_back((Data){x[i], y[i], 3});
    if (u[i] == 'R')
      res.push_back((Data){x[i], y[i], 2});
    if (u[i] == 'L')
      res.push_back((Data){x[i], y[i], 0});
    if (u[i] == 'D')
      res.push_back((Data){x[i], y[i], 1});
  }
  auto rot90 = [&]() {
    // rep(i,n){
    // 	int &_x = res[i].x;
    // 	int &_y = res[i].y;
    // 	int &_dir = res[i].dir;
    // 	int tmp = _x;
    // 	_x = -_y;
    // 	_y = tmp;
    // 	_dir = (_dir+1)%4;
    // }
    rep(i, n) {
      int tmp = res[i].x;
      res[i].x = -res[i].y;
      res[i].y = tmp;
      res[i].dir = (res[i].dir + 1) % 4;
    }
  };
  int ret = INF;
  rep(_, 4) {
    // L,D 0,1
    rep(i, 440000) ld[i].clear();
    rep(i, n) {
      int _x = res[i].x, _y = res[i].y, _dir = res[i].dir;
      if (_dir == 0 || _dir == 1)
        ld[_x + _y].emplace_back(_y, _dir);
    }
    rep(i, 440000) {
      if (ld[i].size() == 0)
        continue;
      sort(ld[i].begin(), ld[i].end());
      rep(j, (int)ld[i].size() - 1) {
        if (ld[i][j].second == 0 && ld[i][j + 1].second == 1) {
          int dist = ld[i][j + 1].first - ld[i][j].first;
          int time = dist * 10;
          ret = min(ret, time);
        }
      }
    }
    // L,R 0,2
    rep(i, 220000) lr[i].clear();
    rep(i, n) {
      int _x = res[i].x, _y = res[i].y, _dir = res[i].dir;
      if (_dir == 0 || _dir == 2)
        lr[_y].emplace_back(_x, _dir);
    }
    rep(i, 220000) {
      if (lr[i].size() == 0)
        continue;
      sort(lr[i].begin(), lr[i].end());
      rep(j, (int)lr[i].size() - 1) {
        if (lr[i][j].second == 2 && lr[i][j + 1].second == 0) {
          int dist = lr[i][j + 1].first - lr[i][j].first;
          int time = dist * 5;
          ret = min(ret, time);
        }
      }
    }
    rot90();
  }

  if (ret == INF)
    cout << "SAFE" << endl;
  else
    cout << ret << endl;
  return 0;
}