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

#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int N;
  cin >> N;
  static int X[200200];
  static int Y[200200];
  static string Type[200200];
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> Type[i];
  }

  static vector<int> U[200200];
  static vector<int> R[200200];
  static vector<int> D[200200];
  static vector<int> L[200200];

  static vector<int> U_[400400];
  static vector<int> R_[400400];
  static vector<int> D_[400400];
  static vector<int> L_[400400];

  static vector<int> U__[400400];
  static vector<int> R__[400400];
  static vector<int> D__[400400];
  static vector<int> L__[400400];

  for (int i = 0; i < N; i++) {
    if (Type[i] == "U") {
      U[X[i]].push_back(Y[i]);
      U_[X[i] + Y[i]].push_back(X[i]);
      U__[X[i] - Y[i] + 200000].push_back(X[i]);
    } else if (Type[i] == "R") {
      R[Y[i]].push_back(X[i]);
      R_[X[i] + Y[i]].push_back(X[i]);
      R__[X[i] - Y[i] + 200000].push_back(X[i]);
    } else if (Type[i] == "D") {
      D[X[i]].push_back(Y[i]);
      D_[X[i] + Y[i]].push_back(X[i]);
      D__[X[i] - Y[i] + 200000].push_back(X[i]);
    } else {
      L[Y[i]].push_back(X[i]);
      L_[X[i] + Y[i]].push_back(X[i]);
      L__[X[i] - Y[i] + 200000].push_back(X[i]);
    }
  }

  int SAFE = 10000000;
  int ans = SAFE;
  for (int i = 0; i <= 200000; i++) {
    if (U[i].size() > 0 && D[i].size() > 0) {
      sort(D[i].begin(), D[i].end());
      for (int j = 0; j < U[i].size(); j++) {
        auto near = lower_bound(D[i].begin(), D[i].end(), U[i][j]);
        if (near != D[i].end()) {
          ans = min(ans, 5 * (*near - U[i][j]));
        }
      }
    }

    if (R[i].size() > 0 && L[i].size() > 0) {
      sort(L[i].begin(), L[i].end());
      for (int j = 0; j < R[i].size(); j++) {
        auto near = lower_bound(L[i].begin(), L[i].end(), R[i][j]);
        if (near != L[i].end()) {
          ans = min(ans, 5 * (*near - R[i][j]));
        }
      }
    }
  }

  // 斜め衝突
  for (int i = 0; i <= 400000; i++) {
    if (R_[i].size() > 0 && U_[i].size() > 0) {
      sort(U_[i].begin(), U_[i].end());
      for (int j = 0; j < R_[i].size(); j++) {
        auto near = lower_bound(U_[i].begin(), U_[i].end(), R_[i][j]);
        if (near != U_[i].end()) {
          ans = min(ans, 10 * (*near - R_[i][j]));
        }
      }
    }
    if (D_[i].size() > 0 && L_[i].size() > 0) {
      sort(L_[i].begin(), L_[i].end());
      for (int j = 0; j < D_[i].size(); j++) {
        auto near = lower_bound(L_[i].begin(), L_[i].end(), D_[i][j]);
        if (near != L_[i].end()) {
          ans = min(ans, 10 * (*near - D_[i][j]));
        }
      }
    }
  }

  // 斜め衝突２
  for (int i = 0; i <= 400000; i++) {
    if (U__[i].size() > 0 && L__[i].size() > 0) {
      sort(L__[i].begin(), L__[i].end());
      for (int j = 0; j < U__[i].size(); j++) {
        auto near = lower_bound(L__[i].begin(), L__[i].end(), U__[i][j]);
        if (near != L__[i].end()) {
          ans = min(ans, 10 * (*near - U__[i][j]));
        }
      }
    }
    if (R__[i].size() > 0 && D__[i].size() > 0) {
      sort(D__[i].begin(), D__[i].end());
      for (int j = 0; j < R__[i].size(); j++) {
        auto near = lower_bound(D__[i].begin(), D__[i].end(), R__[i][j]);
        if (near != D__[i].end()) {
          ans = min(ans, 10 * (*near - R__[i][j]));
        }
      }
    }
  }

  if (ans == SAFE) {
    cout << "SAFE" << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int N;
  cin >> N;
  static int X[200200];
  static int Y[200200];
  static string Type[200200];
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> Type[i];
  }

  static vector<int> U[200200];
  static vector<int> R[200200];
  static set<int> D[200200];
  static set<int> L[200200];

  static set<int> U_[400400];
  static vector<int> R_[400400];
  static vector<int> D_[400400];
  static set<int> L_[400400];

  static vector<int> U__[400400];
  static vector<int> R__[400400];
  static set<int> D__[400400];
  static set<int> L__[400400];

  for (int i = 0; i < N; i++) {
    if (Type[i] == "U") {
      U[X[i]].emplace_back(Y[i]);
      U_[X[i] + Y[i]].insert(X[i]);
      U__[X[i] - Y[i] + 200000].emplace_back(X[i]);
    } else if (Type[i] == "R") {
      R[Y[i]].emplace_back(X[i]);
      R_[X[i] + Y[i]].emplace_back(X[i]);
      R__[X[i] - Y[i] + 200000].emplace_back(X[i]);
    } else if (Type[i] == "D") {
      D[X[i]].insert(Y[i]);
      D_[X[i] + Y[i]].emplace_back(X[i]);
      D__[X[i] - Y[i] + 200000].insert(X[i]);
    } else {
      L[Y[i]].insert(X[i]);
      L_[X[i] + Y[i]].insert(X[i]);
      L__[X[i] - Y[i] + 200000].insert(X[i]);
    }
  }

  int SAFE = 10000000;
  int ans = SAFE;
  for (int i = 0; i <= 200000; i++) {
    if (U[i].size() > 0 && D[i].size() > 0) {
      for (int j = 0; j < U[i].size(); j++) {
        auto near = lower_bound(D[i].begin(), D[i].end(), U[i][j]);
        if (near != D[i].end()) {
          ans = min(ans, 5 * (*near - U[i][j]));
        }
      }
    }

    if (R[i].size() > 0 && L[i].size() > 0) {
      for (int j = 0; j < R[i].size(); j++) {
        auto near = lower_bound(L[i].begin(), L[i].end(), R[i][j]);
        if (near != L[i].end()) {
          ans = min(ans, 5 * (*near - R[i][j]));
        }
      }
    }
  }

  // 斜め衝突
  for (int i = 0; i <= 400000; i++) {
    if (R_[i].size() > 0 && U_[i].size() > 0) {
      for (int j = 0; j < R_[i].size(); j++) {
        auto near = lower_bound(U_[i].begin(), U_[i].end(), R_[i][j]);
        if (near != U_[i].end()) {
          ans = min(ans, 10 * (*near - R_[i][j]));
        }
      }
    }
    if (D_[i].size() > 0 && L_[i].size() > 0) {
      for (int j = 0; j < D_[i].size(); j++) {
        auto near = lower_bound(L_[i].begin(), L_[i].end(), D_[i][j]);
        if (near != L_[i].end()) {
          ans = min(ans, 10 * (*near - D_[i][j]));
        }
      }
    }
  }

  // 斜め衝突２
  for (int i = 0; i <= 400000; i++) {
    if (U__[i].size() > 0 && L__[i].size() > 0) {
      for (int j = 0; j < U__[i].size(); j++) {
        auto near = lower_bound(L__[i].begin(), L__[i].end(), U__[i][j]);
        if (near != L__[i].end()) {
          ans = min(ans, 10 * (*near - U__[i][j]));
        }
      }
    }
    if (R__[i].size() > 0 && D__[i].size() > 0) {
      for (int j = 0; j < R__[i].size(); j++) {
        auto near = lower_bound(D__[i].begin(), D__[i].end(), R__[i][j]);
        if (near != D__[i].end()) {
          ans = min(ans, 10 * (*near - R__[i][j]));
        }
      }
    }
  }

  if (ans == SAFE) {
    cout << "SAFE" << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}