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

#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x + modulo)) % modulo)
#define Inf 10000000000000002

long long get(map<int, vector<int>> M0, map<int, vector<int>> M1) {
  long long ret = Inf;
  for (auto &a : M1) {
    sort(a.second.begin(), a.second.end());
  }

  for (auto a : M0) {
    auto it = M1.lower_bound(a.first);
    if (it == M1.end())
      continue;
    if (it->first != a.first)
      continue;
    for (int i = 0; i < a.second.size(); i++) {
      auto it2 = upper_bound(it->second.begin(), it->second.end(), a.second[i]);
      if (it2 == it->second.end())
        continue;
      ret = min(ret, (long long)abs(a.second[i] - (*it2)));
    }
  }

  return ret * 5;
}

long long get2(map<int, vector<int>> M0, map<int, vector<int>> M1) {
  long long ret = Inf;
  for (auto &a : M1) {
    sort(a.second.begin(), a.second.end());
  }

  for (auto a : M0) {

    auto it = M1.lower_bound(a.first);
    if (it == M1.end())
      continue;
    if (it->first != a.first)
      continue;
    for (int i = 0; i < a.second.size(); i++) {
      auto it2 = upper_bound(it->second.begin(), it->second.end(), a.second[i]);
      // if(a.first==40)cout<<a.second[i]<<endl;
      if (it2 == it->second.end())
        continue;
      ret = min(ret, (long long)abs(a.second[i] - (*it2)));
    }
  }

  return ret * 10;
}

long long solve(int N, vector<int> X, vector<int> Y, vector<int> U) {

  vector<map<int, vector<int>>> M(4), M2(8);

  for (int i = 0; i < N; i++) {
    if (U[i] % 2 == 0)
      M[U[i]][Y[i]].push_back(X[i]);
    else
      M[U[i]][X[i]].push_back(Y[i]);

    if (U[i] == 0) {
      M2[0][X[i] - Y[i]].push_back(-X[i]);
      M2[6][X[i] + Y[i]].push_back(-X[i]);
    }
    if (U[i] == 1) {
      M2[3][X[i] - Y[i]].push_back(X[i]);
      M2[7][X[i] + Y[i]].push_back(-X[i]);
    }
    if (U[i] == 2) {
      M2[2][X[i] - Y[i]].push_back(X[i]);
      M2[4][X[i] + Y[i]].push_back(X[i]);
    }
    if (U[i] == 3) {
      M2[1][X[i] - Y[i]].push_back(-X[i]);
      M2[5][X[i] + Y[i]].push_back(X[i]);
    }
  }

  long long ans = Inf;

  ans = min(ans, get(M[0], M[2]));
  ans = min(ans, get(M[1], M[3]));

  for (int i = 0; i < 8; i += 2) {
    ans = min(ans, get2(M2[i + 1], M2[i]));
  }

  return ans;
}

/*
long long solve2(int N,vector<int> X,vector<int> Y,vector<int> U){
long long ret =Inf;

for(int i=0;i<N;i++){
        X[i]*=10;
        Y[i]*=10;
}

for(int _=0;_<100000;_++){
        for(int i=0;i<N;i++){
                if(U[i]==0)X[i]++;
                if(U[i]==1)Y[i]++;
                if(U[i]==2)X[i]--;
                if(U[i]==3)Y[i]--;
        }

        for(int i=0;i<N;i++){
                for(int j=i+1;j<N;j++){
                        if(X[i]==X[j]&&Y[i]==Y[j]){

                                return _+1;
                        }
                }
        }


}

return Inf;
}


unsigned long xor128() {
static unsigned long x=123456789, y=362436069, z=521288629, w=88675123;
unsigned long t=(x^(x<<11));
x=y; y=z; z=w;
return (w=(w^(w>>19))^(t^(t>>8)));
}
*/
int main() {

  int N;
  cin >> N;

  vector<int> X(N), Y(N);
  vector<int> U(N);
  set<pair<int, int>> S;
  for (int i = 0; i < N; i++) {
    char c;

    scanf("%d %d %c", &X[i], &Y[i], &c);

    if (c == 'R')
      U[i] = 0;
    if (c == 'U')
      U[i] = 1;
    if (c == 'L')
      U[i] = 2;
    if (c == 'D')
      U[i] = 3;
  }

  long long ans = solve(N, X, Y, U);
  if (ans >= 1000000000)
    cout << "SAFE" << endl;
  else
    cout << ans << endl;
  // cout<<ans<<','<<solve2(N,X,Y,U)<<endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x + modulo)) % modulo)
#define Inf 10000000000000002

long long get(map<int, vector<int>> M0, map<int, vector<int>> M1) {
  long long ret = Inf;
  for (auto a : M1) {
    sort(a.second.begin(), a.second.end());
  }

  for (auto a : M0) {
    auto it = M1.lower_bound(a.first);
    if (it->first != a.first)
      continue;
    for (int i = 0; i < a.second.size(); i++) {
      auto it2 = lower_bound(it->second.begin(), it->second.end(), a.second[i]);
      if (it2 == it->second.end())
        continue;
      ret = min(ret, (long long)abs(a.second[i] - (*it2)));
    }
  }

  return ret * 10 / 2;
}

long long get2(map<int, vector<int>> M0, map<int, vector<int>> M1) {
  long long ret = Inf;
  for (auto a : M1) {
    sort(a.second.begin(), a.second.end());
  }

  for (auto a : M0) {
    auto it = M1.lower_bound(a.first);
    if (it->first != a.first)
      continue;
    for (int i = 0; i < a.second.size(); i++) {
      auto it2 = lower_bound(it->second.begin(), it->second.end(), a.second[i]);
      if (it2 == it->second.end())
        continue;

      ret = min(ret, (long long)abs(a.second[i] - (*it2)));
    }
  }

  return ret * 10;
}

int main() {

  int N;
  cin >> N;

  vector<int> X(N), Y(N);
  vector<int> U(N);

  vector<map<int, vector<int>>> M(4);
  vector<map<int, vector<int>>> M2(8);
  for (int i = 0; i < N; i++) {
    char c;
    scanf("%d %d %c", &X[i], &Y[i], &c);

    if (c == 'R')
      U[i] = 0;
    if (c == 'U')
      U[i] = 1;
    if (c == 'L')
      U[i] = 2;
    if (c == 'D')
      U[i] = 3;
    if (U[i] % 2 == 0)
      M[U[i]][Y[i]].push_back(X[i]);
    else
      M[U[i]][X[i]].push_back(Y[i]);

    if (U[i] == 0) {
      M2[0][X[i] - Y[i]].push_back(-X[i]);
      M2[6][X[i] + Y[i]].push_back(-X[i]);
    }
    if (U[i] == 1) {
      M2[3][X[i] - Y[i]].push_back(X[i]);
      M2[7][X[i] + Y[i]].push_back(-X[i]);
    }
    if (U[i] == 2) {
      M2[2][X[i] - Y[i]].push_back(X[i]);
      M2[4][X[i] + Y[i]].push_back(X[i]);
    }
    if (U[i] == 3) {
      M2[1][X[i] - Y[i]].push_back(-X[i]);
      M2[5][X[i] + Y[i]].push_back(X[i]);
    }
  }

  long long ans = Inf;

  ans = min(ans, get(M[0], M[2]));
  ans = min(ans, get(M[1], M[3]));

  for (int i = 0; i < 8; i += 2) {
    ans = min(ans, get2(M2[i + 1], M2[i]));
  }

  if (ans >= 1000000000000)
    cout << "SAFE" << endl;
  else
    cout << ans << endl;

  return 0;
}