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

#include <bits/stdc++.h>
#define rep(a, n) for (ll a = 0; a < (n); ++a)
using namespace std;
using ll = long long;
typedef pair<ll, char> P;
typedef pair<P, ll> PP;
typedef vector<vector<ll>> Graph;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1e18;

ll n;
vector<ll> x, y;
vector<char> u;

void Rotate() {
  rep(i, n) {
    // 方向変換
    char nu;
    if (u[i] == 'U')
      nu = 'R';
    if (u[i] == 'R')
      nu = 'D';
    if (u[i] == 'D')
      nu = 'L';
    if (u[i] == 'L')
      nu = 'U';
    // 座標変換
    ll nx = y[i];
    ll ny = 200000 - x[i];
    y[i] = ny;
    x[i] = nx;
    u[i] = nu;
  }
  return;
}

ll solve1() {
  ll res = INF;
  vector<vector<P>> v1(200010);
  rep(i, n) {
    ll now = x[i];
    v1[now].push_back({y[i], u[i]});
  }
  rep(i, 200010) {
    ll l = v1[i].size();
    if (l <= 1)
      continue;
    sort(v1[i].begin(), v1[i].end());
    rep(j, l - 1) {
      if (v1[i][j].second == 'U' && v1[i][j + 1].second == 'D') {
        if (v1[i][j].first < v1[i][j + 1].first) {
          chmin(res, (v1[i][j + 1].first - v1[i][j].first) * 5);
        }
      }
    }
  }
  return res;
}

ll solve2() {
  ll res = INF;
  vector<vector<P>> v2(400010);
  rep(i, n) {
    ll now = x[i] + y[i];
    v2[now].push_back({y[i], u[i]});
  }
  rep(i, 400010) {
    ll l = v2[i].size();
    if (l <= 1)
      continue;
    sort(v2[i].begin(), v2[i].end());
    rep(j, l - 1) {
      if (v2[i][j].second == 'U' && v2[i][j + 1].second == 'R') {
        if (v2[i][j].first < v2[i][j + 1].first) {
          chmin(res, (v2[i][j + 1].first - v2[i][j].first) * 10);
        }
      }
    }
  }
  return res;
}

int main() {
  cin >> n;
  x.resize(n);
  y.resize(n);
  u.resize(n);
  rep(i, n) cin >> x[i] >> y[i] >> u[i];
  ll ans = INF;
  rep(t, 4) {
    ll val1 = solve1();
    chmin(ans, val1);
    ll val2 = solve2();
    chmin(ans, val2);
    Rotate();
  }
  if (ans == INF)
    cout << "SAFE" << endl;
  else
    cout << ans << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(a, n) for (ll a = 0; a < (n); ++a)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<P, ll> PP;
typedef vector<vector<ll>> Graph;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1e18;

int main() {
  ll ans = INF;
  ll n;
  cin >> n;
  vector<P> U, R, D, L;
  rep(i, n) {
    ll x, y;
    cin >> x >> y;
    char u;
    cin >> u;
    if (u == 'U') {
      U.push_back({x, y});
    }
    if (u == 'D') {
      D.push_back({x, y});
    }
    if (u == 'L') {
      L.push_back({x, y});
    } else {
      R.push_back({x, y});
    }
  }
  vector<P> DL(200001, {0, 0});
  rep(i, D.size()) {
    P now = D[i];
    DL[now.first + now.second].first++;
    DL[now.first + now.second].second++;

    now = L[i];
    DL[now.first + now.second].first++;
  }
  /*
  rep(i,200001){

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