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

#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

#define PI 3.1415926535897932384626433832795

template <typename X> bool max_u(X &m, X v) {
  if (m < v) {
    m = v;
    return true;
  }
  return false;
}

template <typename X> bool min_u(X &m, X v) {
  if (m > v) {
    m = v;
    return true;
  }
  return false;
}

struct plane_t {
  int x, y, d;
};

#define TIME_INF 9876543210

struct solve {
  solve() {}
  ll check_h(vector<plane_t> &plane) {
    ll ret = TIME_INF;
    map<int, vpii> m;
    for (auto &mp : plane) {
      if (mp.d == 0 || mp.d == 2)
        m[mp.y].push_back(make_pair(mp.x, mp.d));
    }
    for (auto &mm : m) {
      auto &mmv = mm.second;
      sort(mmv.begin(), mmv.end());
      ll prev_x = -9999999, prev_d = 2;
      for (auto &mvp : mmv) {
        if (prev_d == 0 && mvp.second == 2) {
          min_u(ret, (mvp.first - prev_x) * 10 / 2);
        }
        prev_x = mvp.first;
        prev_d = mvp.second;
      }
    }
    return ret;
  }
  ll check_rud(vector<plane_t> &plane) {
    ll ret = TIME_INF;
    map<int, vpii> m;
    int n = plane.size();

    for (int i = 0; i < n; i++) {
      auto &mp = plane[i];
      ll c = -mp.x - mp.y;
      if (mp.d == 0 || mp.d == 1)
        m[c].push_back(make_pair(mp.x, mp.d));
    }
    for (auto &mm : m) {
      auto &mmv = mm.second;
      sort(mmv.begin(), mmv.end());
      ll last_rx = -999999;
      for (auto &mvp : mmv) {
        if (mvp.second == 1 && last_rx != -999999) {
          min_u(ret, (mvp.first - last_rx) * 10);
        }
        if (mvp.second == 0) {
          last_rx = mvp.first;
        }
      }
    }
    return ret;
  }
  void rot(vector<plane_t> &plane) {
    for (auto &m : plane) {
      auto t = m;
      m.x = -t.y;
      m.y = t.x;
      m.d = (t.d + 1) % 4;
    }
  }
  void operator()(vector<plane_t> &plane) {
    ll ans = TIME_INF;

    for (int i = 0; i < 4; i++) {
      min_u(ans, check_h(plane));
      min_u(ans, check_rud(plane));
      rot(plane);
    }
    if (ans != TIME_INF)
      cout << ans << "\\n";
    else
      cout << "SAFE\\n";
  }
};

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  for (;;) {
    int n;
    cin >> n;
    if (cin.fail())
      break;
    vector<plane_t> plane(n);
    for (auto &m : plane) {
      int x, y, d = 0;
      string u;
      cin >> x >> y >> u;
      if (u == "R")
        d = 0;
      if (u == "U")
        d = 1;
      if (u == "L")
        d = 2;
      if (u == "D")
        d = 3;
      m = plane_t{x, y, d};
    }
    solve()(plane);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

#define PI 3.1415926535897932384626433832795

template <typename X> bool max_u(X &m, X v) {
  if (m < v) {
    m = v;
    return true;
  }
  return false;
}

template <typename X> bool min_u(X &m, X v) {
  if (m > v) {
    m = v;
    return true;
  }
  return false;
}

struct plane_t {
  int x, y, d;
};

#define TIME_INF 9876543210

struct solve {
  solve() {}
  ll check_h(vector<plane_t> &plane) {
    ll ret = TIME_INF;
    map<int, vi> m;
    for (auto &mp : plane) {
      if (mp.d == 0)
        m[mp.y].push_back(mp.x);
    }
    for (auto &mp : plane) {
      if (mp.d != 2)
        continue;
      for (auto &mm : m[mp.y]) {
        ll t = (mp.x - mm) * 10 / 2;
        if (t > 0)
          min_u(ret, t);
      }
    }
    return ret;
  }
  ll check_rud(vector<plane_t> &plane) {
    ll ret = TIME_INF;
    map<int, vector<int>> m;
    int n = plane.size();

    for (int i = 0; i < n; i++) {
      auto &mp = plane[i];
      if (mp.d == 0)
        m[-mp.x - mp.y].push_back(i);
    }
    for (auto &mp : plane) {
      if (mp.d == 1) {
        ll c = -mp.x - mp.y;
        for (auto &pi : m[c]) {
          ll t = (mp.x - plane[pi].x) * 10;
          if (t > 0)
            min_u(ret, t);
        }
      }
    }
    return ret;
  }
  void rot(vector<plane_t> &plane) {
    for (auto &m : plane) {
      auto t = m;
      m.x = -t.y;
      m.y = t.x;
      m.d = (t.d + 1) % 4;
    }
  }
  void operator()(vector<plane_t> &plane) {
    ll ans = TIME_INF;

    for (int i = 0; i < 4; i++) {
      min_u(ans, check_h(plane));
      min_u(ans, check_rud(plane));
      rot(plane);
    }
    if (ans != TIME_INF)
      cout << ans << "\\n";
    else
      cout << "SAFE\\n";
  }
};

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  for (;;) {
    int n;
    cin >> n;
    if (cin.fail())
      break;
    vector<plane_t> plane(n);
    for (auto &m : plane) {
      int x, y, d = 0;
      string u;
      cin >> x >> y >> u;
      if (u == "R")
        d = 0;
      if (u == "U")
        d = 1;
      if (u == "L")
        d = 2;
      if (u == "D")
        d = 3;
      m = plane_t{x, y, d};
    }
    solve()(plane);
  }
  return 0;
}