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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = x; i < y; i++)
#define range(a) (a).begin(), (a).end()
#define print(A, x, n)                                                         \\
  rep(i, 0, n) { cout << (i ? " " : "") << A[i] x; }                           \\
  cout << endl;
#define pprint(A, y, m, n)                                                     \\
  rep(j, 0, m) { print(A[j], y, n); }
const long mod = 1e9 + 7;
const int siz = 2e5 + 1;
const long inf = 1e9;
int main() {
  int N;
  cin >> N;
  pair<int, int> v[N];
  int dir[N];
  map<int, set<pair<int, int>>> sp1[4], sp2[4];
  set<pair<int, int>> X[2][siz], Y[2][siz];
  rep(i, 0, N) {
    int x, y;
    char u;
    cin >> x >> y >> u;
    int d;
    if (u == 'U') {
      d = 0;
      X[0][x].insert({x, y});
    } else if (u == 'R') {
      d = 1;
      Y[0][y].insert({x, y});
    } else if (u == 'D') {
      d = 2;
      X[1][x].insert({x, y});
    } else {
      d = 3;
      Y[1][y].insert({x, y});
    }
    v[i] = {x, y};
    dir[i] = d;
    sp1[d][x + y].insert({x, y});
    sp2[d][x - y].insert({x, y});
  }
  int ans = inf;
  rep(i, 0, N) {
    int x = v[i].first;
    int y = v[i].second;
    int a, b;
    if (dir[i] == 0) {
      a = 1;
      b = 3;
      auto it = X[1][x].upper_bound({x, y});
      if (it != X[1][x].end())
        ans = min(ans, 5 * (it->second - y));
    } else if (dir[i] == 1) {
      a = 0;
      b = 2;
      auto it = Y[1][y].upper_bound({x, y});
      if (it != Y[1][y].end())
        ans = min(ans, 5 * (it->first - x));
    } else if (dir[i] == 2) {
      a = 3;
      b = 1;
      auto it = X[0][x].upper_bound({x, y});
      if (it != X[0][x].begin()) {
        it--;
        ans = min(ans, 5 * (y - it->second));
      }
    } else {
      a = 2;
      b = 0;
      auto it = Y[0][y].upper_bound({x, y});
      if (it != Y[0][y].begin()) {
        it--;
        ans = min(ans, 5 * (x - it->first));
      }
    }
    bool flag1 = true, flag2 = true;
    auto it1 = sp1[a][x + y].upper_bound({x, y});
    auto it2 = sp2[b][x - y].upper_bound({x, y});
    if (dir[i] == 0 || dir[i] == 3) {
      if (it1 == sp1[a][x + y].begin())
        flag1 = false;
      else
        it1--;
    }
    if (dir[i] == 2 || dir[i] == 3) {
      if (it2 == sp2[b][x - y].begin())
        flag2 = false;
      else
        it2--;
    }
    if (flag1 && it1 != sp1[a][x + y].end())
      ans = min(ans, 10 * abs(it1->first - x));
    if (flag2 && it2 != sp2[b][x - y].end())
      ans = min(ans, 10 * abs(it2->first - x));
  }
  if (ans == inf)
    cout << "SAFE" << endl;
  else
    cout << ans << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = x; i < y; i++)
#define range(a) (a).begin(), (a).end()
#define print(A, x, n)                                                         \\
  rep(i, 0, n) { cout << (i ? " " : "") << A[i] x; }                           \\
  cout << endl;
#define pprint(A, y, m, n)                                                     \\
  rep(j, 0, m) { print(A[j], y, n); }
const long mod = 1e9 + 7;
const int siz = 2e5;
const long inf = 1e9;
int main() {
  int N;
  cin >> N;
  pair<int, int> v[N];
  int dir[N];
  map<int, set<pair<int, int>>> sp1[4], sp2[4];
  set<pair<int, int>> X[2][siz], Y[2][siz];
  rep(i, 0, N) {
    int x, y;
    char u;
    cin >> x >> y >> u;
    int d;
    if (u == 'U') {
      d = 0;
      X[0][x].insert({x, y});
    } else if (u == 'R') {
      d = 1;
      Y[0][y].insert({x, y});
    } else if (u == 'D') {
      d = 2;
      X[1][x].insert({x, y});
    } else {
      d = 3;
      Y[1][y].insert({x, y});
    }
    v[i] = {x, y};
    dir[i] = d;
    sp1[d][x + y].insert({x, y});
    sp2[d][x - y].insert({x, y});
  }
  int ans = inf;
  rep(i, 0, N) {
    int x = v[i].first;
    int y = v[i].second;
    int a, b;
    if (dir[i] == 0) {
      a = 1;
      b = 3;
      auto it = X[1][x].upper_bound({x, y});
      if (it != X[1][x].end())
        ans = min(ans, 5 * (it->second - y));
    } else if (dir[i] == 1) {
      a = 0;
      b = 2;
      auto it = Y[1][y].upper_bound({x, y});
      if (it != Y[1][y].end())
        ans = min(ans, 5 * (it->first - x));
    } else if (dir[i] == 2) {
      a = 3;
      b = 1;
      auto it = X[0][x].upper_bound({x, y});
      if (it != X[0][x].begin()) {
        it--;
        ans = min(ans, 5 * (y - it->second));
      }
    } else {
      a = 2;
      b = 0;
      auto it = Y[0][y].upper_bound({x, y});
      if (it != Y[0][y].begin()) {
        it--;
        ans = min(ans, 5 * (x - it->first));
      }
    }
    bool flag1 = true, flag2 = true;
    auto it1 = sp1[a][x + y].upper_bound({x, y});
    auto it2 = sp2[b][x - y].upper_bound({x, y});
    if (dir[i] == 0 || dir[i] == 3) {
      if (it1 == sp1[a][x + y].begin())
        flag1 = false;
      else
        it1--;
    }
    if (dir[i] == 2 || dir[i] == 3) {
      if (it2 == sp2[b][x - y].begin())
        flag2 = false;
      else
        it2--;
    }
    if (flag1 && it1 != sp1[a][x + y].end())
      ans = min(ans, 10 * abs((it1->first) - x));
    if (flag2 && it2 != sp2[b][x - y].end())
      ans = min(ans, 10 * abs((it2->first) - x));
  }
  if (ans == inf)
    cout << "SAFE" << endl;
  else
    cout << ans << endl;
}