Problem p02605 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int X[100000];
int Y[100000];
char U[100000];

int dir_to_ind(char c) {
  if (c == 'U') {
    return 0;
  } else if (c == 'R') {
    return 1;
  } else if (c == 'D') {
    return 2;
  }
  return 3;
}

vector<int> dir_X_to_Y[4][200001];
vector<int> dir_Y_to_X[4][200001];
vector<int> dir_X_plus_Y_to_Y[4][400001];
vector<int> dir_X_plus_Y_to_X[4][400001];
vector<int> dir_X_minus_Y_to_Y[4][400001];
vector<int> dir_X_minus_Y_to_X[4][400001];

int main() {
  int N;
  cin >> N;

  int X_max = INT_MIN;
  int X_min = INT_MAX;
  int Y_max = INT_MIN;
  int Y_min = INT_MAX;
  int diff_max = INT_MIN;
  int diff_min = INT_MAX;
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> U[i];
    X_max = max(X_max, X[i]);
    X_min = min(X_min, X[i]);
    Y_max = max(Y_max, Y[i]);
    Y_min = min(Y_min, Y[i]);
    diff_max = max(diff_max, X[i] - Y[i]);
    diff_min = min(diff_min, X[i] - Y[i]);
  }

  for (int i = 0; i < N; i++) {
    dir_X_to_Y[dir_to_ind(U[i])][X[i]].push_back(Y[i]);
    dir_Y_to_X[dir_to_ind(U[i])][Y[i]].push_back(X[i]);
    dir_X_plus_Y_to_X[dir_to_ind(U[i])][X[i] + Y[i]].push_back(X[i]);
    dir_X_plus_Y_to_Y[dir_to_ind(U[i])][X[i] + Y[i]].push_back(Y[i]);
    dir_X_minus_Y_to_X[dir_to_ind(U[i])][X[i] - Y[i] - diff_min].push_back(
        X[i]);
    dir_X_minus_Y_to_Y[dir_to_ind(U[i])][X[i] - Y[i] - diff_min].push_back(
        Y[i]);
  }

  for (int i = 0; i <= X_max; i++) {
    for (int j = 0; j < 4; j++) {
      sort(dir_Y_to_X[j][i].begin(), dir_Y_to_X[j][i].end());
      dir_Y_to_X[j][i].erase(
          unique(dir_Y_to_X[j][i].begin(), dir_Y_to_X[j][i].end()),
          dir_Y_to_X[j][i].end());
    }
  }
  for (int i = 0; i <= Y_max; i++) {
    for (int j = 0; j < 4; j++) {
      sort(dir_X_to_Y[j][i].begin(), dir_X_to_Y[j][i].end());
      dir_X_to_Y[j][i].erase(
          unique(dir_X_to_Y[j][i].begin(), dir_X_to_Y[j][i].end()),
          dir_X_to_Y[j][i].end());
    }
  }
  for (int i = 0; i <= X_max + Y_max; i++) {
    for (int j = 0; j < 4; j++) {
      sort(dir_X_plus_Y_to_X[j][i].begin(), dir_X_plus_Y_to_X[j][i].end());
      dir_X_plus_Y_to_X[j][i].erase(unique(dir_X_plus_Y_to_X[j][i].begin(),
                                           dir_X_plus_Y_to_X[j][i].end()),
                                    dir_X_plus_Y_to_X[j][i].end());
      sort(dir_X_plus_Y_to_Y[j][i].begin(), dir_X_plus_Y_to_Y[j][i].end());
      dir_X_plus_Y_to_Y[j][i].erase(unique(dir_X_plus_Y_to_Y[j][i].begin(),
                                           dir_X_plus_Y_to_Y[j][i].end()),
                                    dir_X_plus_Y_to_Y[j][i].end());
    }
  }
  for (int i = diff_min; i <= diff_max; i++) {
    for (int j = 0; j < 4; j++) {
      sort(dir_X_minus_Y_to_X[j][i - diff_min].begin(),
           dir_X_minus_Y_to_X[j][i - diff_min].end());
      dir_X_minus_Y_to_X[j][i - diff_min].erase(
          unique(dir_X_minus_Y_to_X[j][i - diff_min].begin(),
                 dir_X_minus_Y_to_X[j][i - diff_min].end()),
          dir_X_minus_Y_to_X[j][i - diff_min].end());
      sort(dir_X_minus_Y_to_Y[j][i - diff_min].begin(),
           dir_X_minus_Y_to_Y[j][i - diff_min].end());
      dir_X_minus_Y_to_Y[j][i - diff_min].erase(
          unique(dir_X_minus_Y_to_Y[j][i - diff_min].begin(),
                 dir_X_minus_Y_to_Y[j][i - diff_min].end()),
          dir_X_minus_Y_to_Y[j][i - diff_min].end());
    }
  }

  int ans = INT_MAX;
  for (int i = 0; i < N; i++) {
    int sum_XY = X[i] + Y[i];
    int diff_XY = X[i] - Y[i];

    if (U[i] == 'U') {
      vector<int> collidable = dir_X_to_Y[dir_to_ind('D')][X[i]];
      auto it = upper_bound(collidable.begin(), collidable.end(), Y[i]);
      if (it != collidable.end()) {
        ans = min(ans, (*it - Y[i]) * 5);
      }

      collidable = dir_X_plus_Y_to_Y[dir_to_ind('R')][sum_XY];
      it = upper_bound(collidable.begin(), collidable.end(), Y[i]);
      if (it != collidable.end()) {
        ans = min(ans, (*it - Y[i]) * 10);
      }
      collidable = dir_X_minus_Y_to_Y[dir_to_ind('L')][diff_XY - diff_min];
      it = upper_bound(collidable.begin(), collidable.end(), Y[i]);
      if (it != collidable.end()) {
        ans = min(ans, (*it - Y[i]) * 10);
      }

    } else if (U[i] == 'R') {
      vector<int> collidable = dir_Y_to_X[dir_to_ind('L')][Y[i]];
      auto it = upper_bound(collidable.begin(), collidable.end(), X[i]);
      if (it != collidable.end()) {
        ans = min(ans, (*it - X[i]) * 5);
      }

      collidable = dir_X_plus_Y_to_X[dir_to_ind('U')][sum_XY];
      it = upper_bound(collidable.begin(), collidable.end(), X[i]);
      if (it != collidable.end()) {
        ans = min(ans, (*it - X[i]) * 10);
      }
      collidable = dir_X_minus_Y_to_X[dir_to_ind('D')][diff_XY - diff_min];
      it = upper_bound(collidable.begin(), collidable.end(), X[i]);
      if (it != collidable.end()) {
        ans = min(ans, (*it - X[i]) * 10);
      }

    } else if (U[i] == 'D') {
      vector<int> collidable = dir_X_to_Y[dir_to_ind('U')][X[i]];
      auto it = lower_bound(collidable.begin(), collidable.end(), Y[i]);
      if (it != collidable.begin()) {
        it--;
        ans = min(ans, (Y[i] - *it) * 5);
      }

      collidable = dir_X_plus_Y_to_Y[dir_to_ind('L')][sum_XY];
      it = lower_bound(collidable.begin(), collidable.end(), Y[i]);
      if (it != collidable.begin()) {
        it--;
        ans = min(ans, (Y[i] - *it) * 10);
      }
      collidable = dir_X_minus_Y_to_Y[dir_to_ind('R')][diff_XY - diff_min];
      it = lower_bound(collidable.begin(), collidable.end(), Y[i]);
      // cout << collidable.size() << endl;
      if (it != collidable.begin()) {
        it--;
        ans = min(ans, (Y[i] - *it) * 10);
      }

    } else if (U[i] == 'L') {
      vector<int> collidable = dir_Y_to_X[dir_to_ind('R')][Y[i]];
      auto it = lower_bound(collidable.begin(), collidable.end(), X[i]);
      if (it != collidable.begin()) {
        it--;
        ans = min(ans, (X[i] - *it) * 5);
      }

      collidable = dir_X_plus_Y_to_X[dir_to_ind('D')][sum_XY];
      it = lower_bound(collidable.begin(), collidable.end(), X[i]);
      if (it != collidable.begin()) {
        it--;
        ans = min(ans, (X[i] - *it) * 10);
      }
      collidable = dir_X_minus_Y_to_X[dir_to_ind('U')][diff_XY - diff_min];
      it = lower_bound(collidable.begin(), collidable.end(), X[i]);
      if (it != collidable.begin()) {
        it--;
        ans = min(ans, (X[i] - *it) * 10);
      }
    }
  }

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

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int X[200000];
int Y[200000];
char U[200000];

int dir_to_ind(char c) {
  if (c == 'U') {
    return 0;
  } else if (c == 'R') {
    return 1;
  } else if (c == 'D') {
    return 2;
  }
  return 3;
}

vector<int> dir_X_to_Y[4][200001];
vector<int> dir_Y_to_X[4][200001];
vector<int> dir_X_plus_Y_to_Y[4][400001];
vector<int> dir_X_plus_Y_to_X[4][400001];
vector<int> dir_X_minus_Y_to_Y[4][400001];
vector<int> dir_X_minus_Y_to_X[4][400001];

int main() {
  int N;
  cin >> N;

  int X_max = INT_MIN;
  int X_min = INT_MAX;
  int Y_max = INT_MIN;
  int Y_min = INT_MAX;
  int diff_max = INT_MIN;
  int diff_min = INT_MAX;
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> U[i];
    X_max = max(X_max, X[i]);
    X_min = min(X_min, X[i]);
    Y_max = max(Y_max, Y[i]);
    Y_min = min(Y_min, Y[i]);
    diff_max = max(diff_max, X[i] - Y[i]);
    diff_min = min(diff_min, X[i] - Y[i]);
  }

  for (int i = 0; i < N; i++) {
    dir_X_to_Y[dir_to_ind(U[i])][X[i]].push_back(Y[i]);
    dir_Y_to_X[dir_to_ind(U[i])][Y[i]].push_back(X[i]);
    dir_X_plus_Y_to_X[dir_to_ind(U[i])][X[i] + Y[i]].push_back(X[i]);
    dir_X_plus_Y_to_Y[dir_to_ind(U[i])][X[i] + Y[i]].push_back(Y[i]);
    dir_X_minus_Y_to_X[dir_to_ind(U[i])][X[i] - Y[i] - diff_min].push_back(
        X[i]);
    dir_X_minus_Y_to_Y[dir_to_ind(U[i])][X[i] - Y[i] - diff_min].push_back(
        Y[i]);
  }

  for (int i = 0; i <= X_max; i++) {
    for (int j = 0; j < 4; j++) {
      sort(dir_Y_to_X[j][i].begin(), dir_Y_to_X[j][i].end());
      dir_Y_to_X[j][i].erase(
          unique(dir_Y_to_X[j][i].begin(), dir_Y_to_X[j][i].end()),
          dir_Y_to_X[j][i].end());
    }
  }
  for (int i = 0; i <= Y_max; i++) {
    for (int j = 0; j < 4; j++) {
      sort(dir_X_to_Y[j][i].begin(), dir_X_to_Y[j][i].end());
      dir_X_to_Y[j][i].erase(
          unique(dir_X_to_Y[j][i].begin(), dir_X_to_Y[j][i].end()),
          dir_X_to_Y[j][i].end());
    }
  }
  for (int i = 0; i <= X_max + Y_max; i++) {
    for (int j = 0; j < 4; j++) {
      sort(dir_X_plus_Y_to_X[j][i].begin(), dir_X_plus_Y_to_X[j][i].end());
      dir_X_plus_Y_to_X[j][i].erase(unique(dir_X_plus_Y_to_X[j][i].begin(),
                                           dir_X_plus_Y_to_X[j][i].end()),
                                    dir_X_plus_Y_to_X[j][i].end());
      sort(dir_X_plus_Y_to_Y[j][i].begin(), dir_X_plus_Y_to_Y[j][i].end());
      dir_X_plus_Y_to_Y[j][i].erase(unique(dir_X_plus_Y_to_Y[j][i].begin(),
                                           dir_X_plus_Y_to_Y[j][i].end()),
                                    dir_X_plus_Y_to_Y[j][i].end());
    }
  }
  for (int i = diff_min; i <= diff_max; i++) {
    for (int j = 0; j < 4; j++) {
      sort(dir_X_minus_Y_to_X[j][i - diff_min].begin(),
           dir_X_minus_Y_to_X[j][i - diff_min].end());
      dir_X_minus_Y_to_X[j][i - diff_min].erase(
          unique(dir_X_minus_Y_to_X[j][i - diff_min].begin(),
                 dir_X_minus_Y_to_X[j][i - diff_min].end()),
          dir_X_minus_Y_to_X[j][i - diff_min].end());
      sort(dir_X_minus_Y_to_Y[j][i - diff_min].begin(),
           dir_X_minus_Y_to_Y[j][i - diff_min].end());
      dir_X_minus_Y_to_Y[j][i - diff_min].erase(
          unique(dir_X_minus_Y_to_Y[j][i - diff_min].begin(),
                 dir_X_minus_Y_to_Y[j][i - diff_min].end()),
          dir_X_minus_Y_to_Y[j][i - diff_min].end());
    }
  }

  int ans = INT_MAX;
  for (int i = 0; i < N; i++) {
    int sum_XY = X[i] + Y[i];
    int diff_XY = X[i] - Y[i];

    if (U[i] == 'U') {
      vector<int> collidable = dir_X_to_Y[dir_to_ind('D')][X[i]];
      auto it = upper_bound(collidable.begin(), collidable.end(), Y[i]);
      if (it != collidable.end()) {
        ans = min(ans, (*it - Y[i]) * 5);
      }

      collidable = dir_X_plus_Y_to_Y[dir_to_ind('R')][sum_XY];
      it = upper_bound(collidable.begin(), collidable.end(), Y[i]);
      if (it != collidable.end()) {
        ans = min(ans, (*it - Y[i]) * 10);
      }
      collidable = dir_X_minus_Y_to_Y[dir_to_ind('L')][diff_XY - diff_min];
      it = upper_bound(collidable.begin(), collidable.end(), Y[i]);
      if (it != collidable.end()) {
        ans = min(ans, (*it - Y[i]) * 10);
      }

    } else if (U[i] == 'R') {
      vector<int> collidable = dir_Y_to_X[dir_to_ind('L')][Y[i]];
      auto it = upper_bound(collidable.begin(), collidable.end(), X[i]);
      if (it != collidable.end()) {
        ans = min(ans, (*it - X[i]) * 5);
      }

      collidable = dir_X_plus_Y_to_X[dir_to_ind('U')][sum_XY];
      it = upper_bound(collidable.begin(), collidable.end(), X[i]);
      if (it != collidable.end()) {
        ans = min(ans, (*it - X[i]) * 10);
      }
      collidable = dir_X_minus_Y_to_X[dir_to_ind('D')][diff_XY - diff_min];
      it = upper_bound(collidable.begin(), collidable.end(), X[i]);
      if (it != collidable.end()) {
        ans = min(ans, (*it - X[i]) * 10);
      }

    } else if (U[i] == 'D') {
      vector<int> collidable = dir_X_to_Y[dir_to_ind('U')][X[i]];
      auto it = lower_bound(collidable.begin(), collidable.end(), Y[i]);
      if (it != collidable.begin()) {
        it--;
        ans = min(ans, (Y[i] - *it) * 5);
      }

      collidable = dir_X_plus_Y_to_Y[dir_to_ind('L')][sum_XY];
      it = lower_bound(collidable.begin(), collidable.end(), Y[i]);
      if (it != collidable.begin()) {
        it--;
        ans = min(ans, (Y[i] - *it) * 10);
      }
      collidable = dir_X_minus_Y_to_Y[dir_to_ind('R')][diff_XY - diff_min];
      it = lower_bound(collidable.begin(), collidable.end(), Y[i]);
      // cout << collidable.size() << endl;
      if (it != collidable.begin()) {
        it--;
        ans = min(ans, (Y[i] - *it) * 10);
      }

    } else if (U[i] == 'L') {
      vector<int> collidable = dir_Y_to_X[dir_to_ind('R')][Y[i]];
      auto it = lower_bound(collidable.begin(), collidable.end(), X[i]);
      if (it != collidable.begin()) {
        it--;
        ans = min(ans, (X[i] - *it) * 5);
      }

      collidable = dir_X_plus_Y_to_X[dir_to_ind('D')][sum_XY];
      it = lower_bound(collidable.begin(), collidable.end(), X[i]);
      if (it != collidable.begin()) {
        it--;
        ans = min(ans, (X[i] - *it) * 10);
      }
      collidable = dir_X_minus_Y_to_X[dir_to_ind('U')][diff_XY - diff_min];
      it = lower_bound(collidable.begin(), collidable.end(), X[i]);
      if (it != collidable.begin()) {
        it--;
        ans = min(ans, (X[i] - *it) * 10);
      }
    }
  }

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