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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(v) v.size()
#define INF 100000000000000 // 10^14
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ll N;
  cin >> N;
  vector<ll> X(N), Y(N);
  vector<char> U(N);
  map<ll, map<ll, char>> yzahyou;
  map<ll, map<ll, char>> xzahyou;
  map<ll, map<ll, char>> wa;
  map<ll, map<ll, char>> sa;
  rep(i, N) {
    cin >> X.at(i) >> Y.at(i) >> U.at(i);
    yzahyou[Y.at(i)][X.at(i)] = U.at(i);
    xzahyou[X.at(i)][Y.at(i)] = U.at(i);
    wa[X.at(i) + Y.at(i)][Y.at(i)] = U.at(i);
    sa[X.at(i) - Y.at(i)][Y.at(i)] = U.at(i);
  }
  bool safe = true;
  ll time = INF;
  for (auto a : yzahyou) {
    char pd = 'X';
    ll pp = 0;
    for (auto b : a.second) {
      if (pd == 'R' && b.second == 'L') {
        chmin(time, (b.first - pp) * 5);
        safe = false;
      }
      pd = b.second;
      pp = b.first;
    }
  }
  for (auto a : xzahyou) {
    char pd = 'X';
    ll pp = 0;
    for (auto b : a.second) {
      if (pd == 'U' && b.second == 'D') {
        chmin(time, (b.first - pp) * 5);
        safe = false;
      }
      pd = b.second;
      pp = b.first;
    }
  }
  for (auto a : wa) {
    char pd = 'X';
    ll pp = 0;
    for (auto b : a.second) {
      if (pd == 'U' && b.second == 'R') {
        chmin(time, (b.first - pp) * 10);
        safe = false;
      }
      if (pd == 'L' && b.second == 'D') {
        chmin(time, (b.first - pp) * 10);
        safe = false;
      }
      pd = b.second;
      pp = b.first;
    }
  }
  for (auto a : sa) {
    char pd = 'X';
    ll pp = 0;
    for (auto b : a.second) {
      if (pd == 'U' && b.second == 'L') {
        chmin(time, (b.first - pp) * 10);
        safe = false;
      }
      if (pd == 'R' && b.second == 'D') {
        chmin(time, (b.first - pp) * 10);
        safe = false;
      }
      pd = b.second;
      pp = b.first;
    }
  }
  if (safe) {
    cout << "SAFE" << endl;
  } else {
    cout << time << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(v) v.size()
#define INF 100000000000000 // 10^14
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ll N;
  cin >> N;
  vector<ll> X(N), Y(N);
  vector<char> U(N);
  map<ll, set<pair<ll, char>>> yzahyou;
  map<ll, set<pair<ll, char>>> xzahyou;
  map<ll, set<pair<ll, char>>> wa;
  map<ll, set<pair<ll, char>>> sa;
  rep(i, N) {
    cin >> X.at(i) >> Y.at(i) >> U.at(i);
    yzahyou[Y.at(i)].insert(make_pair(i, U.at(i)));
    xzahyou[X.at(i)].insert(make_pair(i, U.at(i)));
    wa[X.at(i) + Y.at(i)].insert(make_pair(i, U.at(i)));
    sa[X.at(i) - Y.at(i)].insert(make_pair(i, U.at(i)));
  }
  bool safe = true;
  ll time = INF;
  rep(i, N) {
    if (sz(yzahyou[Y.at(i)]) > 1) {
      if (U.at(i) == 'U' || U.at(i) == 'D') {
        continue;
      }
      if (U.at(i) == 'R') {
        for (auto p : yzahyou[Y.at(i)]) {
          if (p.first == i) {
            continue;
          }
          if (U.at(p.first) == 'L' && X.at(i) < X.at(p.first)) {
            safe = false;
            chmin(time, (X.at(p.first) - X.at(i)) * 5);
          }
        }
      }
      if (U.at(i) == 'L') {
        for (auto p : yzahyou[Y.at(i)]) {
          if (p.first == i) {
            continue;
          }
          if (U.at(p.first) == 'R' && X.at(i) > X.at(p.first)) {
            safe = false;
            chmin(time, (X.at(i) - X.at(p.first)) * 5);
          }
        }
      }
    }
  }
  rep(i, N) {
    if (sz(xzahyou[X.at(i)]) > 1) {
      if (U.at(i) == 'R' || U.at(i) == 'L') {
        continue;
      }
      if (U.at(i) == 'U') {
        for (auto p : xzahyou[X.at(i)]) {
          if (p.first == i) {
            continue;
          }
          if (U.at(p.first) == 'D' && Y.at(i) < Y.at(p.first)) {
            safe = false;
            chmin(time, (Y.at(p.first) - Y.at(i)) * 5);
          }
        }
      }
      if (U.at(i) == 'D') {
        for (auto p : xzahyou[X.at(i)]) {
          if (p.first == i) {
            continue;
          }
          if (U.at(p.first) == 'U' && Y.at(i) > Y.at(p.first)) {
            safe = false;
            chmin(time, (Y.at(i) - Y.at(p.first)) * 5);
          }
        }
      }
    }
  }
  rep(i, N) {
    if (sz(wa[X.at(i) + Y.at(i)]) > 1) {
      if (U.at(i) == 'U') {
        for (auto p : wa[X.at(i) + Y.at(i)]) {
          if (p.first == i) {
            continue;
          }
          if (U.at(p.first) == 'R' && Y.at(p.first) > Y.at(i)) {
            safe = false;
            chmin(time, (Y.at(p.first) - Y.at(i)) * 10);
          }
        }
      }
      if (U.at(i) == 'R') {
        for (auto p : wa[X.at(i) + Y.at(i)]) {
          if (p.first == i) {
            continue;
          }
          if (U.at(p.first) == 'U' && Y.at(p.first) < Y.at(i)) {
            safe = false;
            chmin(time, (Y.at(i) - Y.at(p.first)) * 10);
          }
        }
      }
      if (U.at(i) == 'D') {
        for (auto p : wa[X.at(i) + Y.at(i)]) {
          if (p.first == i) {
            continue;
          }
          if (U.at(p.first) == 'L' && Y.at(p.first) < Y.at(i)) {
            safe = false;
            chmin(time, (Y.at(i) - Y.at(p.first)) * 10);
          }
        }
      }
      if (U.at(i) == 'L') {
        for (auto p : wa[X.at(i) + Y.at(i)]) {
          if (p.first == i) {
            continue;
          }
          if (U.at(p.first) == 'D' && Y.at(p.first) > Y.at(i)) {
            safe = false;
            chmin(time, (Y.at(p.first) - Y.at(i)) * 10);
          }
        }
      }
    }
  }
  rep(i, N) {
    if (sz(sa[X.at(i) - Y.at(i)]) > 1) {
      if (U.at(i) == 'U') {
        for (auto p : sa[X.at(i) - Y.at(i)]) {
          if (p.first == i) {
            continue;
          }
          if (U.at(p.first) == 'L' && Y.at(p.first) > Y.at(i)) {
            safe = false;
            chmin(time, (Y.at(p.first) - Y.at(i)) * 10);
          }
        }
      }
      if (U.at(i) == 'L') {
        for (auto p : sa[X.at(i) - Y.at(i)]) {
          if (p.first == i) {
            continue;
          }
          if (U.at(p.first) == 'U' && Y.at(p.first) < Y.at(i)) {
            safe = false;
            chmin(time, (Y.at(i) - Y.at(p.first)) * 10);
          }
        }
      }
      if (U.at(i) == 'R') {
        for (auto p : sa[X.at(i) - Y.at(i)]) {
          if (p.first == i) {
            continue;
          }
          if (U.at(p.first) == 'D' && Y.at(p.first) > Y.at(i)) {
            safe = false;
            chmin(time, (Y.at(p.first) - Y.at(i)) * 10);
          }
        }
      }
      if (U.at(i) == 'D') {
        for (auto p : sa[X.at(i) - Y.at(i)]) {
          if (p.first == i) {
            continue;
          }
          if (U.at(p.first) == 'R' && Y.at(p.first) < Y.at(i)) {
            safe = false;
            chmin(time, (Y.at(i) - Y.at(p.first)) * 10);
          }
        }
      }
    }
  }
  if (safe) {
    cout << "SAFE" << endl;
  } else {
    cout << time << endl;
  }
}