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

// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,avx512f")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <immintrin.h>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#ifdef DEBUG
#include "./Lib/Timer.hpp"
#include "./Lib/debug_VC.hpp"
#include "./Lib/sample.hpp"
#else
#define dump(...)
#endif

/* macro */
#define FOR(i, b, e) for (ll i = (ll)(b); i < (ll)(e); ++i)
#define RFOR(i, b, e) for (ll i = (ll)(e - 1); i >= (ll)(b); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define REPC(x, c) for (const auto &x : (c))
#define REPI2(it, b, e) for (auto it = (b); it != (e); ++it)
#define REPI(it, c) REPI2(it, (c).begin(), (c).end())
#define RREPI(it, c) REPI2(it, (c).rbegin(), (c).rend())
#define REPI_ERACE2(it, b, e) for (auto it = (b); it != (e);)
#define REPI_ERACE(it, c) REPI_ERACE2(it, (c).begin(), (c).end())
#define ALL(x) (x).begin(), (x).end()
#define cauto const auto &
/* macro func */
template <class T> inline auto sort(T &t) { std::sort(ALL(t)); }
template <class T> inline auto rsort(T &t) {
  std::sort((t).rbegin(), (t).rend());
}
template <class T> inline auto unique(T &t) {
  (t).erase(unique((t).begin(), (t).end()), (t).end());
}
template <class T, class S> inline auto chmax(T &t, const S &s) {
  if (s > t) {
    t = s;
    return true;
  }
  return false;
}
template <class T, class S> inline auto chmin(T &t, const S &s) {
  if (s < t) {
    t = s;
    return true;
  }
  return false;
}
inline auto BR() { std::cout << "\\n"; }

/* type define */
using ll = long long;
using PAIR = std::pair<ll, ll>;
using VS = std::vector<std::string>;
using VL = std::vector<long long>;
using VVL = std::vector<VL>;
using VVVL = std::vector<VVL>;
using VD = std::vector<double>;
template <class T> using V = std::vector<T>;

/* using std */
using std::cout;
constexpr char endl = '\\n';
using std::bitset;
using std::cin;
using std::list;
using std::map;
using std::multimap;
using std::multiset;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set;
using std::stack;
using std::string;
using std::unordered_map;
using std::unordered_multimap;
using std::unordered_multiset;
using std::unordered_set;
using std::vector;

/* Initial processing  */
struct Preprocessing {
  Preprocessing() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(0);
  };
} _Preprocessing;

/* Remove the source of the bug */
signed pow(signed, signed) {
  assert(false);
  return -1;
}

/* define hash */
namespace std {
template <> class hash<std::pair<ll, ll>> {
public:
  size_t operator()(const std::pair<ll, ll> &x) const {
    return hash<ll>()(1000000000 * x.first + x.second);
  }
};
} // namespace std

/* input */
template <class T> std::istream &operator>>(std::istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

/* constant value */
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;

//=============================================================================================;

constexpr ll ubn = 200010;
// constexpr ll ubn = 50;

auto solve_head(const V<PAIR> &rv, const V<PAIR> &lv, bool b = true) {
  ll sz = rv.size() + lv.size();
  V<pair<PAIR, ll>> v;
  v.reserve(sz);
  for (cauto[x, y] : rv) {
    v.emplace_back(b ? PAIR{x, y} : PAIR{y, x}, 0);
  }
  for (cauto[x, y] : lv) {
    v.emplace_back(b ? PAIR{x, y} : PAIR{y, x}, 1);
  }
  sort(v);
  ll ans = 1e10;
  VL mx(ubn + 10, -1);
  REPC(p, v) {
    auto [pp, b] = p;
    auto [x, y] = pp;
    if (b == 0) {
      mx[y] = x;
      continue;
    }
    if (mx[y] == -1) {
      continue;
    }
    chmin(ans, x - mx[y]);
  }
  return ans * 5;
}

auto solve_side(const V<PAIR> &av, const V<PAIR> &bv, bool b1 = true,
                bool b2 = true) {
  ll sz = av.size() + bv.size();
  V<pair<PAIR, ll>> v;
  v.reserve(sz);
  for (cauto[x, y] : av) {
    v.emplace_back(PAIR{x, y}, 0);
  }
  for (cauto[x, y] : bv) {
    v.emplace_back(PAIR{x, y}, 1);
  }
  if (b2) {
    sort(v);
  } else {
    rsort(v);
  }

  auto diffMin = [](const set<ll> &s, ll x) {
    auto itr = s.lower_bound(x);
    ll ans = 1e11;
    if (itr != s.end()) {
      chmin(ans, std::abs(x - *itr));
    }
    if (itr != s.begin()) {
      --itr;
      chmin(ans, std::abs(x - *itr));
    }
    return ans;
  };
  ll ans = 1e10;

  V<bool> exist(2 * ubn + 10);
  V<set<ll>> allX(2 * ubn + 10);
  V<set<ll>> allY(2 * ubn + 10);
  REPC(p, v) {
    auto [pp, b] = p;
    auto [x, y] = pp;
    ll key = (b1 ? x + y : x - y + ubn);
    if (b == 1) {
      exist[key] = true;
      allX[key].emplace(x);
      allY[key].emplace(y);
    } else {
      if (!exist[key]) {
        continue;
      }
      chmin(ans, diffMin(allX[key], x));
      chmin(ans, diffMin(allY[key], y));
    }
  }
  return ans * 10;
}

auto solve(ll n, const V<PAIR> &uv, const V<PAIR> &rv, const V<PAIR> &dv,
           const V<PAIR> &lv) {
  ll ans = 1e10;

  chmin(ans, solve_head(rv, lv));
  chmin(ans, solve_head(uv, dv, false));

  chmin(ans, solve_side(rv, dv, false, false));
  chmin(ans, solve_side(rv, uv, true, false));
  chmin(ans, solve_side(lv, dv, true, true));
  chmin(ans, solve_side(lv, uv, false, true));

  string ans_str = (ans > 1e9) ? "SAFE" : std::to_string(ans);
  return ans_str;
}
auto solve_c(ll n, const V<PAIR> &uv, const V<PAIR> &rv, const V<PAIR> &dv,
             const V<PAIR> &lv) {
  ll us = uv.size();
  ll rs = rv.size();
  ll ds = dv.size();
  ll ls = lv.size();
  ll ans = 1e10;
  for (cauto[x, y] : uv)
    for (cauto[s, t] : dv) {
      if (x == s && t > y) {
        chmin(ans, 5 * (t - y));
      }
    }
  for (cauto[x, y] : rv)
    for (cauto[s, t] : lv) {
      if (y == t && s > x) {
        chmin(ans, 5 * (s - x));
      }
    }
  for (cauto[x, y] : rv)
    for (cauto[s, t] : uv) {
      if (s > x && s - x == y - t) {
        chmin(ans, 10 * (y - t));
      }
    }
  for (cauto[x, y] : rv)
    for (cauto[s, t] : dv) {
      if (s > x && s - x == t - y) {
        chmin(ans, 10 * (t - y));
      }
    }
  for (cauto[x, y] : lv)
    for (cauto[s, t] : uv) {
      if (x > s && x - s == y - t) {
        chmin(ans, 10 * (y - t));
      }
    }
  for (cauto[x, y] : lv)
    for (cauto[s, t] : dv) {
      if (x > s && x - s == t - y) {
        chmin(ans, 10 * (t - y));
      }
    }

  string ans_str = (ans > 1e9) ? "SAFE" : std::to_string(ans);
  return ans_str;
}
signed main() {
  /**/
  ll n;
  cin >> n;
  V<PAIR> uv, rv, dv, lv;
  uv.reserve(n);
  rv.reserve(n);
  dv.reserve(n);
  lv.reserve(n);
  REP(_, n) {
    ll x, y;
    char c;
    cin >> x >> y >> c;
    (c == 'U' ? uv : c == 'R' ? rv : c == 'D' ? dv : lv).emplace_back(x, y);
  }

  auto ans = solve(n, uv, rv, dv, lv);
  cout << ans << endl;

  // auto ans_c = solve_c(n, uv, rv, dv, lv);
  // dump(ans_c);
  /*/
  auto gen = Sample::SampleGenerator();
  REP(_, 100) {
          auto [nv] = gen.generate(V<PAIR>(4, PAIR{0,10}));
          ll n = 0; REPC(x, nv) { n += x; }

          auto [ux, rx, dx, lx] = gen.generate(
                  V<PAIR>(nv[0], PAIR{0,10}),
                  V<PAIR>(nv[1], PAIR{0,10}),
                  V<PAIR>(nv[2], PAIR{0,10}),
                  V<PAIR>(nv[3], PAIR{0,10})
          );
          auto [uy, ry, dy, ly] = gen.generate(
                  V<PAIR>(nv[0], PAIR{0,10}),
                  V<PAIR>(nv[1], PAIR{0,10}),
                  V<PAIR>(nv[2], PAIR{0,10}),
                  V<PAIR>(nv[3], PAIR{0,10})
          );
          V<PAIR> uv, rv, dv, lv;
          uv.reserve(nv[0]);
          REP(i, nv[0]) { uv.emplace_back(ux[i], uy[i]); }
          rv.reserve(nv[1]);
          REP(i, nv[1]) { rv.emplace_back(rx[i], ry[i]); }
          dv.reserve(nv[2]);
          REP(i, nv[2]) { dv.emplace_back(dx[i], dy[i]); }
          lv.reserve(nv[3]);
          REP(i, nv[3]) { lv.emplace_back(lx[i], ly[i]); }
          auto ans = solve(n, uv, rv, dv, lv);
          auto ans_c = solve_c(n, uv, rv, dv, lv);
          if (ans != ans_c) {
                  cout << n << endl;
                  for (cauto[x, y] : uv) { cout << x << " " << y << " U" <<
  endl; } for (cauto[x, y] : rv) { cout << x << " " << y << " R" << endl; } for
  (cauto[x, y] : dv) { cout << x << " " << y << " D" << endl; } for (cauto[x, y]
  : lv) { cout << x << " " << y << " L" << endl; }

                  dump(ans, ans_c);
                  break;
          }
  }
  /**/
}


Error: TLE

Fixed submission:

// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,avx512f")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <immintrin.h>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#ifdef DEBUG
#include "./Lib/Timer.hpp"
#include "./Lib/debug_VC.hpp"
#include "./Lib/sample.hpp"
#else
#define dump(...)
#endif

/* macro */
#define FOR(i, b, e) for (ll i = (ll)(b); i < (ll)(e); ++i)
#define RFOR(i, b, e) for (ll i = (ll)(e - 1); i >= (ll)(b); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define REPC(x, c) for (const auto &x : (c))
#define REPI2(it, b, e) for (auto it = (b); it != (e); ++it)
#define REPI(it, c) REPI2(it, (c).begin(), (c).end())
#define RREPI(it, c) REPI2(it, (c).rbegin(), (c).rend())
#define REPI_ERACE2(it, b, e) for (auto it = (b); it != (e);)
#define REPI_ERACE(it, c) REPI_ERACE2(it, (c).begin(), (c).end())
#define ALL(x) (x).begin(), (x).end()
#define cauto const auto &
/* macro func */
template <class T> inline auto sort(T &t) { std::sort(ALL(t)); }
template <class T> inline auto rsort(T &t) {
  std::sort((t).rbegin(), (t).rend());
}
template <class T> inline auto unique(T &t) {
  (t).erase(unique((t).begin(), (t).end()), (t).end());
}
template <class T, class S> inline auto chmax(T &t, const S &s) {
  if (s > t) {
    t = s;
    return true;
  }
  return false;
}
template <class T, class S> inline auto chmin(T &t, const S &s) {
  if (s < t) {
    t = s;
    return true;
  }
  return false;
}
inline auto BR() { std::cout << "\\n"; }

/* type define */
using ll = long long;
using PAIR = std::pair<ll, ll>;
using VS = std::vector<std::string>;
using VL = std::vector<long long>;
using VVL = std::vector<VL>;
using VVVL = std::vector<VVL>;
using VD = std::vector<double>;
template <class T> using V = std::vector<T>;

/* using std */
using std::cout;
constexpr char endl = '\\n';
using std::bitset;
using std::cin;
using std::list;
using std::map;
using std::multimap;
using std::multiset;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set;
using std::stack;
using std::string;
using std::unordered_map;
using std::unordered_multimap;
using std::unordered_multiset;
using std::unordered_set;
using std::vector;

/* Initial processing  */
struct Preprocessing {
  Preprocessing() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(0);
  };
} _Preprocessing;

/* Remove the source of the bug */
signed pow(signed, signed) {
  assert(false);
  return -1;
}

/* define hash */
namespace std {
template <> class hash<std::pair<ll, ll>> {
public:
  size_t operator()(const std::pair<ll, ll> &x) const {
    return hash<ll>()(1000000000 * x.first + x.second);
  }
};
} // namespace std

/* input */
template <class T> std::istream &operator>>(std::istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

/* constant value */
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;

//=============================================================================================;

constexpr ll ubn = 200010;
// constexpr ll ubn = 50;

auto solve_head(const V<PAIR> &rv, const V<PAIR> &lv, bool b = true) {
  ll sz = rv.size() + lv.size();
  V<pair<PAIR, ll>> v;
  v.reserve(sz);
  for (cauto[x, y] : rv) {
    v.emplace_back(b ? PAIR{x, y} : PAIR{y, x}, 0);
  }
  for (cauto[x, y] : lv) {
    v.emplace_back(b ? PAIR{x, y} : PAIR{y, x}, 1);
  }
  sort(v);
  ll ans = 1e10;
  VL mx(ubn + 10, -1);
  REPC(p, v) {
    auto [pp, b] = p;
    auto [x, y] = pp;
    if (b == 0) {
      mx[y] = x;
      continue;
    }
    if (mx[y] == -1) {
      continue;
    }
    chmin(ans, x - mx[y]);
  }
  return ans * 5;
}

auto solve_side(const V<PAIR> &av, const V<PAIR> &bv, bool b1 = true,
                bool b2 = true) {
  ll sz = av.size() + bv.size();
  V<pair<PAIR, ll>> v;
  v.reserve(sz);
  for (cauto[x, y] : av) {
    v.emplace_back(PAIR{x, y}, 0);
  }
  for (cauto[x, y] : bv) {
    v.emplace_back(PAIR{x, y}, 1);
  }
  if (b2) {
    sort(v);
  } else {
    rsort(v);
  }

  auto diffMin = [](const set<ll> &s, ll x) {
    auto itr = s.lower_bound(x);
    ll ans = 1e11;
    if (itr != s.end()) {
      chmin(ans, std::abs(x - *itr));
    }
    if (itr != s.begin()) {
      --itr;
      chmin(ans, std::abs(x - *itr));
    }
    return ans;
  };
  ll ans = 1e10;

  V<bool> exist(2 * ubn + 10);
  V<set<ll>> allX(2 * ubn + 10);
  V<set<ll>> allY(2 * ubn + 10);
  REPC(p, v) {
    auto [pp, b] = p;
    auto [x, y] = pp;
    ll key = (b1 ? x + y : x - y + ubn);
    if (b == 1) {
      exist[key] = true;
      allX[key].emplace(x);
      allY[key].emplace(y);
    } else {
      if (!exist[key]) {
        continue;
      }
      chmin(ans, diffMin(allX[key], x));
      chmin(ans, diffMin(allY[key], y));
    }
  }
  return ans * 10;
}

auto solve(ll n, const V<PAIR> &uv, const V<PAIR> &rv, const V<PAIR> &dv,
           const V<PAIR> &lv) {
  ll ans = 1e10;

  chmin(ans, solve_head(rv, lv));
  chmin(ans, solve_head(uv, dv, false));

  chmin(ans, solve_side(rv, dv, false, false));
  chmin(ans, solve_side(rv, uv, true, false));
  chmin(ans, solve_side(lv, dv, true, true));
  chmin(ans, solve_side(lv, uv, false, true));

  string ans_str = (ans > 1e9) ? "SAFE" : std::to_string(ans);
  return ans_str;
}
auto solve_c(ll n, const V<PAIR> &uv, const V<PAIR> &rv, const V<PAIR> &dv,
             const V<PAIR> &lv) {
  ll us = uv.size();
  ll rs = rv.size();
  ll ds = dv.size();
  ll ls = lv.size();
  ll ans = 1e10;
  for (cauto[x, y] : uv)
    for (cauto[s, t] : dv) {
      if (x == s && t > y) {
        chmin(ans, 5 * (t - y));
      }
    }
  for (cauto[x, y] : rv)
    for (cauto[s, t] : lv) {
      if (y == t && s > x) {
        chmin(ans, 5 * (s - x));
      }
    }
  for (cauto[x, y] : rv)
    for (cauto[s, t] : uv) {
      if (s > x && s - x == y - t) {
        chmin(ans, 10 * (y - t));
      }
    }
  for (cauto[x, y] : rv)
    for (cauto[s, t] : dv) {
      if (s > x && s - x == t - y) {
        chmin(ans, 10 * (t - y));
      }
    }
  for (cauto[x, y] : lv)
    for (cauto[s, t] : uv) {
      if (x > s && x - s == y - t) {
        chmin(ans, 10 * (y - t));
      }
    }
  for (cauto[x, y] : lv)
    for (cauto[s, t] : dv) {
      if (x > s && x - s == t - y) {
        chmin(ans, 10 * (t - y));
      }
    }

  string ans_str = (ans > 1e9) ? "SAFE" : std::to_string(ans);
  return ans_str;
}
signed main() {
  /**/
  ll n;
  cin >> n;
  V<PAIR> uv, rv, dv, lv;
  uv.reserve(n);
  rv.reserve(n);
  dv.reserve(n);
  lv.reserve(n);
  REP(_, n) {
    ll x, y;
    char c;
    cin >> x >> y >> c;
    (c == 'U' ? uv : c == 'R' ? rv : c == 'D' ? dv : lv).emplace_back(x, y);
  }

  auto ans = solve(n, uv, rv, dv, lv);
  cout << ans << endl;

  auto ans_c = solve_c(n, uv, rv, dv, lv);
  dump(ans_c);
  /*/
  auto gen = Sample::SampleGenerator();
  REP(_, 100) {
          auto [nv] = gen.generate(V<PAIR>(4, PAIR{0,10}));
          ll n = 0; REPC(x, nv) { n += x; }

          auto [ux, rx, dx, lx] = gen.generate(
                  V<PAIR>(nv[0], PAIR{0,10}),
                  V<PAIR>(nv[1], PAIR{0,10}),
                  V<PAIR>(nv[2], PAIR{0,10}),
                  V<PAIR>(nv[3], PAIR{0,10})
          );
          auto [uy, ry, dy, ly] = gen.generate(
                  V<PAIR>(nv[0], PAIR{0,10}),
                  V<PAIR>(nv[1], PAIR{0,10}),
                  V<PAIR>(nv[2], PAIR{0,10}),
                  V<PAIR>(nv[3], PAIR{0,10})
          );
          V<PAIR> uv, rv, dv, lv;
          uv.reserve(nv[0]);
          REP(i, nv[0]) { uv.emplace_back(ux[i], uy[i]); }
          rv.reserve(nv[1]);
          REP(i, nv[1]) { rv.emplace_back(rx[i], ry[i]); }
          dv.reserve(nv[2]);
          REP(i, nv[2]) { dv.emplace_back(dx[i], dy[i]); }
          lv.reserve(nv[3]);
          REP(i, nv[3]) { lv.emplace_back(lx[i], ly[i]); }
          auto ans = solve(n, uv, rv, dv, lv);
          auto ans_c = solve_c(n, uv, rv, dv, lv);
          if (ans != ans_c) {
                  cout << n << endl;
                  for (cauto[x, y] : uv) { cout << x << " " << y << " U" <<
  endl; } for (cauto[x, y] : rv) { cout << x << " " << y << " R" << endl; } for
  (cauto[x, y] : dv) { cout << x << " " << y << " D" << endl; } for (cauto[x, y]
  : lv) { cout << x << " " << y << " L" << endl; }

                  dump(ans, ans_c);
                  break;
          }
  }
  /**/
}