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

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;
int INF = 9999999;
int N;
int X[200006], Y[200006];
string U[200006];
int ans = INF;

set<pair<int, int>> S;
set<pair<int, int>> T;

vector<int> Vp[400006], Vm[400006], V[200006];

int geta = 200000;

int main(void) {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> X[i] >> Y[i] >> U[i];
  }

  // -- U R--//
  for (int i = 1; i <= N; i++) {
    if (U[i] == "R") {
      Vp[X[i] + Y[i]].push_back(Y[i]);
    }
  }

  for (int i = 0; i <= 400000; i++) {
    sort(Vp[i].begin(), Vp[i].end());
  }

  for (int i = 1; i <= N; i++) {
    if (U[i] != "U") {
      continue;
    }
    auto it = lower_bound(Vp[X[i] + Y[i]].begin(), Vp[X[i] + Y[i]].end(), Y[i]);
    if (it == Vp[X[i] + Y[i]].end()) {
      continue;
    }
    ans = min(ans, ((*it) - Y[i]) * 10);
  }

  for (int i = 0; i <= 400000; i++) {
    Vp[i].clear();
  }
  /*
  S.clear(); T.clear();
  for (int i = 1; i <= N; i++) {
                  if (U[i] == "R") { T.insert(make_pair(X[i] + Y[i], Y[i])); }
  }

          for (int i = 1; i <= N; i++) {
                  if (U[i] != "U") { continue; }

          auto it = upper_bound(T.begin(), T.end(), make_pair( X[i]+Y[i], INF));
                  auto it2=lower_bound(T.begin(), T.end(), make_pair( X[i]+Y[i],
  Y[i]));
                  ;
                  if (it != it2) {
                          auto itr = lower_bound(T.begin(), T.end(), make_pair(
  X[i]+Y[i],  Y[i])); if (itr == T.end()) { continue; } ans = min(ans,
  ((*itr).second -Y[i])*10);
                  }
  }
  */
  //===========//

  //==== U L ==//
  for (int i = 1; i <= N; i++) {
    if (U[i] != "L") {
      continue;
    }
    Vm[X[i] - Y[i] + geta].push_back(Y[i]);
  }

  for (int i = 0; i <= 400000; i++) {
    sort(Vm[i].begin(), Vm[i].end());
  }

  for (int i = 1; i <= N; i++) {
    if (U[i] != "U") {
      continue;
    }
    auto it = lower_bound(Vm[X[i] - Y[i] + geta].begin(),
                          Vm[X[i] - Y[i] + geta].end(), Y[i]);
    if (it == Vm[X[i] - Y[i] + geta].end()) {
      continue;
    }
    ans = min(ans, ((*it) - Y[i]) * 10);
  }

  for (int i = 0; i <= 400000; i++) {
    Vm[i].clear();
  }

  /*	S.clear(); T.clear();
          for (int i = 1; i <= N; i++) {
                  if (U[i] == "L") { T.insert(make_pair(X[i] - Y[i], Y[i])); }
          }

                  for(int i=1;i<=N;i++){
                          if (U[i]!="U"){continue;}
                  auto it = upper_bound(T.begin(), T.end(), make_pair(X[i]-Y[i],
     INF)); auto it2 = lower_bound(T.begin(), T.end(), make_pair( X[i]-Y[i],
     Y[i]));
                  ;
                  if (it != it2) {
                          auto itr = lower_bound(T.begin(), T.end(),
     make_pair(X[i]-Y[i],  Y[i])); if (itr == T.end()) { continue; } ans =
     min(ans, ((*itr).second -  Y[i])*10);
                  }
          }

          */
  ///=== D L ===//

  for (int i = 1; i <= N; i++) {
    if (U[i] != "L") {
      continue;
    }
    Vp[X[i] + Y[i]].push_back(X[i]);
  }

  for (int i = 0; i <= 400000; i++) {
    sort(Vp[i].begin(), Vp[i].end());
  }

  for (int i = 1; i <= N; i++) {
    if (U[i] != "D") {
      continue;
    }
    auto it = lower_bound(Vp[X[i] + Y[i]].begin(), Vp[X[i] + Y[i]].end(), X[i]);
    if (it == Vp[X[i] + Y[i]].end()) {
      continue;
    }
    ans = min(ans, ((*it) - X[i]) * 10);
  }

  for (int i = 0; i <= 400000; i++) {
    Vp[i].clear();
  }

  /*	S.clear(); T.clear();
          for (int i = 1; i <= N; i++) {
                  if (U[i] == "L") { T.insert(make_pair(X[i] + Y[i], X[i])); }
          }

                  for (int i = 1; i <= N; i++) {
                          if (U[i] != "D") { continue; }

                  auto it = upper_bound(T.begin(), T.end(), make_pair(
     X[i]+Y[i], INF)); auto it2 = lower_bound(T.begin(), T.end(), make_pair(
     X[i]+Y[i],  X[i]));
                  ;
                  if (it != it2) {
                          auto itr = lower_bound(T.begin(), T.end(), make_pair(
     X[i]+Y[i], X[i])); if (itr == T.end()) { continue; } ans = min(ans,
     ((*itr).second -  X[i])*10);
                  }
          }
                  */
  //===== R D ===//

  for (int i = 1; i <= N; i++) {
    if (U[i] != "D") {
      continue;
    }
    Vm[X[i] - Y[i] + geta].push_back(Y[i]);
  }

  for (int i = 0; i <= 400000; i++) {
    sort(Vm[i].begin(), Vm[i].end());
  }

  for (int i = 1; i <= N; i++) {
    if (U[i] != "R") {
      continue;
    }
    auto it = lower_bound(Vm[X[i] - Y[i] + geta].begin(),
                          Vm[X[i] - Y[i] + geta].end(), Y[i]);
    if (it == Vm[X[i] - Y[i] + geta].end()) {
      continue;
    }
    ans = min(ans, ((*it) - Y[i]) * 10);
  }

  /*	S.clear(); T.clear();
          for (int i = 1; i <= N; i++) {
                  if (U[i] == "R") { S.insert(make_pair(X[i] - Y[i], Y[i])); }
                  else if (U[i] == "D") { T.insert(make_pair(X[i] - Y[i],
     Y[i])); }
          }

          for (auto v : S) {

                  auto it = upper_bound(T.begin(), T.end(), make_pair(v.first,
     INF)); auto it2 = lower_bound(T.begin(), T.end(), make_pair(v.first,
     v.second));
                  ;
                  if (it != it2) {
                          auto itr = lower_bound(T.begin(), T.end(),
     make_pair(v.first, v.second)); if (itr == T.end()) { continue; } ans =
     min(ans, ((*itr).second - v.second)*10);
                  }
          }
  */
  //======= U D ===//

  for (int i = 1; i <= N; i++) {
    if (U[i] == "D") {
      V[X[i]].push_back(Y[i]);
    }
  }

  for (int i = 0; i <= 200000; i++) {
    sort(V[i].begin(), V[i].end());
  }

  for (int i = 1; i <= N; i++) {
    if (U[i] != "U") {
      continue;
    }
    auto it = lower_bound(V[X[i]].begin(), V[X[i]].end(), Y[i]);
    if (it == V[X[i]].end()) {
      continue;
    }
    ans = min(ans, ((*it) - Y[i]) * 5);
  }

  for (int i = 0; i <= 200000; i++) {
    V[i].clear();
  }

  /*	S.clear(); T.clear();
          for (int i = 1; i <= N; i++) {
                  if (U[i] == "U") { S.insert(make_pair(X[i], Y[i])); }
                  else if (U[i] == "D") { T.insert(make_pair(X[i], Y[i])); }
          }

          for (auto v : S) {

                  auto it = upper_bound(T.begin(), T.end(), make_pair(v.first,
     INF)); auto it2 = lower_bound(T.begin(), T.end(), make_pair(v.first,
     v.second));
                  ;
                  if (it != it2) {
                          auto itr = lower_bound(T.begin(), T.end(),
     make_pair(v.first, v.second)); if (itr == T.end()) { continue; } ans =
     min(ans, ((*itr).second - v.second)*5);
                  }
          }
          */
  //======= L R

  for (int i = 1; i <= N; i++) {
    if (U[i] == "L") {
      V[Y[i]].push_back(X[i]);
    }
  }

  for (int i = 0; i <= 200000; i++) {
    sort(V[i].begin(), V[i].end());
  }

  for (int i = 1; i <= N; i++) {
    if (U[i] != "R") {
      continue;
    }
    auto it = lower_bound(V[Y[i]].begin(), V[Y[i]].end(), X[i]);
    if (it == V[Y[i]].end()) {
      continue;
    }
    ans = min(ans, ((*it) - X[i]) * 5);
  }

  /*	S.clear(); T.clear();
          for (int i = 1; i <= N; i++) {
                  if (U[i] == "R") { S.insert(make_pair(Y[i], X[i])); }
                  else if (U[i] == "L") { T.insert(make_pair(Y[i], X[i])); }
          }

          for (auto v : S) {

                  auto it = upper_bound(T.begin(), T.end(), make_pair(v.first,
     INF)); auto it2 = lower_bound(T.begin(), T.end(), make_pair(v.first,
     v.second));
                  ;
                  if (it != it2) {
                          auto itr = lower_bound(T.begin(), T.end(),
     make_pair(v.first, v.second)); if (itr == T.end()) { continue; } ans =
     min(ans, ((*itr).second - v.second) * 5);
                  }
          }
          */

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

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;
int INF = 9999999;
int N;
int X[200006], Y[200006];
string U[200006];
int ans = INF;

set<pair<int, int>> S;
set<pair<int, int>> T;

vector<int> Vp[400006], Vm[400006], V[200006];

int geta = 200000;

int main(void) {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> X[i] >> Y[i] >> U[i];
  }

  // -- U R--//
  for (int i = 1; i <= N; i++) {
    if (U[i] == "R") {
      Vp[X[i] + Y[i]].push_back(Y[i]);
    }
  }

  for (int i = 0; i <= 400000; i++) {
    sort(Vp[i].begin(), Vp[i].end());
  }

  for (int i = 1; i <= N; i++) {
    if (U[i] != "U") {
      continue;
    }
    auto it = lower_bound(Vp[X[i] + Y[i]].begin(), Vp[X[i] + Y[i]].end(), Y[i]);
    if (it == Vp[X[i] + Y[i]].end()) {
      continue;
    }
    ans = min(ans, ((*it) - Y[i]) * 10);
  }

  for (int i = 0; i <= 40000; i++) {
    Vp[i].clear();
  }
  /*
  S.clear(); T.clear();
  for (int i = 1; i <= N; i++) {
                  if (U[i] == "R") { T.insert(make_pair(X[i] + Y[i], Y[i])); }
  }

          for (int i = 1; i <= N; i++) {
                  if (U[i] != "U") { continue; }

          auto it = upper_bound(T.begin(), T.end(), make_pair( X[i]+Y[i], INF));
                  auto it2=lower_bound(T.begin(), T.end(), make_pair( X[i]+Y[i],
  Y[i]));
                  ;
                  if (it != it2) {
                          auto itr = lower_bound(T.begin(), T.end(), make_pair(
  X[i]+Y[i],  Y[i])); if (itr == T.end()) { continue; } ans = min(ans,
  ((*itr).second -Y[i])*10);
                  }
  }
  */
  //===========//

  //==== U L ==//
  for (int i = 1; i <= N; i++) {
    if (U[i] != "L") {
      continue;
    }
    Vm[X[i] - Y[i] + geta].push_back(Y[i]);
  }

  for (int i = 0; i <= 400000; i++) {
    sort(Vm[i].begin(), Vm[i].end());
  }

  for (int i = 1; i <= N; i++) {
    if (U[i] != "U") {
      continue;
    }
    auto it = lower_bound(Vm[X[i] - Y[i] + geta].begin(),
                          Vm[X[i] - Y[i] + geta].end(), Y[i]);
    if (it == Vm[X[i] - Y[i] + geta].end()) {
      continue;
    }
    ans = min(ans, ((*it) - Y[i]) * 10);
  }

  for (int i = 0; i <= 40000; i++) {
    Vm[i].clear();
  }

  /*	S.clear(); T.clear();
          for (int i = 1; i <= N; i++) {
                  if (U[i] == "L") { T.insert(make_pair(X[i] - Y[i], Y[i])); }
          }

                  for(int i=1;i<=N;i++){
                          if (U[i]!="U"){continue;}
                  auto it = upper_bound(T.begin(), T.end(), make_pair(X[i]-Y[i],
     INF)); auto it2 = lower_bound(T.begin(), T.end(), make_pair( X[i]-Y[i],
     Y[i]));
                  ;
                  if (it != it2) {
                          auto itr = lower_bound(T.begin(), T.end(),
     make_pair(X[i]-Y[i],  Y[i])); if (itr == T.end()) { continue; } ans =
     min(ans, ((*itr).second -  Y[i])*10);
                  }
          }

          */
  ///=== D L ===//

  for (int i = 1; i <= N; i++) {
    if (U[i] != "L") {
      continue;
    }
    Vp[X[i] + Y[i]].push_back(X[i]);
  }

  for (int i = 0; i <= 400000; i++) {
    sort(Vp[i].begin(), Vp[i].end());
  }

  for (int i = 1; i <= N; i++) {
    if (U[i] != "D") {
      continue;
    }
    auto it = lower_bound(Vp[X[i] + Y[i]].begin(), Vp[X[i] + Y[i]].end(), X[i]);
    if (it == Vp[X[i] + Y[i]].end()) {
      continue;
    }
    ans = min(ans, ((*it) - X[i]) * 10);
  }

  for (int i = 0; i <= 400000; i++) {
    Vp[i].clear();
  }

  /*	S.clear(); T.clear();
          for (int i = 1; i <= N; i++) {
                  if (U[i] == "L") { T.insert(make_pair(X[i] + Y[i], X[i])); }
          }

                  for (int i = 1; i <= N; i++) {
                          if (U[i] != "D") { continue; }

                  auto it = upper_bound(T.begin(), T.end(), make_pair(
     X[i]+Y[i], INF)); auto it2 = lower_bound(T.begin(), T.end(), make_pair(
     X[i]+Y[i],  X[i]));
                  ;
                  if (it != it2) {
                          auto itr = lower_bound(T.begin(), T.end(), make_pair(
     X[i]+Y[i], X[i])); if (itr == T.end()) { continue; } ans = min(ans,
     ((*itr).second -  X[i])*10);
                  }
          }
                  */
  //===== R D ===//

  for (int i = 1; i <= N; i++) {
    if (U[i] != "D") {
      continue;
    }
    Vm[X[i] - Y[i] + geta].push_back(Y[i]);
  }

  for (int i = 0; i <= 40000; i++) {
    sort(Vm[i].begin(), Vm[i].end());
  }

  for (int i = 1; i <= N; i++) {
    if (U[i] != "R") {
      continue;
    }
    auto it = lower_bound(Vm[X[i] - Y[i] + geta].begin(),
                          Vm[X[i] - Y[i] + geta].end(), Y[i]);
    if (it == Vm[X[i] - Y[i] + geta].end()) {
      continue;
    }
    ans = min(ans, ((*it) - Y[i]) * 10);
  }

  /*	S.clear(); T.clear();
          for (int i = 1; i <= N; i++) {
                  if (U[i] == "R") { S.insert(make_pair(X[i] - Y[i], Y[i])); }
                  else if (U[i] == "D") { T.insert(make_pair(X[i] - Y[i],
     Y[i])); }
          }

          for (auto v : S) {

                  auto it = upper_bound(T.begin(), T.end(), make_pair(v.first,
     INF)); auto it2 = lower_bound(T.begin(), T.end(), make_pair(v.first,
     v.second));
                  ;
                  if (it != it2) {
                          auto itr = lower_bound(T.begin(), T.end(),
     make_pair(v.first, v.second)); if (itr == T.end()) { continue; } ans =
     min(ans, ((*itr).second - v.second)*10);
                  }
          }
  */
  //======= U D ===//

  S.clear();
  T.clear();
  for (int i = 1; i <= N; i++) {
    if (U[i] == "U") {
      S.insert(make_pair(X[i], Y[i]));
    } else if (U[i] == "D") {
      T.insert(make_pair(X[i], Y[i]));
    }
  }

  for (auto v : S) {

    auto it = upper_bound(T.begin(), T.end(), make_pair(v.first, INF));
    auto it2 = lower_bound(T.begin(), T.end(), make_pair(v.first, v.second));
    ;
    if (it != it2) {
      auto itr = lower_bound(T.begin(), T.end(), make_pair(v.first, v.second));
      if (itr == T.end()) {
        continue;
      }
      ans = min(ans, ((*itr).second - v.second) * 5);
    }
  }

  //======= L R
  S.clear();
  T.clear();
  for (int i = 1; i <= N; i++) {
    if (U[i] == "R") {
      S.insert(make_pair(Y[i], X[i]));
    } else if (U[i] == "L") {
      T.insert(make_pair(Y[i], X[i]));
    }
  }

  for (auto v : S) {

    auto it = upper_bound(T.begin(), T.end(), make_pair(v.first, INF));
    auto it2 = lower_bound(T.begin(), T.end(), make_pair(v.first, v.second));
    ;
    if (it != it2) {
      auto itr = lower_bound(T.begin(), T.end(), make_pair(v.first, v.second));
      if (itr == T.end()) {
        continue;
      }
      ans = min(ans, ((*itr).second - v.second) * 5);
    }
  }

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

  return 0;
}