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

#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;

int main() {
  unsigned N;
  scanf("%u", &N);
  vector<pair<unsigned, unsigned>> A[4];
  for (unsigned i = 0; i < N; i++) {
    unsigned x, y;
    char u;
    scanf("%u %u %c", &x, &y, &u);
    if (u == 'U')
      A[0].push_back(make_pair(x, y));
    else if (u == 'R')
      A[1].push_back(make_pair(x, y));
    else if (u == 'D')
      A[2].push_back(make_pair(x, y));
    else
      A[3].push_back(make_pair(x, y));
  }
  unsigned answer = 4000001;
  vector<unsigned> sum1[400001];
  for (auto i : A[0])
    sum1[i.first + i.second].push_back(i.first);
  for (unsigned i = 0; i < 400001; i++) {
    sort(sum1[i].begin(), sum1[i].end());
  }
  for (auto i : A[1]) {
    unsigned tmp = i.first + i.second;
    if (auto id = upper_bound(sum1[tmp].begin(), sum1[tmp].end(), i.first);
        id != sum1[tmp].end())
      if (answer > (*id - i.first) * 10)
        answer = (*id - i.first) * 10;
  }
  vector<unsigned> sum2[400001];
  for (auto i : A[3])
    sum2[i.first + i.second].push_back(i.first);
  for (unsigned i = 0; i < 400001; i++) {
    sort(sum2[i].begin(), sum2[i].end());
  }
  for (auto i : A[2]) {
    unsigned tmp = i.first + i.second;
    if (auto id = upper_bound(sum2[tmp].begin(), sum2[tmp].end(), i.first);
        id != sum2[tmp].end())
      if (answer > (*id - i.first) * 10)
        answer = (*id - i.first) * 10;
  }
  vector<unsigned> diff1[400001];
  for (auto i : A[3])
    diff1[200000 + i.first - i.second].push_back(i.first);
  for (unsigned i = 0; i < 400001; i++) {
    sort(diff1[i].begin(), diff1[i].end());
  }
  for (auto i : A[0]) {
    unsigned tmp = 200000 + i.first - i.second;
    if (auto id = upper_bound(diff1[tmp].begin(), diff1[tmp].end(), i.first);
        id != diff1[tmp].end())
      if (answer > (*id - i.first) * 10)
        answer = (*id - i.first) * 10;
  }
  vector<unsigned> diff2[400001];
  for (auto i : A[2])
    diff2[200000 + i.first - i.second].push_back(i.first);
  for (unsigned i = 0; i < 400001; i++) {
    sort(diff2[i].begin(), diff2[i].end());
  }
  for (auto i : A[1]) {
    unsigned tmp = 200000 + i.first - i.second;
    if (auto id = upper_bound(diff2[tmp].begin(), diff2[tmp].end(), i.first);
        id != diff2[tmp].end())
      if (answer > (*id - i.first) * 10)
        answer = (*id - i.first) * 10;
  }
  vector<unsigned> x1[200001];
  for (auto i : A[2])
    x1[i.first].push_back(i.second);
  for (unsigned i = 0; i < 200001; i++) {
    sort(x1[i].begin(), x1[i].end());
  }
  for (auto i : A[0]) {
    unsigned tmp = i.first;
    if (auto id = upper_bound(x1[tmp].begin(), x1[tmp].end(), i.second);
        id != x1[tmp].end())
      if (answer > (*id - i.second) * 5)
        answer = (*id - i.second) * 5;
  }
  vector<unsigned> y1[200001];
  for (auto i : A[3])
    y1[i.second].push_back(i.first);
  for (unsigned i = 0; i < 200001; i++) {
    sort(y1[i].begin(), y1[i].end());
  }
  for (auto i : A[1]) {
    unsigned tmp = i.second;
    if (auto id = upper_bound(y1[tmp].begin(), y1[tmp].end(), i.first);
        id != y1[tmp].end())
      if (answer > (*id - i.first) * 5)
        answer = (*id - i.first) * 5;
  }
  if (answer == 4000001)
    printf("SAFE\\n");
  else
    printf("%u\\n", answer);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;

int main() {
  unsigned N;
  scanf("%u", &N);
  vector<pair<unsigned, unsigned>> A[4];
  for (unsigned i = 0; i < N; i++) {
    unsigned x, y;
    char u;
    scanf("%u %u %c", &x, &y, &u);
    if (u == 'U')
      A[0].push_back(make_pair(x, y));
    else if (u == 'R')
      A[1].push_back(make_pair(x, y));
    else if (u == 'D')
      A[2].push_back(make_pair(x, y));
    else
      A[3].push_back(make_pair(x, y));
  }
  unsigned answer = 4000001;
  vector<unsigned> sum1[400001];
  for (auto i : A[0])
    sum1[i.first + i.second].push_back(i.first);
  for (unsigned i = 0; i < 400001; i++) {
    sort(sum1[i].begin(), sum1[i].end());
  }
  for (auto i : A[1]) {
    unsigned tmp = i.first + i.second;
    if (auto id = upper_bound(sum1[tmp].begin(), sum1[tmp].end(), i.first);
        id != sum1[tmp].end())
      if (answer > (*id - i.first) * 10)
        answer = (*id - i.first) * 10;
  }
  vector<unsigned> sum2[400001];
  for (auto i : A[3])
    sum2[i.first + i.second].push_back(i.first);
  for (unsigned i = 0; i < 400001; i++) {
    sort(sum2[i].begin(), sum2[i].end());
  }
  for (auto i : A[2]) {
    unsigned tmp = i.first + i.second;
    if (auto id = upper_bound(sum2[tmp].begin(), sum2[tmp].end(), i.first);
        id != sum2[tmp].end())
      if (answer > (*id - i.first) * 10)
        answer = (*id - i.first) * 10;
  }
  vector<unsigned> diff1[400001];
  for (auto i : A[3])
    diff1[200000 + i.first - i.second].push_back(i.first);
  for (unsigned i = 0; i < 400001; i++) {
    sort(diff1[i].begin(), diff1[i].end());
  }
  for (auto i : A[0]) {
    unsigned tmp = 20000 + i.first - i.second;
    if (auto id = upper_bound(diff1[tmp].begin(), diff1[tmp].end(), i.first);
        id != diff1[tmp].end())
      if (answer > (*id - i.first) * 10)
        answer = (*id - i.first) * 10;
  }
  vector<unsigned> diff2[400001];
  for (auto i : A[2])
    diff2[200000 + i.first - i.second].push_back(i.first);
  for (unsigned i = 0; i < 400001; i++) {
    sort(diff2[i].begin(), diff2[i].end());
  }
  for (auto i : A[1]) {
    unsigned tmp = 20000 + i.first - i.second;
    if (auto id = upper_bound(diff2[tmp].begin(), diff2[tmp].end(), i.first);
        id != diff2[tmp].end())
      if (answer > (*id - i.first) * 10)
        answer = (*id - i.first) * 10;
  }
  vector<unsigned> x1[200001];
  for (auto i : A[2])
    x1[i.first].push_back(i.second);
  for (unsigned i = 0; i < 200001; i++) {
    sort(x1[i].begin(), x1[i].end());
  }
  for (auto i : A[0]) {
    unsigned tmp = i.first;
    if (auto id = upper_bound(x1[tmp].begin(), x1[tmp].end(), i.second);
        id != x1[tmp].end())
      if (answer > (*id - i.second) * 5)
        answer = (*id - i.second) * 5;
  }
  vector<unsigned> y1[200001];
  for (auto i : A[3])
    y1[i.second].push_back(i.first);
  for (unsigned i = 0; i < 200001; i++) {
    sort(y1[i].begin(), y1[i].end());
  }
  for (auto i : A[1]) {
    unsigned tmp = i.second;
    if (auto id = upper_bound(y1[tmp].begin(), y1[tmp].end(), i.first);
        id != y1[tmp].end())
      if (answer > (*id - i.first) * 5)
        answer = (*id - i.first) * 5;
  }
  if (answer == 4000001)
    printf("SAFE\\n");
  else
    printf("%u\\n", answer);
  return 0;
}