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

#include <bits/stdc++.h>
using namespace std;

#define for_(i, a, b) for (int i = a; i < b; ++i)
#define ALL(a) a.begin(), a.end()
#define INIT(a, b) memset(a, b, sizeof(a))
using lint = long long int;

int N, X[200005], Y[200005];
char P[200005];

int main() {
  cin >> N;
  for_(i, 0, N) cin >> X[i] >> Y[i] >> P[i];

  int ans = 1L << 30;
  {
    set<int> xs, ys;
    map<int, vector<int>> U, D, L, R;
    for_(i, 0, N) {
      xs.insert(X[i]);
      ys.insert(Y[i]);
      if (P[i] == 'U')
        U[X[i]].push_back(Y[i]);
      if (P[i] == 'D')
        D[X[i]].push_back(Y[i]);
      if (P[i] == 'L')
        L[Y[i]].push_back(X[i]);
      if (P[i] == 'R')
        R[Y[i]].push_back(X[i]);
    }

    for (int x : xs) {
      sort(ALL(U[x]));
      sort(ALL(D[x]));
    }

    for (int y : ys) {
      sort(ALL(L[y]));
      sort(ALL(R[y]));
    }

    for (int x : xs) {
      for (int y : U[x]) {
        auto it = upper_bound(ALL(D[x]), y);
        if (it != D[x].end())
          ans = min(ans, *it - y);
      }
    }
    for (int y : ys) {
      for (int x : R[y]) {
        auto it = upper_bound(ALL(L[y]), x);
        if (it != L[y].end())
          ans = min(ans, *it - x);
      }
    }
  }

  // rotate
  {
    for_(i, 0, N) {
      int x = X[i] - Y[i];
      int y = X[i] + Y[i];
      X[i] = x;
      Y[i] = y;
    }

    set<int> xs, ys;
    map<int, vector<int>> Ux, Uy, Dx, Dy, Lx, Ly, Rx, Ry;
    for_(i, 0, N) {
      xs.insert(X[i]);
      ys.insert(Y[i]);
      if (P[i] == 'U')
        Ux[X[i]].push_back(Y[i]), Uy[Y[i]].push_back(X[i]);
      if (P[i] == 'D')
        Dx[X[i]].push_back(Y[i]), Dy[Y[i]].push_back(X[i]);
      if (P[i] == 'L')
        Ly[Y[i]].push_back(X[i]), Lx[X[i]].push_back(Y[i]);
      if (P[i] == 'R')
        Ry[Y[i]].push_back(X[i]), Rx[X[i]].push_back(Y[i]);
    }

    for (int x : xs) {
      sort(ALL(Ux[x]));
      sort(ALL(Dx[x]));
      sort(ALL(Lx[x]));
      sort(ALL(Rx[x]));
    }

    for (int y : ys) {
      sort(ALL(Uy[y]));
      sort(ALL(Dy[y]));
      sort(ALL(Ly[y]));
      sort(ALL(Ry[y]));
    }

    for (int x : xs) {
      for (int y : Ux[x]) {
        auto it = upper_bound(ALL(Lx[x]), y);
        if (it != Lx[x].end())
          ans = min(ans, *it - y);
      }
      for (int y : Rx[x]) {
        auto it = upper_bound(ALL(Dx[x]), y);
        if (it != Dx[x].end())
          ans = min(ans, *it - y);
      }
    }

    for (int y : ys) {
      for (int x : Ry[y]) {
        auto it = upper_bound(ALL(Uy[y]), x);
        if (it != Uy[y].end())
          ans = min(ans, *it - x);
      }
      for (int x : Dy[y]) {
        auto it = upper_bound(ALL(Ly[y]), x);
        if (it != Ly[y].end())
          ans = min(ans, *it - x);
      }
    }
  }

  if (ans == 1L << 30)
    puts("SAFE");
  else
    cout << ans * 5 << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define for_(i, a, b) for (int i = a; i < b; ++i)
#define ALL(a) a.begin(), a.end()
#define INIT(a, b) memset(a, b, sizeof(a))
using lint = long long int;

int N, X[200005], Y[200005];
char P[200005];

int main() {
  cin >> N;
  for_(i, 0, N) cin >> X[i] >> Y[i] >> P[i];

  int ans = 1L << 30;
  {
    vector<vector<int>> U(200005), D(200005);
    vector<vector<int>> L(200005), R(200005);
    for_(i, 0, N) {
      if (P[i] == 'U')
        U[X[i]].push_back(Y[i]);
      if (P[i] == 'D')
        D[X[i]].push_back(Y[i]);
      if (P[i] == 'L')
        L[Y[i]].push_back(X[i]);
      if (P[i] == 'R')
        R[Y[i]].push_back(X[i]);
    }

    for_(v, 0, 200005) {
      sort(ALL(U[v]));
      sort(ALL(D[v]));
      sort(ALL(L[v]));
      sort(ALL(R[v]));
    }

    for_(v, 0, 200005) {
      int x = v;
      for (int y : U[x]) {
        auto it = upper_bound(ALL(D[x]), y);
        if (it != D[x].end())
          ans = min(ans, *it - y);
      }
      int y = v;
      for (int x : R[y]) {
        auto it = upper_bound(ALL(L[y]), x);
        if (it != L[y].end())
          ans = min(ans, *it - x);
      }
    }
  }

  // rotate
  {
    for_(i, 0, N) {
      int x = X[i] - Y[i];
      int y = X[i] + Y[i];
      X[i] = x + 200000;
      Y[i] = y + 200000;
    }

    vector<vector<int>> Ux(400005), Uy(400005), Dx(400005), Dy(400005);
    vector<vector<int>> Lx(400005), Ly(400005), Rx(400005), Ry(400005);
    for_(i, 0, N) {
      if (P[i] == 'U')
        Ux[X[i]].push_back(Y[i]), Uy[Y[i]].push_back(X[i]);
      if (P[i] == 'D')
        Dx[X[i]].push_back(Y[i]), Dy[Y[i]].push_back(X[i]);
      if (P[i] == 'L')
        Ly[Y[i]].push_back(X[i]), Lx[X[i]].push_back(Y[i]);
      if (P[i] == 'R')
        Ry[Y[i]].push_back(X[i]), Rx[X[i]].push_back(Y[i]);
    }

    for_(v, 0, 400005) {
      sort(ALL(Ux[v]));
      sort(ALL(Dx[v]));
      sort(ALL(Lx[v]));
      sort(ALL(Rx[v]));
      sort(ALL(Uy[v]));
      sort(ALL(Dy[v]));
      sort(ALL(Ly[v]));
      sort(ALL(Ry[v]));
    }

    for_(v, 0, 400005) {
      int x = v;
      for (int y : Ux[x]) {
        auto it = upper_bound(ALL(Lx[x]), y);
        if (it != Lx[x].end())
          ans = min(ans, *it - y);
      }
      for (int y : Rx[x]) {
        auto it = upper_bound(ALL(Dx[x]), y);
        if (it != Dx[x].end())
          ans = min(ans, *it - y);
      }

      int y = v;
      for (int x : Ry[y]) {
        auto it = upper_bound(ALL(Uy[y]), x);
        if (it != Uy[y].end())
          ans = min(ans, *it - x);
      }
      for (int x : Dy[y]) {
        auto it = upper_bound(ALL(Ly[y]), x);
        if (it != Ly[y].end())
          ans = min(ans, *it - x);
      }
    }
  }

  if (ans == 1L << 30)
    puts("SAFE");
  else
    cout << ans * 5 << endl;
}