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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef pair<int, int> PI;
typedef pair<ll, ll> PL;
using graph = vector<vector<int>>;

const double pi = 3.14159265358979323846;
const ll mod = 1000000007;

int n;
int x[200020], y[200020];
char u[200020];

void rotate() {
  for (int i = 0; i < n; i++) {
    int nx = y[i], ny = 200000 - x[i];
    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';
    x[i] = nx;
    y[i] = ny;
    u[i] = nu;
  }
}
vector<vector<pair<int, char>>> par;
ll solve01() {
  ll ans = 1001001001;
  par.resize(400010);
  rep(i, 400010) par.at(i).clear();
  for (int i = 0; i < n; i++) {
    par.at(y[i]).push_back({x[i], u[i]});
  }
  for (int i = 0; i < 400001; i++) {
    sort(par.at(i).begin(), par.at(i).end());
    for (int j = 0; j < (int)par.at(i).size() - 1; j++) {
      if (!(par.at(i).at(j).second == 'R' && par.at(i).at(j + 1).second == 'L'))
        continue;
      ans = min(ans, 5ll * (par.at(i).at(j + 1).first - par.at(i).at(j).first));
    }
  }
  return ans;
}
vector<vector<pair<int, char>>> cro;
ll solve02() {
  ll ans = 1001001001;
  cro.resize(400010);
  rep(i, 400010) cro.at(i).clear();
  for (int i = 0; i < n; i++) {
    cro.at(x[i] + y[i]).push_back({x[i], u[i]});
  }
  for (int i = 0; i < 400001; i++) {
    sort(cro.at(i).begin(), cro.at(i).end());
    for (int j = 0; j < (int)cro.at(i).size() - 1; j++) {
      if (!(cro.at(i).at(j).second == 'R' && cro.at(i).at(j + 1).second == 'U'))
        continue;
      ans =
          min(ans, 10ll * (cro.at(i).at(j + 1).first - cro.at(i).at(j).first));
    }
  }
  return ans;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  rep(i, n) cin >> x[i] >> y[i] >> u[i];
  ll FA = 1001001001;
  for (int i = 0; i < 4; i++) {
    FA = min(FA, solve01());
    FA = min(FA, solve02());
    rotate();
  }
  if (FA == 1001001001)
    cout << "SAFE" << endl;
  else
    cout << FA << endl;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef pair<int, int> PI;
typedef pair<ll, ll> PL;
using graph = vector<vector<int>>;

const double pi = 3.14159265358979323846;
const ll mod = 1000000007;

int n;
int x[200020], y[200020];
char u[200020];

void rotate() {
  for (int i = 0; i < n; i++) {
    int nx = y[i], ny = 200000 - x[i];
    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';
    x[i] = nx;
    y[i] = ny;
    u[i] = nu;
  }
}
vector<vector<pair<int, char>>> par;
int solve01() {
  int ans = 1001001001;
  par.resize(400001);
  for (int i = 0; i < n; i++) {
    par.at(y[i]).push_back({x[i], u[i]});
  }
  for (int i = 0; i < 400001; i++) {
    sort(par.at(i).begin(), par.at(i).end());
    for (int j = 0; j < par.at(i).size() - 1; j++) {
      if (!(par.at(i).at(j).second == 'R' && par.at(i).at(j + 1).second == 'L'))
        continue;
      ans = min(ans, par.at(i).at(j + 1).first - par.at(i).at(j).first);
    }
  }
  return ans * 5;
}
vector<vector<pair<int, char>>> cro;
int solve02() {
  int ans = 1001001001;
  cro.resize(400001);
  for (int i = 0; i < n; i++) {
    cro.at(x[i] + y[i]).push_back({x[i], u[i]});
  }
  for (int i = 0; i < 400001; i++) {
    sort(cro.at(i).begin(), cro.at(i).end());
    for (int j = 0; j < cro.at(i).size() - 1; j++) {
      if (!(cro.at(i).at(j).second == 'R' && cro.at(i).at(j + 1).second == 'U'))
        continue;
      ans = min(ans, cro.at(i).at(j + 1).first - cro.at(i).at(j).first);
    }
  }
  return ans * 10;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  rep(i, n) cin >> x[i] >> y[i] >> u[i];
  int FA = 1001001001;
  for (int i = 0; i < 4; i++) {
    int tmp1 = solve01();
    int tmp2 = solve02();
    cout << tmp1 << " " << tmp2 << endl;
    FA = min(FA, solve01());
    FA = min(FA, solve02());
    rotate();
  }
  if (FA == 1001001001)
    cout << "SAFE" << endl;
  cout << FA << endl;
}