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

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

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

ll nextLong() {
  ll x;
  scanf("%lld", &x);
  return x;
}

const int MAX_N = 212345;
const ll INF = 1LL << 61;

int N;
int X[MAX_N], Y[MAX_N];
char D[MAX_N];

ll solve1() {
  ll res = INF;
  map<int, set<int>> M;
  REP(i, N) if (D[i] == 'D') M[X[i]].insert(Y[i]);
  REP(i, N) if (D[i] == 'U') {
    const auto &m = M[X[i]];
    auto it = m.lower_bound(Y[i]);
    if (it != m.end()) {
      ll d = (*it) - Y[i];
      chmin(res, d * 5);
    }
  }
  return res;
}
ll solve2() {
  ll res = INF;
  map<int, set<int>> M;
  REP(i, N) if (D[i] == 'L') M[Y[i]].insert(X[i]);
  REP(i, N) if (D[i] == 'R') {
    const auto &m = M[Y[i]];
    auto it = m.lower_bound(X[i]);
    if (it != m.end()) {
      ll d = (*it) - X[i];
      chmin(res, d * 5);
    }
  }
  return res;
}

ll solve3() {
  ll res = INF;
  map<int, set<int>> M;
  REP(i, N) if (D[i] == 'D') M[X[i] - Y[i]].insert(X[i]);
  REP(i, N) if (D[i] == 'R') {
    const auto &m = M[X[i] - Y[i]];
    auto it = m.lower_bound(X[i]);
    if (it != m.end()) {
      ll d = (*it) - X[i];
      chmin(res, d * 10);
    }
  }
  return res;
}
ll solve4() {
  ll res = INF;
  map<int, set<int>> M;
  REP(i, N) if (D[i] == 'L') M[X[i] - Y[i]].insert(X[i]);
  REP(i, N) if (D[i] == 'U') {
    const auto &m = M[X[i] - Y[i]];
    auto it = m.lower_bound(X[i]);
    if (it != m.end()) {
      ll d = (*it) - X[i];
      chmin(res, d * 10);
    }
  }
  return res;
}
ll solve5() {
  ll res = INF;
  map<int, set<int>> M;
  REP(i, N) if (D[i] == 'L') M[X[i] + Y[i]].insert(X[i]);
  REP(i, N) if (D[i] == 'D') {
    const auto &m = M[X[i] + Y[i]];
    auto it = m.lower_bound(X[i]);
    if (it != m.end()) {
      ll d = (*it) - X[i];
      chmin(res, d * 10);
    }
  }
  return res;
}
ll solve6() {
  ll res = INF;
  map<int, set<int>> M;
  REP(i, N) if (D[i] == 'U') M[X[i] + Y[i]].insert(X[i]);
  REP(i, N) if (D[i] == 'R') {
    const auto &m = M[X[i] + Y[i]];
    auto it = m.lower_bound(X[i]);
    if (it != m.end()) {
      ll d = (*it) - X[i];
      chmin(res, d * 10);
    }
  }
  return res;
}

int main2() {
  N = nextLong();
  REP(i, N) { cin >> X[i] >> Y[i] >> D[i]; }

  ll ans = INF;
  chmin(ans, solve1());
  chmin(ans, solve2());
  chmin(ans, solve3());
  chmin(ans, solve4());
  chmin(ans, solve5());
  chmin(ans, solve6());

  if (ans >= INF / 2)
    cout << "SAFE" << endl;
  else
    cout << ans << endl;
  return 0;
}

int main() {

#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

ll nextLong() {
  ll x;
  scanf("%lld", &x);
  return x;
}

const int MAX_N = 212345;
const ll INF = 1LL << 61;

int N;
int X[MAX_N], Y[MAX_N];
char D[MAX_N];

ll solve1() {
  ll res = INF;
  map<int, set<int>> M;
  REP(i, N) if (D[i] == 'D') M[X[i]].insert(Y[i]);
  REP(i, N) if (D[i] == 'U') {
    auto it = M[X[i]].lower_bound(Y[i]);
    if (it != M[X[i]].end()) {
      ll d = (*it) - Y[i];
      chmin(res, d * 5);
    }
  }
  return res;
}
ll solve2() {
  ll res = INF;
  map<int, set<int>> M;
  REP(i, N) if (D[i] == 'L') M[Y[i]].insert(X[i]);
  REP(i, N) if (D[i] == 'R') {
    auto it = M[Y[i]].lower_bound(X[i]);
    if (it != M[Y[i]].end()) {
      ll d = (*it) - X[i];
      chmin(res, d * 5);
    }
  }
  return res;
}

ll solve3() {
  ll res = INF;
  map<int, set<int>> M;
  REP(i, N) if (D[i] == 'D') M[X[i] - Y[i]].insert(X[i]);
  REP(i, N) if (D[i] == 'R') {
    auto m = M[X[i] - Y[i]];
    auto it = m.lower_bound(X[i]);
    if (it != m.end()) {
      ll d = (*it) - X[i];
      chmin(res, d * 10);
    }
  }
  return res;
}
ll solve4() {
  ll res = INF;
  map<int, set<int>> M;
  REP(i, N) if (D[i] == 'L') M[X[i] - Y[i]].insert(X[i]);
  REP(i, N) if (D[i] == 'U') {
    auto m = M[X[i] - Y[i]];
    auto it = m.lower_bound(X[i]);
    if (it != m.end()) {
      ll d = (*it) - X[i];
      chmin(res, d * 10);
    }
  }
  return res;
}
ll solve5() {
  ll res = INF;
  map<int, set<int>> M;
  REP(i, N) if (D[i] == 'L') M[X[i] + Y[i]].insert(X[i]);
  REP(i, N) if (D[i] == 'D') {
    auto m = M[X[i] + Y[i]];
    auto it = m.lower_bound(X[i]);
    if (it != m.end()) {
      ll d = (*it) - X[i];
      chmin(res, d * 10);
    }
  }
  return res;
}
ll solve6() {
  ll res = INF;
  map<int, set<int>> M;
  REP(i, N) if (D[i] == 'U') M[X[i] + Y[i]].insert(X[i]);
  REP(i, N) if (D[i] == 'R') {
    auto m = M[X[i] + Y[i]];
    auto it = m.lower_bound(X[i]);
    if (it != m.end()) {
      ll d = (*it) - X[i];
      chmin(res, d * 10);
    }
  }
  return res;
}

int main2() {
  N = nextLong();
  REP(i, N) { cin >> X[i] >> Y[i] >> D[i]; }

  ll ans = INF;
  chmin(ans, solve1());
  chmin(ans, solve2());
  chmin(ans, solve3());
  chmin(ans, solve4());
  chmin(ans, solve5());
  chmin(ans, solve6());

  if (ans >= INF / 2)
    cout << "SAFE" << endl;
  else
    cout << ans << endl;
  return 0;
}

int main() {

#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}