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

#include <iostream>
#include <set>
using namespace std;

int main() {
  long long N;
  cin >> N;
  long long X[N], Y[N];
  char U[N];
  for (int i = 0; i < N; i++)
    cin >> X[i] >> Y[i] >> U[i];
  long long min_ = 100000000000000;

  set<long long> Up[500000], Right[500000], Down[500000], Left[500000];
  for (int i = 0; i < N; i++) {
    if (U[i] == 'U') {
      Up[250000 + X[i]].insert(Y[i]);
    }
    if (U[i] == 'R') {
      Right[250000 + Y[i]].insert(X[i]);
    }
    if (U[i] == 'D') {
      Down[250000 + X[i]].insert(Y[i]);
    }
    if (U[i] == 'L') {
      Left[250000 + Y[i]].insert(X[i]);
    }
  }
  for (int i = 0; i < 500000; i++) {
    if (Up[i].empty() || Down[i].empty())
      continue;
    auto itr = Up[i].begin();
    long long a = Up[i].size();
    for (long long j = 0; j < a; j++) {
      auto itr2 = Down[i].lower_bound(*itr);
      long long b = (*itr2) - (*itr);
      if (b <= 0)
        continue;
      min_ = min(b, min_);
      itr++;
    }
  }
  for (int i = 0; i < 500000; i++) {
    if (Right[i].empty() || Left[i].empty())
      continue;
    auto itr = Right[i].begin();
    long long a = Right[i].size();
    for (long long j = 0; j < a; j++) {
      auto itr2 = Left[i].lower_bound(*itr);
      long long b = (*itr2) - (*itr);
      if (b <= 0)
        continue;
      min_ = min(b, min_);
      itr++;
    }
  }

  set<long long> UR[500000], RU[500000], RD[500000], DR[500000], DL[500000],
      LD[500000], LU[500000], UL[500000];
  for (int i = 0; i < N; i++) {
    if (U[i] == 'U') {
      if (X[i] + Y[i] < 250000 && X[i] + Y[i] >= -250000)
        UR[X[i] + Y[i] + 250000].insert(X[i]);
      if (Y[i] - X[i] < 250000 && Y[i] - X[i] >= -250000)
        UL[Y[i] - X[i] + 250000].insert(X[i]);
    }
    if (U[i] == 'R') {
      if (X[i] + Y[i] < 250000 && X[i] + Y[i] >= -250000)
        RU[X[i] + Y[i] + 250000].insert(X[i]);
      if (Y[i] - X[i] < 250000 && Y[i] - X[i] >= -250000)
        RD[Y[i] - X[i] + 250000].insert(X[i]);
    }
    if (U[i] == 'D') {
      if (X[i] + Y[i] < 250000 && X[i] + Y[i] >= -250000)
        DL[X[i] + Y[i] + 250000].insert(X[i]);
      if (Y[i] - X[i] < 250000 && Y[i] - X[i] >= -250000)
        DR[Y[i] - X[i] + 250000].insert(X[i]);
    }
    if (U[i] == 'L') {
      if (X[i] + Y[i] < 250000 && X[i] + Y[i] >= -250000)
        LD[X[i] + Y[i] + 250000].insert(X[i]);
      if (Y[i] - X[i] < 250000 && Y[i] - X[i] >= -250000)
        LU[Y[i] - X[i] + 250000].insert(X[i]);
    }
  }
  for (int i = 0; i < 500000; i++) {
    if (UR[i].empty() || RU[i].empty())
      continue;
    auto itr = RU[i].begin();
    long long a = RU[i].size();
    for (long long j = 0; j < a; j++) {
      auto itr2 = UR[i].lower_bound(*itr);
      long long b = (*itr2) - (*itr);
      if (b <= 0)
        continue;
      min_ = min(2 * b, min_);
      itr++;
    }
  }
  for (int i = 0; i < 500000; i++) {
    if (RD[i].empty() || DR[i].empty())
      continue;
    auto itr = RD[i].begin();
    long long a = RD[i].size();
    for (long long j = 0; j < a; j++) {
      auto itr2 = DR[i].lower_bound(*itr);
      long long b = (*itr2) - (*itr);
      if (b <= 0)
        continue;
      min_ = min(2 * b, min_);
      itr++;
    }
  }
  for (int i = 0; i < 500000; i++) {
    if (DL[i].empty() || LD[i].empty())
      continue;
    auto itr = DL[i].begin();
    long long a = DL[i].size();
    for (long long j = 0; j < a; j++) {
      auto itr2 = LD[i].lower_bound(*itr);
      long long b = (*itr2) - (*itr);
      if (b <= 0)
        continue;
      min_ = min(2 * b, min_);
      itr++;
    }
  }
  for (int i = 0; i < 500000; i++) {
    if (UL[i].empty() || LU[i].empty())
      continue;
    auto itr = UL[i].begin();
    long long a = UL[i].size();
    for (long long j = 0; j < a; j++) {
      auto itr2 = LU[i].lower_bound(*itr);
      long long b = (*itr2) - (*itr);
      if (b <= 0)
        continue;
      min_ = min(2 * b, min_);
      itr++;
    }
  }
  if (min_ == 100000000000000)
    cout << "SAFE" << endl;
  else
    cout << min_ * 5 << endl;
}


Error: -11

Fixed submission:

#include <iostream>
#include <set>
#include <string>
using namespace std;

int main() {
  long long N;
  cin >> N;
  long long X[N], Y[N];
  char U[N];
  for (long long i = 0; i < N; i++)
    cin >> X[i] >> Y[i] >> U[i];
  long long min_ = 1000000000000;

  set<int> Up[500000], Right[500000], Down[500000], Left[500000];
  /*for (long long i = 0; i < N; i++){
    if (U[i] == 'U') {
      Up[250000 + X[i]].insert(Y[i]);
    }
    if (U[i] == 'R') {
      Right[250000 + Y[i]].insert(X[i]);
    }
    if (U[i] == 'D') {
      Down[250000 + X[i]].insert(Y[i]);
    }
    if (U[i] == 'L') {
      Left[250000 + Y[i]].insert(X[i]);
    }
  }
  for (int i = 0; i < 500000; i++){
    if (Up[i].empty() || Down[i].empty()) continue;
    auto itr = Up[i].begin();
    while (itr != Up[i].end()){
      auto itr2 = Down[i].lower_bound(*itr);
      long long b = (*itr2) - (*itr);
      if (b <= 0) continue;
      min_ = min(b, min_);
      itr++;
    }
  }
  for (int i = 0; i < 500000; i++){
    if (Right[i].empty() || Left[i].empty()) continue;
    auto itr = Right[i].begin();
    while (itr != Right[i].end()){
      auto itr2 = Left[i].lower_bound(*itr);
      long long b = (*itr2) - (*itr);
      if (b <= 0) continue;
      min_ = min(b, min_);
      itr++;
    }
  }*/

  set<int> UR[500000], RU[500000], RD[500000], DR[500000], DL[500000],
      LD[500000], LU[500000], UL[500000];
  /*for (long long i = 0; i < N; i++){
    if (U[i] == 'U'){
      if (X[i] + Y[i] < 250000 && X[i] + Y[i] >= -250000) UR[X[i] + Y[i] +
  250000].insert(X[i]); if (Y[i] - X[i] < 250000 && Y[i] - X[i] >= -250000)
  UL[Y[i] - X[i] + 250000].insert(X[i]);
    }
    if (U[i] == 'R'){
      if (X[i] + Y[i] < 250000 && X[i] + Y[i] >= -250000) RU[X[i] + Y[i] +
  250000].insert(X[i]); if (Y[i] - X[i] < 250000 && Y[i] - X[i] >= -250000)
  RD[Y[i] - X[i] + 250000].insert(X[i]);
    }
    if (U[i] == 'D'){
      if (X[i] + Y[i] < 250000 && X[i] + Y[i] >= -250000) DL[X[i] + Y[i] +
  250000].insert(X[i]); if (Y[i] - X[i] < 250000 && Y[i] - X[i] >= -250000)
  DR[Y[i] - X[i] + 250000].insert(X[i]);
    }
    if (U[i] == 'L'){
      if (X[i] + Y[i] < 250000 && X[i] + Y[i] >= -250000) LD[X[i] + Y[i] +
  250000].insert(X[i]); if (Y[i] - X[i] < 250000 && Y[i] - X[i] >= -250000)
  LU[Y[i] - X[i] + 250000].insert(X[i]);
    }
  }
  for (int i = 0; i < 500000; i++){
    if (UR[i].empty() || RU[i].empty()) continue;
    auto itr = RU[i].begin();
    while (itr != RU[i].end()){
      auto itr2 = UR[i].lower_bound(*itr);
      long long b = (*itr2) - (*itr);
      if (b <= 0) continue;
      min_ = min(2 * b, min_);
      itr++;
    }
  }
  for (int i = 0; i < 500000; i++){
    if (RD[i].empty() || DR[i].empty()) continue;
    auto itr = RD[i].begin();
    while (itr != RD[i].end()){
      auto itr2 = DR[i].lower_bound(*itr);
      long long b = (*itr2) - (*itr);
      if (b <= 0) continue;
      min_ = min(2 * b, min_);
      itr++;
    }
  }
  for (int i = 0; i < 500000; i++){
    if (DL[i].empty() || LD[i].empty()) continue;
    auto itr = DL[i].begin();
    while (itr != DL[i].end()){
      auto itr2 = LD[i].lower_bound(*itr);
      long long b = (*itr2) - (*itr);
      if (b <= 0) continue;
      min_ = min(2 * b, min_);
      itr++;
    }
  }
  for (int i = 0; i < 500000; i++){
    if (UL[i].empty() || LU[i].empty()) continue;
    auto itr = UL[i].begin();
    while (itr != DL[i].end()){
      auto itr2 = LU[i].lower_bound(*itr);
      long long b = (*itr2) - (*itr);
      if (b <= 0) continue;
      min_ = min(2 * b, min_);
      itr++;
    }
  }*/
  if (min_ == 1000000000000)
    cout << "SAFE" << endl;
  else
    cout << min_ * 5 << endl;
}