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

#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// typedef tree <int,null_type,less <int>, rb_tree_tag,
// tree_order_statistics_node_update> indexed_set;
//  #include <ext/rope>
//  using namespace __gnu_cxx;
typedef long long int ll;
typedef long double ld;
#define FAST                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define rep(g, i, n) for (ll g = i; g < n; g++)
#define rev(g, n, i) for (ll g = n - 1; g >= i; g--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define mxe(v) *max_element(v.begin(), v.end())
#define mne(v) *min_element(v.begin(), v.end())
#define ve vector
#define lb lower_bound
#define ub upper_bound
#define bb break
#define cc continue
#define rem 1000000007
#define PI 3.141592653589793238462643383279502
ll power(ll x, ll y, ll p) {
  ll res = 1; // Initialize result
  x = x % p;  // Update x if it is more than or
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}
ll modInverse(ll n, ll p) { return power(n, p - 2, p); }
struct point {
  ll x, y;
};
int main() {
  FAST
      // freopen("input1.txt", "r", stdin);
      // freopen("output.txt", "w", stdout);
      /*ll tests;
  cin>>tests;
  rep (gg,0,tests)
  {}*/
      ll n;
  cin >> n;
  ve<pair<ll, ll>> arr[4]; // URDL
  unordered_map<char, ll> ind;
  ind['U'] = 0;
  ind['R'] = 1;
  ind['D'] = 2;
  ind['L'] = 3;
  rep(i, 0, n) {
    ll x, y;
    char z;
    cin >> x >> y >> z;
    arr[ind[z]].pb({x, y});
  }
  ll ans = 1e10;

  // RL clash
  unordered_map<ll, ve<ll>> mrl1;
  unordered_map<ll, ve<ll>> mrl2;
  for (auto x : arr[1]) {
    mrl1[x.second].pb(x.first);
  }
  for (auto x : arr[3]) {
    mrl2[x.second].pb(x.first);
  }
  for (auto x : mrl1) {
    sort(all(mrl1[x.first]));
  }
  for (auto x : mrl2) {
    sort(all(mrl2[x.first]));
  }
  for (auto x : mrl1) {
    for (auto y : x.second) {
      ll ind1 = lb(all(mrl2[x.first]), y) - mrl2[x.first].begin();
      if (ind1 < mrl2[x.first].size()) {
        // cout << "RL\\n";
        ans = min(ans, (mrl2[x.first][ind1] - y) * 5);
      }
    }
  }

  // UD clash
  unordered_map<ll, ve<ll>> mud1;
  unordered_map<ll, ve<ll>> mud2;
  for (auto x : arr[0]) {
    mud1[x.first].pb(x.second);
  }
  for (auto x : arr[2]) {
    mud2[x.first].pb(x.second);
  }
  for (auto x : mud1) {
    sort(all(mud1[x.first]));
  }
  for (auto x : mud2) {
    sort(all(mud2[x.first]));
  }
  for (auto x : mud1) {
    for (auto y : x.second) {
      ll ind1 = lb(all(mud2[x.first]), y) - mud2[x.first].begin();
      if (ind1 < mud2[x.first].size()) {
        // cout << "UD\\n";
        ans = min(ans, (mud2[x.first][ind1] - y) * 5);
      }
    }
  }

  // RD clash
  unordered_map<ll, ve<ll>> mrd1;
  unordered_map<ll, ve<ll>> mrd2;
  for (auto &x : arr[1]) {
    mrd1[x.first - x.second].pb(x.first);
  }
  for (auto &x : arr[2]) {
    mrd2[x.first - x.second].pb(x.first);
  }
  for (auto x : mrd1) {
    sort(all(mrd1[x.first]));
  }
  for (auto x : mrd2) {
    sort(all(mrd2[x.first]));
  }
  for (auto x : mrd1) {
    if (mrd2[x.first].size() > 0) {
      for (auto a : x.second) {
        ll ind1 = lb(all(mrd2[x.first]), a) - mrd2[x.first].begin();
        if (ind1 < mrd2[x.first].size()) {
          // cout << "UD\\n";
          ans = min(ans, (mrd2[x.first][ind1] - a) * 10);
        }
      }
    }
  }

  // DL clash
  unordered_map<ll, ve<ll>> mdl1;
  unordered_map<ll, ve<ll>> mdl2;
  for (auto &x : arr[2]) {
    mdl1[x.first + x.second].pb(x.first);
  }
  for (auto &x : arr[3]) {
    mdl2[x.first + x.second].pb(x.first);
  }
  for (auto x : mdl1) {
    sort(all(mdl1[x.first]));
  }
  for (auto x : mdl2) {
    sort(all(mdl2[x.first]));
  }
  for (auto x : mdl1) {
    if (mdl2[x.first].size() > 0) {
      for (auto a : x.second) {
        ll ind1 = lb(all(mdl2[x.first]), a) - mdl2[x.first].begin();
        if (ind1 < mdl2[x.first].size()) {
          // cout << "UD\\n";
          ans = min(ans, (mdl2[x.first][ind1] - a) * 10);
        }
      }
    }
  }

  // UL clash
  unordered_map<ll, ve<ll>> mul1;
  unordered_map<ll, ve<ll>> mul2;
  for (auto &x : arr[0]) {
    mul1[x.first - x.second].pb(x.first);
  }
  for (auto &x : arr[3]) {
    mul2[x.first - x.second].pb(x.first);
  }
  for (auto x : mul1) {
    sort(all(mul1[x.first]));
  }
  for (auto x : mul2) {
    sort(all(mul2[x.first]));
  }
  for (auto x : mul1) {
    if (mul2[x.first].size() > 0) {
      for (auto a : x.second) {
        ll ind1 = lb(all(mul2[x.first]), a) - mul2[x.first].begin();
        if (ind1 < mul2[x.first].size()) {
          // cout << "UD\\n";
          ans = min(ans, (mul2[x.first][ind1] - a) * 10);
        }
      }
    }
  }

  // RU clash
  unordered_map<ll, ve<ll>> mru1;
  unordered_map<ll, ve<ll>> mru2;
  for (auto &x : arr[1]) {
    mru1[x.first + x.second].pb(x.first);
  }
  for (auto &x : arr[0]) {
    mru2[x.first + x.second].pb(x.first);
  }
  for (auto x : mru1) {
    sort(all(mru1[x.first]));
  }
  for (auto x : mru2) {
    sort(all(mru2[x.first]));
  }
  for (auto x : mru1) {
    if (mru2[x.first].size() > 0) {
      for (auto a : x.second) {
        ll ind1 = lb(all(mru2[x.first]), a) - mru2[x.first].begin();
        if (ind1 < mru2[x.first].size()) {
          // cout << "UD\\n";
          ans = min(ans, (mru2[x.first][ind1] - a) * 10);
        }
      }
    }
  }

  if (ans == 1e10) {
    cout << "SAFE\\n";
  } else {
    cout << ans << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// typedef tree <int,null_type,less <int>, rb_tree_tag,
// tree_order_statistics_node_update> indexed_set;
//  #include <ext/rope>
//  using namespace __gnu_cxx;
typedef long long int ll;
typedef long double ld;
#define FAST                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define rep(g, i, n) for (ll g = i; g < n; g++)
#define rev(g, n, i) for (ll g = n - 1; g >= i; g--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define mxe(v) *max_element(v.begin(), v.end())
#define mne(v) *min_element(v.begin(), v.end())
#define ve vector
#define lb lower_bound
#define ub upper_bound
#define bb break
#define cc continue
#define rem 1000000007
#define PI 3.141592653589793238462643383279502
ll power(ll x, ll y, ll p) {
  ll res = 1; // Initialize result
  x = x % p;  // Update x if it is more than or
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}
ll modInverse(ll n, ll p) { return power(n, p - 2, p); }
struct point {
  ll x, y;
};
int main() {
  FAST
      // freopen("input1.txt", "r", stdin);
      // freopen("output.txt", "w", stdout);
      /*ll tests;
  cin>>tests;
  rep (gg,0,tests)
  {}*/
      ll n;
  cin >> n;
  ve<pair<ll, ll>> arr[4]; // URDL
  unordered_map<char, ll> ind;
  ind['U'] = 0;
  ind['R'] = 1;
  ind['D'] = 2;
  ind['L'] = 3;
  rep(i, 0, n) {
    ll x, y;
    char z;
    cin >> x >> y >> z;
    arr[ind[z]].pb({x, y});
  }
  ll ans = 1e10;

  // RL clash
  unordered_map<ll, ve<ll>> mrl1;
  unordered_map<ll, ve<ll>> mrl2;
  for (auto x : arr[1]) {
    mrl1[x.second].pb(x.first);
  }
  for (auto x : arr[3]) {
    mrl2[x.second].pb(x.first);
  }
  for (auto x : mrl1) {
    sort(all(mrl1[x.first]));
  }
  for (auto x : mrl2) {
    sort(all(mrl2[x.first]));
  }
  for (auto x : mrl1) {
    for (auto y : x.second) {
      ll ind1 = lb(all(mrl2[x.first]), y) - mrl2[x.first].begin();
      if (ind1 < mrl2[x.first].size()) {
        // cout << "RL\\n";
        ans = min(ans, (mrl2[x.first][ind1] - y) * 5);
      }
    }
  }

  // UD clash
  unordered_map<ll, ve<ll>> mud1;
  unordered_map<ll, ve<ll>> mud2;
  for (auto x : arr[0]) {
    mud1[x.first].pb(x.second);
  }
  for (auto x : arr[2]) {
    mud2[x.first].pb(x.second);
  }
  for (auto x : mud1) {
    sort(all(mud1[x.first]));
  }
  for (auto x : mud2) {
    sort(all(mud2[x.first]));
  }
  for (auto x : mud1) {
    for (auto y : x.second) {
      ll ind1 = lb(all(mud2[x.first]), y) - mud2[x.first].begin();
      if (ind1 < mud2[x.first].size()) {
        // cout << "UD\\n";
        ans = min(ans, (mud2[x.first][ind1] - y) * 5);
      }
    }
  }

  // RD clash
  unordered_map<ll, ve<ll>> mrd1;
  unordered_map<ll, ve<ll>> mrd2;
  for (auto &x : arr[2]) {
    mrd1[x.first - x.second].pb(x.first);
  }
  for (auto &x : arr[1]) {
    mrd2[x.first - x.second].pb(x.first);
  }
  for (auto x : mrd1) {
    if (mrd2[x.first].size() > 0) {
      for (auto a : x.second) {
        for (auto b : mrd2[x.first]) {
          if (a - b > 0)

          {
            // cout << "RD\\n";
            ans = min(ans, (a - b) * 10);
          }
        }
      }
    }
  }

  // DL clash
  unordered_map<ll, ve<ll>> mdl1;
  unordered_map<ll, ve<ll>> mdl2;
  for (auto &x : arr[2]) {
    mdl1[x.first + x.second].pb(x.first);
  }
  for (auto &x : arr[3]) {
    mdl2[x.first + x.second].pb(x.first);
  }
  for (auto x : mdl1) {
    if (mdl2[x.first].size() > 0) {
      for (auto a : x.second) {
        for (auto b : mdl2[x.first]) {
          if (b - a > 0) {
            // cout << "DL\\n";
            ans = min(ans, (b - a) * 10);
          }
        }
      }
    }
  }

  // UL clash
  unordered_map<ll, ve<ll>> mul1;
  unordered_map<ll, ve<ll>> mul2;
  for (auto &x : arr[0]) {
    mul1[x.first - x.second].pb(x.first);
  }
  for (auto &x : arr[3]) {
    mul2[x.first - x.second].pb(x.first);
  }
  for (auto x : mul1) {
    if (mul2[x.first].size() > 0) {
      for (auto a : x.second) {
        for (auto b : mul2[x.first]) {
          if (b - a > 0) {
            // cout << "UL\\n";
            ans = min(ans, (b - a) * 10);
          }
        }
      }
    }
  }

  // RU clash
  unordered_map<ll, ve<ll>> mru1;
  unordered_map<ll, ve<ll>> mru2;
  for (auto &x : arr[0]) {
    mru1[x.first + x.second].pb(x.first);
  }
  for (auto &x : arr[1]) {
    mru2[x.first + x.second].pb(x.first);
  }
  for (auto x : mru1) {
    if (mru2[x.first].size() > 0) {
      for (auto a : x.second) {
        for (auto b : mru2[x.first]) {
          if (a - b > 0) {
            // cout << "RU\\n";
            ans = min(ans, (a - b) * 10);
          }
        }
      }
    }
  }

  if (ans == 1e10) {
    cout << "SAFE\\n";
  } else {
    cout << ans << "\\n";
  }
}