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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#define kata 200000
#define ryou 400000

int main() {
  int N;
  cin >> N;
  vector<pair<int, int>> xr[kata + 1];
  vector<pair<int, int>> xl[kata + 1];
  vector<pair<int, int>> yu[kata + 1]; // xy gyaku
  vector<pair<int, int>> yd[kata + 1]; // xy gyaku
  vector<pair<int, int>> migiup_l[ryou + 1];
  vector<pair<int, int>> migiup_r[ryou + 1];
  vector<pair<int, int>> migiup_u[ryou + 1];
  vector<pair<int, int>> migiup_d[ryou + 1];
  vector<pair<int, int>> migidown_r[ryou + 1];
  vector<pair<int, int>> migidown_l[ryou + 1];
  vector<pair<int, int>> migidown_u[ryou + 1];
  vector<pair<int, int>> migidown_d[ryou + 1];
  int X, Y;
  char U;
  int i, j, k;
  for (i = 1; i <= N; i++) {
    cin >> X >> Y >> U;
    if (U == 'R') {
      xr[Y].push_back({X, Y});
      migiup_r[Y - X + kata].push_back({X, Y});
      migidown_r[Y + X].push_back({X, Y});
    } else if (U == 'L') {
      xl[Y].push_back({X, Y});
      migiup_l[Y - X + kata].push_back({X, Y});
      migidown_l[Y + X].push_back({X, Y});
    } else if (U == 'U') {
      yu[X].push_back({Y, X});
      migiup_u[Y - X + kata].push_back({X, Y});
      migidown_u[Y + X].push_back({X, Y});
    } else if (U == 'D') {
      yd[X].push_back({Y, X});
      migiup_d[Y - X + kata].push_back({X, Y});
      migidown_d[Y + X].push_back({X, Y});
    }
  }
  for (i = 0; i <= kata; i++) {
    sort(xr[i].begin(), xr[i].end());
    sort(xl[i].begin(), xl[i].end());
    sort(yd[i].begin(), yd[i].end());
    sort(yu[i].begin(), yu[i].end());
  }
  for (i = 0; i <= ryou; i++) {
    sort(migiup_r[i].begin(), migiup_r[i].end());
    sort(migiup_l[i].begin(), migiup_l[i].end());
    sort(migiup_d[i].begin(), migiup_d[i].end());
    sort(migiup_u[i].begin(), migiup_u[i].end());
    sort(migidown_r[i].begin(), migidown_r[i].end());
    sort(migidown_l[i].begin(), migidown_l[i].end());
    sort(migidown_d[i].begin(), migidown_d[i].end());
    sort(migidown_u[i].begin(), migidown_u[i].end());
  }
  int ans = 1000000000;
  for (i = 0; i <= kata; i++) {
    if (xr[i].size() > 0 && xl[i].size() > 0) {
      for (j = 0; j < xr[i].size(); j++) {
        k = lower_bound(xl[i].begin(), xl[i].end(),
                        make_pair(xr[i][j].first, 0)) -
            xl[i].begin();
        if (k < xl[i].size()) {
          ans = min(ans, 5 * (xl[i][k].first - xr[i][j].first));
        }
      }
    }
  }
  for (i = 0; i <= kata; i++) {
    if (yu[i].size() > 0 && yd[i].size() > 0) {
      for (j = 0; j < yu[i].size(); j++) {
        k = lower_bound(yd[i].begin(), yd[i].end(),
                        make_pair(yu[i][j].first, 0)) -
            yd[i].begin();
        if (k < yd[i].size()) {
          ans = min(ans, 5 * (yd[i][k].first - yu[i][j].first));
        }
      }
    }
  }
  for (i = 0; i <= ryou; i++) {
    if (migiup_r[i].size() > 0 && migiup_d[i].size() > 0) {
      for (j = 0; j < migiup_r[i].size(); j++) {
        k = lower_bound(migiup_d[i].begin(), migiup_d[i].end(),
                        make_pair(migiup_r[i][j].first, 0)) -
            migiup_d[i].begin();
        if (k < migiup_d[i].size()) {
          ans = min(ans, 10 * (migiup_d[i][k].first - migiup_r[i][j].first));
        }
      }
    }
  }
  for (i = 0; i <= ryou; i++) {
    if (migiup_u[i].size() > 0 && migiup_l[i].size() > 0) {
      for (j = 0; j < migiup_u[i].size(); j++) {
        k = lower_bound(migiup_l[i].begin(), migiup_l[i].end(),
                        make_pair(migiup_u[i][j].first, 0)) -
            migiup_l[i].begin();
        if (k < migiup_l[i].size()) {
          ans = min(ans, 10 * (migiup_l[i][k].first - migiup_u[i][j].first));
        }
      }
    }
  }
  for (i = 0; i <= ryou; i++) {
    if (migidown_d[i].size() > 0 && migidown_l[i].size() > 0) {
      for (j = 0; j < migidown_d[i].size(); j++) {
        k = lower_bound(migidown_l[i].begin(), migidown_l[i].end(),
                        make_pair(migidown_d[i][j].first, 0)) -
            migidown_l[i].begin();
        if (k < migidown_l[i].size()) {
          ans =
              min(ans, 10 * (migidown_l[i][k].first - migidown_d[i][j].first));
        }
      }
    }
  }
  for (i = 0; i <= ryou; i++) {
    if (migidown_r[i].size() > 0 && migidown_u[i].size() > 0) {
      for (j = 0; j < migidown_r[i].size(); j++) {
        k = lower_bound(migidown_u[i].begin(), migidown_u[i].end(),
                        make_pair(migidown_r[i][j].first, 0)) -
            migidown_u[i].begin();
        if (k < migidown_u[i].size()) {
          ans =
              min(ans, 10 * (migidown_u[i][k].first - migidown_r[i][j].first));
        }
      }
    }
  }
  if (ans < 1000000000) {
    cout << ans << endl;
  } else {
    cout << "SAFE" << endl;
  }
  system("pause");
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#define kata 200000
#define ryou 400000

int main() {
  int N;
  cin >> N;
  vector<pair<int, int>> xr[kata + 1];
  vector<pair<int, int>> xl[kata + 1];
  vector<pair<int, int>> yu[kata + 1]; // xy gyaku
  vector<pair<int, int>> yd[kata + 1]; // xy gyaku
  vector<pair<int, int>> migiup_l[ryou + 1];
  vector<pair<int, int>> migiup_r[ryou + 1];
  vector<pair<int, int>> migiup_u[ryou + 1];
  vector<pair<int, int>> migiup_d[ryou + 1];
  vector<pair<int, int>> migidown_r[ryou + 1];
  vector<pair<int, int>> migidown_l[ryou + 1];
  vector<pair<int, int>> migidown_u[ryou + 1];
  vector<pair<int, int>> migidown_d[ryou + 1];
  int X, Y;
  char U;
  int i, j, k;
  for (i = 1; i <= N; i++) {
    cin >> X >> Y >> U;
    if (U == 'R') {
      xr[Y].push_back({X, Y});
      migiup_r[Y - X + kata].push_back({X, Y});
      migidown_r[Y + X + kata].push_back({X, Y});
    } else if (U == 'L') {
      xl[Y].push_back({X, Y});
      migiup_l[Y - X + kata].push_back({X, Y});
      migidown_l[Y + X + kata].push_back({X, Y});
    } else if (U == 'U') {
      yu[X].push_back({Y, X});
      migiup_u[Y - X + kata].push_back({X, Y});
      migidown_u[Y + X + kata].push_back({X, Y});
    } else if (U == 'D') {
      yd[X].push_back({Y, X});
      migiup_d[Y - X + kata].push_back({X, Y});
      migidown_d[Y + X + kata].push_back({X, Y});
    }
  }
  for (i = 0; i <= kata; i++) {
    sort(xr[i].begin(), xr[i].end());
    sort(xl[i].begin(), xl[i].end());
    sort(yd[i].begin(), yd[i].end());
    sort(yu[i].begin(), yu[i].end());
  }
  for (i = 0; i <= ryou; i++) {
    sort(migiup_r[i].begin(), migiup_r[i].end());
    sort(migiup_l[i].begin(), migiup_l[i].end());
    sort(migiup_d[i].begin(), migiup_d[i].end());
    sort(migiup_u[i].begin(), migiup_u[i].end());
    sort(migidown_r[i].begin(), migidown_r[i].end());
    sort(migidown_l[i].begin(), migidown_l[i].end());
    sort(migidown_d[i].begin(), migidown_d[i].end());
    sort(migidown_u[i].begin(), migidown_u[i].end());
  }
  int ans = 1000000000;
  for (i = 0; i <= kata; i++) {
    if (xr[i].size() > 0 && xl[i].size() > 0) {
      for (j = 0; j < xr[i].size(); j++) {
        k = lower_bound(xl[i].begin(), xl[i].end(),
                        make_pair(xr[i][j].first, 0)) -
            xl[i].begin();
        if (k < xl[i].size()) {
          ans = min(ans, 5 * (xl[i][k].first - xr[i][j].first));
        }
      }
    }
  }
  for (i = 0; i <= kata; i++) {
    if (yd[i].size() > 0 && yu[i].size() > 0) {
      for (j = 0; j < yd[i].size(); j++) {
        k = lower_bound(yu[i].begin(), yu[i].end(),
                        make_pair(yd[i][j].first, 0)) -
            yu[i].begin();
        if (k < yu[i].size()) {
          ans = min(ans, 5 * (yu[i][k].first - yd[i][j].first));
        }
      }
    }
  }
  for (i = 0; i <= ryou; i++) {
    if (migiup_r[i].size() > 0 && migiup_d[i].size() > 0) {
      for (j = 0; j < migiup_r[i].size(); j++) {
        k = lower_bound(migiup_d[i].begin(), migiup_d[i].end(),
                        make_pair(migiup_r[i][j].first, 0)) -
            migiup_d[i].begin();
        if (k < migiup_d[i].size()) {
          ans = min(ans, 10 * (migiup_d[i][k].first - migiup_r[i][j].first));
        }
      }
    }
  }
  for (i = 0; i <= ryou; i++) {
    if (migiup_u[i].size() > 0 && migiup_l[i].size() > 0) {
      for (j = 0; j < migiup_u[i].size(); j++) {
        k = lower_bound(migiup_l[i].begin(), migiup_l[i].end(),
                        make_pair(migiup_u[i][j].first, 0)) -
            migiup_l[i].begin();
        if (k < migiup_l[i].size()) {
          ans = min(ans, 10 * (migiup_l[i][k].first - migiup_u[i][j].first));
        }
      }
    }
  }
  for (i = 0; i <= ryou; i++) {
    if (migidown_d[i].size() > 0 && migidown_l[i].size() > 0) {
      for (j = 0; j < migidown_d[i].size(); j++) {
        k = lower_bound(migidown_l[i].begin(), migidown_l[i].end(),
                        make_pair(migidown_d[i][j].first, 0)) -
            migidown_l[i].begin();
        if (k < migidown_l[i].size()) {
          ans =
              min(ans, 10 * (migidown_l[i][k].first - migidown_l[i][j].first));
        }
      }
    }
  }
  for (i = 0; i <= ryou; i++) {
    if (migidown_r[i].size() > 0 && migidown_u[i].size() > 0) {
      for (j = 0; j < migidown_r[i].size(); j++) {
        k = lower_bound(migidown_u[i].begin(), migidown_u[i].end(),
                        make_pair(migidown_r[i][j].first, 0)) -
            migidown_u[i].begin();
        if (k < migidown_u[i].size()) {
          ans =
              min(ans, 10 * (migidown_u[i][k].first - migidown_r[i][j].first));
        }
      }
    }
  }
  if (ans < 1000000000) {
    cout << ans << endl;
  } else {
    cout << "SAFE" << endl;
  }
  system("pause");
  return 0;
}