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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

const int X_MAX = 200020;
const int S_MAX = 400040;
const int INF = INT_MAX;

set<int> LSet[X_MAX];
set<int> RSet[X_MAX];
set<int> USet[X_MAX];
set<int> DSet[X_MAX];

set<int> LSum[S_MAX], LDiff[S_MAX];
set<int> RSum[S_MAX], RDiff[S_MAX];
set<int> USum[S_MAX], UDiff[S_MAX];
set<int> DSum[S_MAX], DDiff[S_MAX];

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  rep(i, N) {
    int x, y;
    cin >> x >> y;
    char c;
    cin >> c;
    if (c == 'L')
      LSet[y].ins(x), LSum[x + y].ins(x), LDiff[x - y + X_MAX].ins(x);
    if (c == 'R')
      RSet[y].ins(x), RSum[x + y].ins(x), RDiff[x - y + X_MAX].ins(x);
    if (c == 'U')
      USet[x].ins(y), USum[x + y].ins(x), UDiff[x - y + X_MAX].ins(x);
    if (c == 'D')
      DSet[x].ins(y), DSum[x + y].ins(x), DDiff[x - y + X_MAX].ins(x);
  }
  int ans = INF;
  // L-R
  rep(y, X_MAX) {
    for (auto r : RSet[y]) {
      auto itr = LSet[y].upper_bound(r);
      if (itr == LSet[y].end())
        continue;
      int l = *itr;
      chmin(ans, (l - r) * 5);
    }
  }
  // U-D
  rep(x, X_MAX) {
    for (auto u : USet[x]) {
      auto itr = DSet[x].upper_bound(u);
      if (itr == DSet[x].end())
        continue;
      int d = *itr;
      chmin(ans, (d - u) * 5);
    }
  }
  // D-L
  rep(s, S_MAX) {
    for (auto d : DSum[s]) {
      auto itr = LSum[s].upper_bound(d);
      if (itr == LSum[s].end())
        continue;
      int l = *itr;
      chmin(ans, (l - d) * 10);
    }
  }
  // U-L
  rep(s, S_MAX) {
    for (auto u : UDiff[s]) {
      auto itr = LDiff[s].upper_bound(u);
      if (itr == LDiff[s].end())
        continue;
      int l = *itr;
      chmin(ans, (l - u) * 10);
    }
  }
  // R-U
  rep(s, S_MAX) {
    for (auto r : RSum[s]) {
      auto itr = USum[s].upper_bound(r);
      if (itr == USum[s].end())
        continue;
      int u = *itr;
      chmin(ans, (u - r) * 10);
    }
  }
  // R-D
  rep(s, S_MAX) {
    for (auto r : RDiff[s]) {
      auto itr = DDiff[s].upper_bound(r);
      if (itr == DDiff[s].end())
        continue;
      int d = *itr;
      chmin(ans, (d - r) * 10);
    }
  }
  if (ans == INF) {
    cout << "SAFE\\n";
  } else {
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

const int X_MAX = 200020;
const int INF = INT_MAX;

set<int> LSet[X_MAX];
set<int> RSet[X_MAX];
set<int> USet[X_MAX];
set<int> DSet[X_MAX];

set<pi> LSum, LDiff;
set<pi> RSum, RDiff;
set<pi> USum, UDiff;
set<pi> DSum, DDiff;

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  rep(i, N) {
    int x, y;
    cin >> x >> y;
    char c;
    cin >> c;
    if (c == 'L')
      LSet[y].ins(x), LSum.ins({x + y, x}), LDiff.ins({x - y, x});
    if (c == 'R')
      RSet[y].ins(x), RSum.ins({x + y, x}), RDiff.ins({x - y, x});
    if (c == 'U')
      USet[x].ins(y), USum.ins({x + y, x}), UDiff.ins({x - y, x});
    if (c == 'D')
      DSet[x].ins(y), DSum.ins({x + y, x}), DDiff.ins({x - y, x});
  }
  int ans = INF;
  // L-R
  rep(y, X_MAX) {
    for (auto r : RSet[y]) {
      auto itr = LSet[y].upper_bound(r);
      if (itr == LSet[y].end())
        continue;
      int l = *itr;
      chmin(ans, (l - r) * 5);
    }
  }
  // U-D
  rep(x, X_MAX) {
    for (auto u : USet[x]) {
      auto itr = DSet[x].upper_bound(u);
      if (itr == DSet[x].end())
        continue;
      int d = *itr;
      chmin(ans, (d - u) * 5);
    }
  }
  // Cross
  for (auto p : LSum) {
    auto itr = DSum.lower_bound(pi(p.fi, 0));
    while (itr != DSum.end() && itr->fi == p.fi) {
      pi q = *itr;
      if (p.se > q.se)
        chmin(ans, (p.se - q.se) * 10);
      itr++;
    }
  }
  for (auto p : LDiff) {
    auto itr = UDiff.lower_bound(pi(p.fi, 0));
    while (itr != UDiff.end() && itr->fi == p.fi) {
      pi q = *itr;
      if (p.se > q.se)
        chmin(ans, (p.se - q.se) * 10);
      itr++;
    }
  }
  for (auto p : RSum) {
    auto itr = USum.lower_bound(pi(p.fi, 0));
    while (itr != USum.end() && itr->fi == p.fi) {
      pi q = *itr;
      if (p.se < q.se)
        chmin(ans, (q.se - p.se) * 10);
      itr++;
    }
  }
  for (auto p : RDiff) {
    auto itr = DDiff.lower_bound(pi(p.fi, 0));
    while (itr != DDiff.end() && itr->fi == p.fi) {
      pi q = *itr;
      if (p.se < q.se)
        chmin(ans, (q.se - p.se) * 10);
      itr++;
    }
  }
  if (ans == INF) {
    cout << "SAFE\\n";
  } else {
    cout << ans << endl;
  }
  return 0;
}