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

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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;

const ll INF = 1e18;
ll ans = INF;

void f(map<int, vector<P>> X, int d1, int d2) {
  for (auto [x, mp] : X) {
    sort(all(mp));
    ll tmp = INF;
    for (auto [y, d] : mp) {
      if (d == d1)
        tmp = y;
      if (tmp == INF)
        continue;
      if (d == d2) {
        tmp = y - tmp;
        ans = min(ans, tmp);
        // cout << x << ", " << y << ", " << d << ": " << ans << endl;
        tmp = INF;
      }
    }
  }
  return;
}

void g(map<int, vector<P>> X, int d1, int d2) {
  for (auto [x, mp] : X) {
    sort(all(mp));
    ll tmp = INF;
    for (auto [y, d] : mp) {
      if (d == d1)
        tmp = y;
      if (tmp == INF)
        continue;
      if (d == d2) {
        tmp = y - tmp;
        ans = min(ans, tmp * 2);
        // cout << x << ", " << y << ", " << d << ": " << ans << endl;
        tmp = INF;
      }
    }
  }
  return;
}

void Main() {
  int n;
  cin >> n;
  map<int, vector<P>> X, Y, YpX, YmX;

  rep(i, n) {
    int x, y;
    char u;
    cin >> x >> y >> u;
    int d;
    if (u == 'U')
      d = 0;
    else if (u == 'R')
      d = 1;
    else if (u == 'D')
      d = 2;
    else if (u == 'L')
      d = 3;

    X[x].eb(y, d);
    Y[y].eb(x, d);
    YpX[y + x].eb(y, d);
    YmX[y - x].eb(y, d);
  }

  // cout << "X" << endl;
  f(X, 0, 2);
  // cout << "Y" << endl;
  f(Y, 1, 3);
  // cout << "Y+X" << endl;
  g(YpX, 0, 1);
  g(YpX, 3, 2);
  // cout << "Y-X" << endl;
  g(YmX, 0, 3);
  g(YmX, 1, 2);

  if (ans == INF)
    cout << "SAFE" << endl;
  else
    cout << ans * 5 << endl;
  return;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;

const ll INF = 1001001001001001001;

void Main() {
  int n;
  cin >> n;
  vector<P> U, D, R;
  map<int, vi> Dmap, Lp, Lm, Rp, Rm, Lmap;

  rep(i, n) {
    int x, y;
    char u;
    cin >> x >> y >> u;
    if (u == 'U') {
      U.eb(x, y);
    } else if (u == 'D') {
      D.eb(x, y);
      Dmap[x].pb(y);
    } else if (u == 'L') {
      Lp[y + x].pb(y);
      Lm[y - x].pb(y);
      Lmap[y].pb(x);
    } else if (u == 'R') {
      R.eb(x, y);
      Rp[y + x].pb(y);
      Rm[y - x].pb(y);
    }
  }

  ll ans = INF;
  ll tmp = INF;

  for (auto [x, y] : U) {
    if (!Dmap[x].empty()) {
      sort(all(Dmap[x]));
      auto it = lower_bound(all(Dmap[x]), y);
      if (it != Dmap[x].end()) {
        tmp = *it - y;
        ans = min(ans, tmp);
      }
      // cout << tmp << endl;
    }

    if (!Lm[y - x].empty()) {
      sort(all(Lm[y - x]));
      auto it = lower_bound(all(Lm[y - x]), y);
      if (it != Lm[y - x].end()) {
        tmp = (*it - y) * 2;
        ans = min(ans, tmp);
      }
      // cout << tmp << endl;
    }

    if (!Rp[y + x].empty()) {
      sort(all(Rp[y + x]));
      auto it = lower_bound(all(Rp[y + x]), y);
      if (it != Rp[y + x].end()) {
        tmp = (*it - y) * 2;
        ans = min(ans, tmp);
      }
      // cout << tmp << endl;
    }
  }

  for (auto [x, y] : D) {
    if (!Lp[y + x].empty()) {
      sort(all(Lp[y + x]));
      auto it = upper_bound(all(Lp[y + x]), y);
      if (it != Lp[y + x].begin()) {
        it--;
        tmp = (y - *it) * 2;
        ans = min(ans, tmp);
      }
      // cout << tmp << endl;
    }

    if (!Rm[y - x].empty()) {
      sort(all(Rm[y - x]));
      auto it = upper_bound(all(Rm[y - x]), y);
      if (it != Rm[y - x].begin()) {
        it--;
        tmp = (y - *it) * 2;
        ans = min(ans, tmp);
      }
      // cout << tmp << endl;
    }
  }

  for (auto [x, y] : R) {
    if (!Lmap[y].empty()) {
      sort(all(Lmap[y]));
      auto it = lower_bound(all(Lmap[y]), x);
      if (it != Lmap[y].end()) {
        tmp = *it - x;
        ans = min(ans, tmp);
      }
      // cout << x << " " << y << " " << tmp << endl;
    }
  }

  if (ans == INF)
    cout << "SAFE" << endl;
  else
    cout << ans * 5 << endl;
  return;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}