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++)
using namespace std;

typedef long long ll;

int main() {
  int N, X, Y;
  cin >> N;
  // vector<ll> X(N), Y(N);
  // vector<char> U(N);
  char U;
  map<int, vector<int>> UR, RU, UL, LU, DR, RD, DL, LD, UD, DU, RL, LR;
  rep(i, N) {
    cin >> X >> Y >> U;
    if (U == 'U') {
      UR[X + Y].push_back(X);
      UL[X - Y].push_back(X);
      UD[X].push_back(Y);
    }
    if (U == 'D') {
      DR[X - Y].push_back(X);
      DL[X + Y].push_back(X);
      DU[X].push_back(Y);
    }
    if (U == 'R') {
      RU[X + Y].push_back(X);
      RD[X - Y].push_back(X);
      RL[Y].push_back(X);
    }
    if (U == 'L') {
      LU[X - Y].push_back(X);
      LD[X + Y].push_back(X);
      LR[Y].push_back(X);
    }
  }
  int ans = 1e9;
  for (auto it : UR) {
    int i = it.first;
    sort(UR[i].begin(), UR[i].end());
    if (UR[i].size() > 0) {
      if (RU.find(i) != RU.end()) {
        sort(RU[i].begin(), RU[i].end());
        rep(j, UR[i].size()) {
          int k =
              lower_bound(RU[i].begin(), RU[i].end(), UR[i][j]) - RU[i].begin();
          if (k > 0) {
            ans = min(ans, (UR[i][j] - RU[i][k - 1]) * 10);
          }
        }
      }
    }
  }
  for (auto it : LU) {
    int i = it.first;
    sort(LU[i].begin(), LU[i].end());
    if (LU[i].size() > 0) {
      if (UL.find(i) != UL.end()) {
        sort(UL[i].begin(), UL[i].end());
        rep(j, LU[i].size()) {
          int k =
              lower_bound(UL[i].begin(), UL[i].end(), LU[i][j]) - UL[i].begin();
          if (k > 0) {
            ans = min(ans, (LU[i][j] - UL[i][k - 1]) * 10);
          }
        }
      }
    }
  }
  for (auto it : DR) {
    int i = it.first;
    sort(DR[i].begin(), DR[i].end());
    if (DR[i].size() > 0) {
      if (RD.find(i) != RD.end()) {
        sort(RD[i].begin(), RD[i].end());
        rep(j, DR[i].size()) {
          int k =
              lower_bound(RD[i].begin(), RD[i].end(), DR[i][j]) - RD[i].begin();
          if (k > 0) {
            ans = min(ans, (DR[i][j] - RD[i][k - 1]) * 10);
          }
        }
      }
    }
  }
  for (auto it : LD) {
    int i = it.first;
    sort(LD[i].begin(), LD[i].end());
    if (LD[i].size() > 0) {
      if (DL.find(i) != DL.end()) {
        sort(DL[i].begin(), DL[i].end());
        rep(j, LD[i].size()) {
          int k =
              lower_bound(DL[i].begin(), DL[i].end(), LD[i][j]) - DL[i].begin();
          if (k > 0) {
            ans = min(ans, (LD[i][j] - DL[i][k - 1]) * 10);
          }
        }
      }
    }
  }
  for (auto it : DU) {
    int i = it.first;
    sort(DU[i].begin(), DU[i].end());
    if (DU[i].size() > 0) {
      if (UD.find(i) != UD.end()) {
        sort(UD[i].begin(), UD[i].end());
        rep(j, DU[i].size()) {
          int k =
              lower_bound(UD[i].begin(), UD[i].end(), DU[i][j]) - UD[i].begin();
          if (k > 0) {
            ans = min(ans, (DU[i][j] - UD[i][k - 1]) * 5);
          }
        }
      }
    }
  }
  for (auto it : LR) {
    int i = it.first;
    sort(LR[i].begin(), LR[i].end());
    if (LR[i].size() > 0) {
      if (RL.find(i) != RL.end()) {
        sort(RL[i].begin(), RL[i].end());
        rep(j, LR[i].size()) {
          int k =
              lower_bound(RL[i].begin(), RL[i].end(), LR[i][j]) - RL[i].begin();
          if (k > 0) {
            ans = min(ans, (LR[i][j] - RL[i][k - 1]) * 5);
          }
        }
      }
    }
  }
  if (ans == 1e9)
    cout << "SAFE\\n";
  else
    cout << ans << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

int main() {
  int N, X, Y;
  cin >> N;
  // vector<ll> X(N), Y(N);
  // vector<char> U(N);
  char U;
  map<int, vector<int>> UR, RU, UL, LU, DR, RD, DL, LD, UD, DU, RL, LR;
  rep(i, N) {
    cin >> X >> Y >> U;
    if (U == 'U') {
      UR[X + Y].push_back(X);
      UL[X - Y].push_back(X);
      UD[X].push_back(Y);
    }
    if (U == 'D') {
      DR[X - Y].push_back(X);
      DL[X + Y].push_back(X);
      DU[X].push_back(Y);
    }
    if (U == 'R') {
      RU[X + Y].push_back(X);
      RD[X - Y].push_back(X);
      RL[Y].push_back(X);
    }
    if (U == 'L') {
      LU[X - Y].push_back(X);
      LD[X + Y].push_back(X);
      LR[Y].push_back(X);
    }
  }
  int ans = 1e9;
  for (int i = -200000; i < 400001; i++) {
    sort(UR[i].begin(), UR[i].end());
    sort(RU[i].begin(), RU[i].end());
    sort(UL[i].begin(), UL[i].end());
    sort(LU[i].begin(), LU[i].end());
    sort(DR[i].begin(), DR[i].end());
    sort(RD[i].begin(), RD[i].end());
    sort(DL[i].begin(), DL[i].end());
    sort(LD[i].begin(), LD[i].end());
    sort(UD[i].begin(), UD[i].end());
    sort(DU[i].begin(), DU[i].end());
    sort(RL[i].begin(), RL[i].end());
    sort(LR[i].begin(), LR[i].end());
    if (UR[i].size() > 0) {
      if (RU[i].size() > 0) {
        rep(j, UR[i].size()) {
          int k =
              lower_bound(RU[i].begin(), RU[i].end(), UR[i][j]) - RU[i].begin();
          if (k > 0) {
            ans = min(ans, UR[i][j] - RU[i][k - 1]) * 10;
          }
        }
      }
    }
    if (LU[i].size() > 0) {
      if (UL[i].size() > 0) {
        rep(j, LU[i].size()) {
          int k =
              lower_bound(UL[i].begin(), UL[i].end(), LU[i][j]) - UL[i].begin();
          if (k > 0) {
            ans = min(ans, LU[i][j] - UL[i][k - 1]) * 10;
          }
        }
      }
    }
    if (DR[i].size() > 0) {
      if (RD[i].size() > 0) {
        rep(j, DR[i].size()) {
          int k =
              lower_bound(RD[i].begin(), RD[i].end(), DR[i][j]) - RD[i].begin();
          if (k > 0) {
            ans = min(ans, DR[i][j] - RD[i][k - 1]) * 10;
          }
        }
      }
    }
    if (LD[i].size() > 0) {
      if (DL[i].size() > 0) {
        rep(j, LD[i].size()) {
          int k =
              lower_bound(DL[i].begin(), DL[i].end(), LD[i][j]) - DL[i].begin();
          if (k > 0) {
            ans = min(ans, LD[i][j] - DL[i][k - 1]) * 10;
          }
        }
      }
    }
    if (DU[i].size() > 0) {
      if (UD[i].size() > 0) {
        rep(j, DU[i].size()) {
          int k =
              lower_bound(UD[i].begin(), UD[i].end(), DU[i][j]) - UD[i].begin();
          if (k > 0) {
            ans = min(ans, DU[i][j] - UD[i][k - 1]) * 5;
          }
        }
      }
    }
    if (LR[i].size() > 0) {
      if (RL[i].size() > 0) {
        rep(j, LR[i].size()) {
          int k =
              lower_bound(RL[i].begin(), RL[i].end(), LR[i][j]) - RL[i].begin();
          if (k > 0) {
            ans = min(ans, LR[i][j] - RL[i][k - 1]) * 5;
          }
        }
      }
    }
  }
  if (ans == 1e9)
    cout << "SAFE\\n";
  else
    cout << ans << "\\n";
}