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

// Utkarsh.25dec
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#define ll int
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define rep(i, n) for (ll i = 0; i < n; i++)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define vi vector<int>
#define vs vector<string>
#define vc vector<char>
#define vl vector<ll>
#define all(c) (c).begin(), (c).end()
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define deb(x) cerr << #x << ' ' << '=' << ' ' << x << '\\n'
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(val)  no. of elements strictly less than val
// s.find_by_order(i)  itertor to ith element (0 indexed)
typedef vector<vector<ll>> matrix;
ll power(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll modInverse(ll a) { return power(a, mod - 2); }
const int N = 500023;
bool vis[N];
vector<int> adj[N];
void solve() {
  ll n;
  cin >> n;
  vector<tuple<ll, ll, char>> v;
  map<pair<char, ll>, vl> xplusy;
  map<pair<char, ll>, vl> xminusy;
  map<pair<char, ll>, vl> xx;
  map<pair<char, ll>, vl> yy;
  set<ll> s;
  ll up = 0;
  ll down = 0;
  ll left = 0;
  ll right = 0;
  for (int i = 0; i < n; i++) {
    ll x, y;
    cin >> x >> y;
    char dir;
    cin >> dir;
    v.pb(make_tuple(x, y, dir));
  }
  sort(all(v));
  for (int i = 0; i < n; i++) {
    ll x = get<0>(v[i]);
    ll y = get<1>(v[i]);
    char dir = get<2>(v[i]);
    xplusy[mp(dir, x + y)].pb(x);
    xminusy[mp(dir, x - y)].pb(x);
    if (dir == 'D') {
      xx[mp(dir, x)].pb(y);
      s.insert(x);
    }
    yy[mp(dir, y)].pb(x);
    if (dir == 'U')
      up++;
    else if (dir == 'D')
      down++;
    else if (dir == 'L')
      left++;
    else
      right++;
  }
  for (auto it : s) {
    sort(all(xx[mp('D', it)]));
  }
  ll crash = mod;
  for (int i = 0; i < n; i++) {
    auto t = v[i];
    char dir = get<2>(v[i]);
    ll x = get<0>(v[i]);
    ll y = get<1>(v[i]);
    if (dir == 'R') {
      if (up > 0) {
        auto it1 = lower_bound(all(xplusy[mp('U', x + y)]), x);
        if (it1 != xplusy[mp('U', x + y)].end()) {
          ll diff = (*it1) - x;
          crash = min(crash, diff * 10);
        }
      }
      if (down > 0) {
        auto it2 = lower_bound(all(xminusy[mp('D', x - y)]), x);
        if (it2 != xminusy[mp('D', x - y)].end()) {
          ll diff = (*it2) - x;
          crash = min(crash, diff * 10);
        }
      }
      auto it3 = lower_bound(all(yy[mp('L', y)]), x);
      if (it3 != yy[mp('L', y)].end()) {
        ll diff = (*it3) - x;
        crash = min(crash, 5 * diff);
      }
    } else if (dir == 'L') {
      auto it1 = upper_bound(all(xplusy[mp('D', x + y)]), x);
      if (it1 != xplusy[mp('D', x + y)].begin()) {
        it1--;
        ll diff = abs((*it1) - x);
        crash = min(crash, diff * 10);
      }
      auto it2 = upper_bound(all(xminusy[mp('U', x - y)]), x);
      if (it2 != xminusy[mp('U', x - y)].begin()) {
        it2--;
        ll diff = abs((*it2) - x);
        crash = min(crash, diff * 10);
      }
      auto it3 = upper_bound(all(yy[mp('R', y)]), x);
      if (it3 != yy[mp('R', y)].begin()) {
        it3--;
        ll diff = abs((*it3) - x);
        crash = min(crash, 5 * diff);
      }
    } else if (dir == 'U') {
      auto it = lower_bound(all(xx[mp('D', x)]), y);
      if (it != xx[mp('D', x)].end()) {
        ll diff = abs((*it) - y);
        crash = min(crash, diff * 5);
      }
    }
  }
  if (crash == mod) {
    cout << "SAFE\\n";
  } else
    cout << crash << '\\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T = 1;
  // cin>>T;
  int t = 0;
  while (t++ < T) {
    // cout<<"Case #"<<t<<":"<<' ';
    solve();
    // cout<<'\\n';
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}

Error: TLE

Fixed submission:

// Utkarsh.25dec
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#define ll int
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define rep(i, n) for (ll i = 0; i < n; i++)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define vi vector<int>
#define vs vector<string>
#define vc vector<char>
#define vl vector<ll>
#define all(c) (c).begin(), (c).end()
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define deb(x) cerr << #x << ' ' << '=' << ' ' << x << '\\n'
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(val)  no. of elements strictly less than val
// s.find_by_order(i)  itertor to ith element (0 indexed)
typedef vector<vector<ll>> matrix;
ll power(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll modInverse(ll a) { return power(a, mod - 2); }
const int N = 500023;
bool vis[N];
vector<int> adj[N];
void solve() {
  ll n;
  cin >> n;
  vector<tuple<ll, ll, char>> v;
  map<pair<char, ll>, vl> xplusy;
  map<pair<char, ll>, vl> xminusy;
  map<pair<char, ll>, vl> xx;
  map<pair<char, ll>, vl> yy;
  set<pair<char, ll>> s;
  for (int i = 0; i < n; i++) {
    ll x, y;
    cin >> x >> y;
    char dir;
    cin >> dir;
    v.pb(make_tuple(x, y, dir));
    xplusy[mp(dir, x + y)].pb(x);
    xminusy[mp(dir, x - y)].pb(x);
    xx[mp(dir, x)].pb(y);
    yy[mp(dir, y)].pb(x);
    s.insert(mp(dir, x + y));
    s.insert(mp(dir, x - y));
    s.insert(mp(dir, x));
    s.insert(mp(dir, y));
  }
  for (auto it : s) {
    sort(all(xplusy[it]));
    sort(all(xminusy[it]));
    sort(all(xx[it]));
    sort(all(yy[it]));
  }
  ll crash = mod;
  for (int i = 0; i < n; i++) {
    auto t = v[i];
    char dir = get<2>(v[i]);
    ll x = get<0>(v[i]);
    ll y = get<1>(v[i]);
    if (dir == 'R') {
      auto it1 = lower_bound(all(xplusy[mp('U', x + y)]), x);
      if (it1 != xplusy[mp('U', x + y)].end()) {
        ll diff = (*it1) - x;
        crash = min(crash, diff * 10);
      }
      auto it2 = lower_bound(all(xminusy[mp('D', x - y)]), x);
      if (it2 != xminusy[mp('D', x - y)].end()) {
        ll diff = (*it2) - x;
        crash = min(crash, diff * 10);
      }
      auto it3 = lower_bound(all(yy[mp('L', y)]), x);
      if (it3 != yy[mp('L', y)].end()) {
        ll diff = (*it3) - x;
        crash = min(crash, 5 * diff);
      }
    } else if (dir == 'L') {
      auto it1 = upper_bound(all(xplusy[mp('D', x + y)]), x);
      if (it1 != xplusy[mp('D', x + y)].begin()) {
        it1--;
        ll diff = abs((*it1) - x);
        crash = min(crash, diff * 10);
      }
      auto it2 = upper_bound(all(xminusy[mp('U', x - y)]), x);
      if (it2 != xminusy[mp('U', x - y)].begin()) {
        it2--;
        ll diff = abs((*it2) - x);
        crash = min(crash, diff * 10);
      }
      auto it3 = upper_bound(all(yy[mp('R', y)]), x);
      if (it3 != yy[mp('R', y)].begin()) {
        it3--;
        ll diff = abs((*it3) - x);
        crash = min(crash, 5 * diff);
      }
    } else if (dir == 'U') {
      auto it = lower_bound(all(xx[mp('D', x)]), y);
      if (it != xx[mp('D', x)].end()) {
        ll diff = abs((*it) - y);
        crash = min(crash, diff * 5);
      }
    }
  }
  if (crash == mod) {
    cout << "SAFE\\n";
  } else
    cout << crash << '\\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T = 1;
  // cin>>T;
  int t = 0;
  while (t++ < T) {
    // cout<<"Case #"<<t<<":"<<' ';
    solve();
    // cout<<'\\n';
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}