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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
#define rep(i, n) for (long int i = 0; i < (n); ++i)
using namespace std;

int main() {
  typedef pair<long int, char> P;
  long int N;
  cin >> N;
  vector<long int> X(N), Y(N), a(N), b(N);
  vector<char> U(N);
  vector<vector<P>> aa(400005), bb(400005), cc(200005), dd(200005);
  rep(i, N) {
    cin >> X[i] >> Y[i] >> U[i];
    a[i] = Y[i] - X[i] + 200000;
    b[i] = X[i] + Y[i];
    aa[a[i]].push_back(P(X[i], U[i]));
    bb[b[i]].push_back(P(X[i], U[i]));
    cc[X[i]].push_back(P(Y[i], U[i]));
    dd[Y[i]].push_back(P(X[i], U[i]));
  }
  rep(i, 400005) {
    if (!aa[i].empty())
      sort(aa[i].begin(), aa[i].end());
    if (!bb[i].empty())
      sort(bb[i].begin(), bb[i].end());
  }
  long int ans = 1000000000;
  rep(i, 400005) {
    long int l = aa[i].size();
    rep(j, l - 1) {
      if (aa[i][j].second == 'U' && aa[i][j + 1].second == 'L')
        ans = min(ans, 10 * (aa[i][j + 1].first - aa[i][j].first));
      if (aa[i][j].second == 'R' && aa[i][j + 1].second == 'D')
        ans = min(ans, 10 * (aa[i][j + 1].first - aa[i][j].first));
    }
    l = bb[i].size();
    rep(j, l - 1) {
      if (bb[i][j].second == 'R' && bb[i][j + 1].second == 'U')
        ans = min(ans, 10 * (bb[i][j + 1].first - bb[i][j].first));
      if (bb[i][j].second == 'D' && bb[i][j + 1].second == 'L')
        ans = min(ans, 10 * (bb[i][j + 1].first - bb[i][j].first));
    }
  }
  rep(i, 200005) {
    if (!cc[i].empty())
      sort(cc[i].begin(), cc[i].end());
    if (!dd[i].empty())
      sort(dd[i].begin(), dd[i].end());
  }
  rep(i, 200005) {
    long int l = cc[i].size();
    rep(j, l - 1) {
      if (cc[i][j].second == 'U' && cc[i][j + 1].second == 'D')
        ans = min(ans, 5 * (cc[i][j + 1].first - cc[i][j].first));
    }
    l = dd[i].size();
    rep(j, l - 1) {
      if (dd[i][j].second == 'R' && dd[i][j + 1].second == 'L')
        ans = min(ans, 5 * (dd[i][j + 1].first - dd[i][j].first));
    }
  }
  if (ans == 1000000000)
    cout << "SAFE" << endl;
  else
    cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
#define rep(i, n) for (long int i = 0; i < (n); ++i)
using namespace std;

int main() {
  typedef pair<long int, char> P;
  long int N;
  cin >> N;
  vector<long int> X(N), Y(N), a(N), b(N);
  vector<char> U(N);
  vector<vector<P>> aa(400005), bb(400005), cc(200005), dd(200005);
  rep(i, N) {
    cin >> X[i] >> Y[i] >> U[i];
    a[i] = Y[i] - X[i] + 200000;
    b[i] = X[i] + Y[i];
    aa[a[i]].push_back(P(X[i], U[i]));
    bb[b[i]].push_back(P(X[i], U[i]));
    cc[X[i]].push_back(P(Y[i], U[i]));
    dd[Y[i]].push_back(P(X[i], U[i]));
  }
  rep(i, 400005) {
    if (!aa[i].empty())
      sort(aa[i].begin(), aa[i].end());
    if (!bb[i].empty())
      sort(bb[i].begin(), bb[i].end());
  }
  long int ans = 1000000000;
  rep(i, 400005) {
    long int l = aa[i].size();
    rep(j, l - 1) {
      if (aa[i][j].second == 'U' && aa[i][j + 1].second == 'L')
        ans = min(ans, aa[i][j + 1].first - aa[i][j].first);
      if (aa[i][j].second == 'R' && aa[i][j + 1].second == 'D')
        ans = min(ans, aa[i][j + 1].first - aa[i][j].first);
    }
    l = bb[i].size();
    rep(j, l - 1) {
      if (bb[i][j].second == 'R' && aa[i][j + 1].second == 'U')
        ans = min(ans, bb[i][j + 1].first - bb[i][j].first);
      if (bb[i][j].second == 'D' && aa[i][j + 1].second == 'L')
        ans = min(ans, bb[i][j + 1].first - bb[i][j].first);
    }
  }
  rep(i, 200005) {
    if (!cc[i].empty())
      sort(cc[i].begin(), cc[i].end());
    if (!dd[i].empty())
      sort(dd[i].begin(), dd[i].end());
  }
  rep(i, 200005) {
    long int l = cc[i].size();
    rep(j, l - 1) {
      if (cc[i][j].second == 'U' && cc[i][j + 1].second == 'D')
        ans = min(ans, cc[i][j + 1].first - cc[i][j].first);
    }
    l = dd[i].size();
    rep(j, l - 1) {
      if (dd[i][j].second == 'R' && dd[i][j + 1].second == 'L')
        ans = min(ans, dd[i][j + 1].first - dd[i][j].first);
    }
  }

  if (ans == 1000000000)
    cout << "SAFE" << endl;
  else
    cout << ans * 5 << endl;

  return 0;
}