Problem p02605 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr)

#define ri(x)                                                                  \\
  i64 x;                                                                       \\
  cin >> x
#define rii(x, y)                                                              \\
  i64 x, y;                                                                    \\
  cin >> x >> y
#define ri3(x, y, z)                                                           \\
  i64 x, y, z;                                                                 \\
  cin >> x >> y >> z
#define ri4(x, y, z, u)                                                        \\
  i64 x, y, z, u;                                                              \\
  cin >> x >> y >> z >> u
#define rvi(x, n)                                                              \\
  vi x(n);                                                                     \\
  cin >> x
#define rvvi(x, n, m)                                                          \\
  vvi x(n, vi(m));                                                             \\
  cin >> x
#define rx(x, t)                                                               \\
  t x;                                                                         \\
  cin >> x

#define ws(x) cout << x << ' '
#define wl(x) cout << x << '\\n'
#define wln cout << '\\n'

#define dbg(x) cerr << '[' << #x << "]: " << x << '\\n'

#define fa(x, v) for (auto &x : v)
#define fi(i, n) for (i32 i = 0; i < n; i++)
#define fp(i, n) for (i32 i = 1; i < n; i++)
#define fr(i, n) for (i32 i = n - 1; i >= 0; i--)
#define fu(x)                                                                  \\
  ri(_x);                                                                      \\
  fi(x, _x)

#define l first
#define r second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define bit(x) ((u64)1 << (u32)(x))

using namespace std;

typedef uint32_t u32;
typedef uint64_t u64;
typedef int32_t i32;
typedef int64_t i64;
typedef vector<i64> vi;
typedef vector<vi> vvi;
typedef pair<i64, i64> pi;

template <typename T> inline istream &operator>>(istream &in, vector<T> &v) {
  fa(x, v) in >> x;
  return in;
}

template <typename T>
inline ostream &operator<<(ostream &out, const vector<T> &v) {
  fa(x, v) ws(x);
  wln;
  return out;
}

inline i32 ubound(const vector<i32> &a, const i32 &v) {
  i32 i = 0;
  i32 j = a.size();
  while (i < j) {
    i32 m = (i + j) / 2;
    if (a[m] <= v) {
      i = m + 1;
    } else {
      j = m;
    }
  }
  return i;
}

inline i32 lbound(const vector<i32> &a, const i32 &v) {
  i32 i = -1;
  i32 j = a.size() - 1;
  while (i < j) {
    i32 m = (i + j + 1) / 2;
    if (a[m] >= v) {
      j = m - 1;
    } else {
      i = m;
    }
  }
  return i;
}

int main() {
  fio;

  typedef tuple<i64, i64, char> Q;
  vector<Q> prs;
  unordered_map<char, unordered_map<i64, vector<i32>>> diags;
  unordered_map<char, unordered_map<i64, vector<i32>>> rdiags;
  unordered_map<char, unordered_map<i64, vector<i32>>> xaxis;
  unordered_map<char, unordered_map<i64, vector<i32>>> yaxis;

  ri(n);
  fi(t, n) {
    rii(x, y);
    rx(u, char);
    prs.emplace_back(x, y, u);
    xaxis[u][x].push_back(y);
    yaxis[u][y].push_back(x);
    if (u == 'R' || u == 'L') {
      diags[u][x + y].push_back(y - x);
      rdiags[u][y - x].push_back(x + y);
    }
  }
  fa(x, xaxis) fa(y, x.second) sort(all(y.second));
  fa(x, yaxis) fa(y, x.second) sort(all(y.second));
  fa(x, diags) fa(y, x.second) sort(all(y.second));
  fa(x, rdiags) fa(y, x.second) sort(all(y.second));

  i64 best = numeric_limits<int>().max();

  fi(t, n) {
    i64 x = get<0>(prs[t]);
    i64 y = get<1>(prs[t]);
    char u = get<2>(prs[t]);

    if (u == 'L') {
      auto &axis = yaxis['R'][y];
      i32 k = lbound(axis, x);
      if (k >= 0) {
        best = min(best, 5 * abs(axis[k] - x));
      }
    }
    if (u == 'U') {
      auto &axis = xaxis['D'][x];
      i32 k = ubound(axis, y);
      if (k < axis.size()) {
        best = min(best, 5 * abs(axis[k] - y));
      }

      auto &diag = diags['R'][x + y];
      i32 i = ubound(diag, y - x);
      if (i < diag.size()) {
        best = min(best, 5 * abs(diag[i] - (y - x)));
      }

      auto &rdiag = rdiags['L'][y - x];
      i32 j = ubound(rdiag, x + y);
      if (j < rdiag.size()) {
        best = min(best, 5 * abs(rdiag[j] - (x + y)));
      }
    }
    if (u == 'D') {
      auto &diag = diags['L'][x + y];
      i32 i = lbound(diag, y - x);
      if (i >= 0) {
        best = min(best, 5 * abs(diag[i] - (y - x)));
      }

      auto &rdiag = rdiags['R'][y - x];
      i32 j = lbound(rdiag, x + y);
      if (j >= 0) {
        best = min(best, 5 * abs(rdiag[j] - (x + y)));
      }
    }
  }

  if (best == numeric_limits<int>().max())
    wl("SAFE");
  else
    wl(best);

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr)

#define ri(x)                                                                  \\
  i64 x;                                                                       \\
  cin >> x
#define rii(x, y)                                                              \\
  i64 x, y;                                                                    \\
  cin >> x >> y
#define ri3(x, y, z)                                                           \\
  i64 x, y, z;                                                                 \\
  cin >> x >> y >> z
#define ri4(x, y, z, u)                                                        \\
  i64 x, y, z, u;                                                              \\
  cin >> x >> y >> z >> u
#define rvi(x, n)                                                              \\
  vi x(n);                                                                     \\
  cin >> x
#define rvvi(x, n, m)                                                          \\
  vvi x(n, vi(m));                                                             \\
  cin >> x
#define rx(x, t)                                                               \\
  t x;                                                                         \\
  cin >> x

#define ws(x) cout << x << ' '
#define wl(x) cout << x << '\\n'
#define wln cout << '\\n'

#define dbg(x) cerr << '[' << #x << "]: " << x << '\\n'

#define fa(x, v) for (auto &x : v)
#define fi(i, n) for (i32 i = 0; i < n; i++)
#define fp(i, n) for (i32 i = 1; i < n; i++)
#define fr(i, n) for (i32 i = n - 1; i >= 0; i--)
#define fu(x)                                                                  \\
  ri(_x);                                                                      \\
  fi(x, _x)

#define l first
#define r second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define bit(x) ((u64)1 << (u32)(x))

using namespace std;

typedef uint32_t u32;
typedef uint64_t u64;
typedef int32_t i32;
typedef int64_t i64;
typedef vector<i64> vi;
typedef vector<vi> vvi;
typedef pair<i64, i64> pi;

template <typename T> inline istream &operator>>(istream &in, vector<T> &v) {
  fa(x, v) in >> x;
  return in;
}

template <typename T>
inline ostream &operator<<(ostream &out, const vector<T> &v) {
  fa(x, v) ws(x);
  wln;
  return out;
}

i32 ubound(vector<i64> &a, i64 v) {
  i32 i = 0;
  i32 j = a.size();
  while (i < j) {
    i32 m = (i + j) / 2;
    if (a[m] <= v) {
      i = m + 1;
    } else {
      j = m;
    }
  }
  return i;
}

i32 lbound(vector<i64> &a, i64 v) {
  i32 i = -1;
  i32 j = a.size() - 1;
  while (i < j) {
    i32 m = (i + j + 1) / 2;
    if (a[m] >= v) {
      j = m - 1;
    } else {
      i = m;
    }
  }
  return i;
}

int main() {
  fio;

  typedef tuple<i64, i64, char> Q;
  vector<Q> prs;
  unordered_map<char, unordered_map<i64, vi>> diags;
  unordered_map<char, unordered_map<i64, vi>> rdiags;
  unordered_map<char, unordered_map<i64, vi>> xaxis;
  unordered_map<char, unordered_map<i64, vi>> yaxis;

  ri(n);
  fi(t, n) {
    rii(x, y);
    rx(u, char);
    prs.emplace_back(x, y, u);
    xaxis[u][x].push_back(y);
    yaxis[u][y].push_back(x);
    if (u == 'R' || u == 'L') {
      diags[u][x + y].push_back(y - x);
      rdiags[u][y - x].push_back(x + y);
    }
  }
  fa(x, xaxis) fa(y, x.second) sort(all(y.second));
  fa(x, yaxis) fa(y, x.second) sort(all(y.second));
  fa(x, diags) fa(y, x.second) sort(all(y.second));
  fa(x, rdiags) fa(y, x.second) sort(all(y.second));

  i64 best = numeric_limits<int>().max();

  fi(t, n) {
    i64 x = get<0>(prs[t]);
    i64 y = get<1>(prs[t]);
    char u = get<2>(prs[t]);

    if (u == 'L') {
      auto axis = yaxis['R'][y];
      i32 k = lbound(axis, x);
      if (k >= 0) {
        best = min(best, 5 * abs(axis[k] - x));
      }
    }
    if (u == 'U') {
      auto axis = xaxis['D'][x];
      i32 k = ubound(axis, y);
      if (k < axis.size()) {
        best = min(best, 5 * abs(axis[k] - y));
      }

      auto diag = diags['R'][x + y];
      i32 i = ubound(diag, y - x);
      if (i < diag.size()) {
        best = min(best, 5 * abs(diag[i] - (y - x)));
      }

      auto rdiag = rdiags['L'][y - x];
      i32 j = ubound(rdiag, x + y);
      if (j < rdiag.size()) {
        best = min(best, 5 * abs(rdiag[j] - (x + y)));
      }
    }
    if (u == 'D') {
      auto diag = diags['L'][x + y];
      i32 i = lbound(diag, y - x);
      if (i >= 0) {
        best = min(best, 5 * abs(diag[i] - (y - x)));
      }

      auto rdiag = rdiags['R'][y - x];
      i32 j = lbound(rdiag, x + y);
      if (j >= 0) {
        best = min(best, 5 * abs(rdiag[j] - (x + y)));
      }
    }
  }

  if (best == numeric_limits<int>().max())
    wl("SAFE");
  else
    wl(best);

  return 0;
}