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

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
const ll MOD = 1000000007;
const ll INF = 10000000000000;
#define all(v) v.begin(), v.end()

int main() {
  ll N, m = INF;
  cin >> N;
  vector<ll> X(N), Y(N);
  vector<char> U(N);
  vector<vector<vector<ll>>> A(200001, vector<vector<ll>>(2)),
      B(200001, vector<vector<ll>>(2)), C(400001, vector<vector<ll>>(4)),
      D(400001, vector<vector<ll>>(4));
  rep(i, N) {
    cin >> X.at(i) >> Y.at(i) >> U.at(i);
    if (U.at(i) == 'U') {
      A.at(X.at(i)).at(0).push_back(Y.at(i));
      C.at(X.at(i) + Y.at(i)).at(0).push_back(Y.at(i));
      D.at(X.at(i) - Y.at(i) + 200000).at(0).push_back(Y.at(i));
    }
    if (U.at(i) == 'R') {
      B.at(Y.at(i)).at(0).push_back(X.at(i));
      C.at(X.at(i) + Y.at(i)).at(1).push_back(Y.at(i));
      D.at(X.at(i) - Y.at(i) + 200000).at(1).push_back(Y.at(i));
    }
    if (U.at(i) == 'D') {
      A.at(X.at(i)).at(1).push_back(Y.at(i));
      C.at(X.at(i) + Y.at(i)).at(2).push_back(Y.at(i));
      D.at(X.at(i) - Y.at(i) + 200000).at(2).push_back(Y.at(i));
    }
    if (U.at(i) == 'L') {
      B.at(Y.at(i)).at(1).push_back(X.at(i));
      C.at(X.at(i) + Y.at(i)).at(3).push_back(Y.at(i));
      D.at(X.at(i) - Y.at(i) + 200000).at(3).push_back(Y.at(i));
    }
  }
  rep(i, 200001) {
    sort(all(A.at(i).at(0)));
    sort(all(A.at(i).at(1)));
    if (A.at(i).at(0).size() > 0 && A.at(i).at(1).size() > 0) {
      ll t = 0;
      rep(j, A.at(i).at(0).size()) {
        while (t < A.at(i).at(1).size() &&
               A.at(i).at(0).at(j) > A.at(i).at(1).at(t)) {
          t++;
        }
        if (t == A.at(i).at(1).size()) {
          break;
        }
        m = min(m, (A.at(i).at(1).at(t) - A.at(i).at(0).at(j)) * 5);
      }
    }
    sort(all(B.at(i).at(0)));
    sort(all(B.at(i).at(1)));
    if (B.at(i).at(0).size() > 0 && B.at(i).at(1).size() > 0) {
      ll t = 0;
      rep(j, B.at(i).at(0).size()) {
        while (t < B.at(i).at(1).size() &&
               B.at(i).at(0).at(j) > B.at(i).at(1).at(t)) {
          t++;
        }
        if (t == B.at(i).at(1).size()) {
          break;
        }
        m = min(m, (B.at(i).at(1).at(t) - B.at(i).at(0).at(j)) * 5);
      }
    }
  }
  rep(i, 400001) {
    sort(all(C.at(i).at(0)));
    sort(all(C.at(i).at(1)));
    sort(all(C.at(i).at(2)));
    sort(all(C.at(i).at(3)));
    if (C.at(i).at(0).size() > 0 && C.at(i).at(1).size() > 0) {
      ll t = 0;
      rep(j, C.at(i).at(0).size()) {
        while (t < C.at(i).at(1).size() &&
               C.at(i).at(0).at(j) > C.at(i).at(1).at(t)) {
          t++;
        }
        if (t == C.at(i).at(1).size()) {
          break;
        }
        m = min(m, (C.at(i).at(1).at(t) - C.at(i).at(0).at(j)) * 10);
      }
    }
    if (C.at(i).at(3).size() > 0 && C.at(i).at(2).size() > 0) {
      ll t = 0;
      rep(j, C.at(i).at(3).size()) {
        while (t < C.at(i).at(2).size() &&
               C.at(i).at(3).at(j) > C.at(i).at(2).at(t)) {
          t++;
        }
        if (t == C.at(i).at(2).size()) {
          break;
        }
        m = min(m, (C.at(i).at(2).at(t) - C.at(i).at(3).at(j)) * 10);
      }
    }
    sort(all(D.at(i).at(0)));
    sort(all(D.at(i).at(1)));
    sort(all(D.at(i).at(2)));
    sort(all(D.at(i).at(3)));
    if (D.at(i).at(0).size() > 0 && D.at(i).at(3).size() > 0) {
      ll t = 0;
      rep(j, D.at(i).at(0).size()) {
        while (t < D.at(i).at(3).size() &&
               D.at(i).at(0).at(j) > D.at(i).at(3).at(t)) {
          t++;
        }
        if (t == D.at(i).at(3).size()) {
          break;
        }
        m = min(m, (D.at(i).at(3).at(t) - D.at(i).at(0).at(j)) * 10);
      }
    }
    if (D.at(i).at(1).size() > 0 && D.at(i).at(2).size() > 0) {
      ll t = 0;
      rep(j, D.at(i).at(1).size()) {
        while (t < D.at(i).at(2).size() &&
               D.at(i).at(1).at(j) > D.at(i).at(2).at(t)) {
          t++;
        }
        if (t == D.at(i).at(2).size()) {
          break;
        }
        m = min(m, (D.at(i).at(2).at(t) - D.at(i).at(1).at(j)) * 10);
      }
    }
  }
  if (m == INF) {
    cout << "SAFE" << endl;
  } else {
    cout << m << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
const ll MOD = 1000000007;
const ll INF = 10000000000000;
#define all(v) v.begin(), v.end()

int main() {
  ll N, m = INF;
  cin >> N;
  vector<ll> X(N), Y(N);
  vector<char> U(N);
  vector<vector<ll>> A(200001), B(200001), C(400001), D(400001);
  rep(i, N) {
    cin >> X.at(i) >> Y.at(i) >> U.at(i);
    A.at(X.at(i)).push_back(i);
    B.at(Y.at(i)).push_back(i);
    C.at(X.at(i) + Y.at(i)).push_back(i);
    D.at(X.at(i) - Y.at(i) + 200000).push_back(i);
  }
  rep(i, 200001) {
    if (A.at(i).size() > 1) {
      rep(j, A.at(i).size() - 1) {
        for (ll k = j + 1; k < A.at(i).size(); k++) {
          if ((Y.at(A.at(i).at(j)) > Y.at(A.at(i).at(k)) &&
               U.at(A.at(i).at(j)) == 'D' && U.at(A.at(i).at(k)) == 'U') ||
              (Y.at(A.at(i).at(j)) < Y.at(A.at(i).at(k)) &&
               U.at(A.at(i).at(k)) == 'D' && U.at(A.at(i).at(j)) == 'U')) {
            m = min(m, abs(Y.at(A.at(i).at(j)) - Y.at(A.at(i).at(k))) * 5);
          }
        }
      }
    }
    if (B.at(i).size() > 1) {
      rep(j, B.at(i).size() - 1) {
        for (ll k = j + 1; k < B.at(i).size(); k++) {
          if ((X.at(B.at(i).at(j)) > X.at(B.at(i).at(k)) &&
               U.at(B.at(i).at(j)) == 'L' && U.at(B.at(i).at(k)) == 'R') ||
              (X.at(B.at(i).at(j)) < X.at(B.at(i).at(k)) &&
               U.at(B.at(i).at(k)) == 'L' && U.at(B.at(i).at(j)) == 'R')) {
            m = min(m, abs(X.at(B.at(i).at(j)) - X.at(B.at(i).at(k))) * 5);
          }
        }
      }
    }
  }
  rep(i, 400001) {
    if (C.at(i).size() > 1) {
      rep(j, C.at(i).size() - 1) {
        for (ll k = j + 1; k < C.at(i).size(); k++) {
          if ((Y.at(C.at(i).at(j)) > Y.at(C.at(i).at(k)) &&
               ((U.at(C.at(i).at(j)) == 'D' && U.at(C.at(i).at(k)) == 'L') ||
                (U.at(C.at(i).at(j)) == 'R' && U.at(C.at(i).at(k)) == 'U'))) ||
              (Y.at(C.at(i).at(j)) < Y.at(C.at(i).at(k)) &&
               ((U.at(C.at(i).at(k)) == 'D' && U.at(C.at(i).at(j)) == 'L') ||
                (U.at(C.at(i).at(k)) == 'R' && U.at(C.at(i).at(j)) == 'U')))) {
            m = min(m, abs(Y.at(C.at(i).at(j)) - Y.at(C.at(i).at(k))) * 10);
          }
        }
      }
    }
    if (D.at(i).size() > 1) {
      rep(j, D.at(i).size() - 1) {
        for (ll k = j + 1; k < D.at(i).size(); k++) {
          if ((Y.at(D.at(i).at(j)) > Y.at(D.at(i).at(k)) &&
               ((U.at(D.at(i).at(j)) == 'D' && U.at(D.at(i).at(k)) == 'R') ||
                (U.at(D.at(i).at(j)) == 'L' && U.at(D.at(i).at(k)) == 'U'))) ||
              (Y.at(D.at(i).at(j)) < Y.at(D.at(i).at(k)) &&
               ((U.at(D.at(i).at(k)) == 'D' && U.at(D.at(i).at(j)) == 'R') ||
                (U.at(D.at(i).at(k)) == 'L' && U.at(D.at(i).at(j)) == 'U')))) {
            m = min(m, abs(Y.at(D.at(i).at(j)) - Y.at(D.at(i).at(k))) * 10);
          }
        }
      }
    }
  }
  if (m == INF) {
    cout << "SAFE" << endl;
  } else {
    cout << m << endl;
  }
}