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

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n)                                                           \\
  for (ll i = 0; i < n; i++) {                                                 \\
    cout << (x[i]) << " \\n"[i == n - 1];                                       \\
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \\n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
typedef struct {
  ll x;
  ll y;
} P;
vector<pair<ll, char>> x_val[200010], y_val[200010];
ll X[200010], Y[200010];
char U[200010];
vector<P> vu, vd, vr, vl;

vector<ll> v_plus_l[400010], v_minus_l[400010];
vector<ll> v_plus_r[400010], v_minus_r[400010];
vector<ll> v_plus_d[400010], v_minus_d[400010];
vector<ll> v_plus_u[400010], v_minus_u[400010];

void solve() {
  ll N;
  cin >> N;
  rep(i, 0, N) cin >> X[i] >> Y[i] >> U[i];
  ll ans = INF;
  rep(i, 0, N) {
    if (U[i] == 'U') {
      vu.push_back({X[i], Y[i]});
    } else if (U[i] == 'D') {
      vd.push_back({X[i], Y[i]});

    } else if (U[i] == 'R') {
      vr.push_back({X[i], Y[i]});
    } else if (U[i] == 'L') {
      vl.push_back({X[i], Y[i]});
    }
  }
  rep(i, 0, N) {
    if (U[i] == 'U' || U[i] == 'D') {
      x_val[X[i]].push_back({Y[i], U[i]});
    } else {
      y_val[Y[i]].push_back({X[i], U[i]});
    }
  }
  rep(i, 0, 200001) {
    sort(x_val[i].begin(), x_val[i].end(),
         [](pair<ll, char> l1, pair<ll, char> l2) {
           return l1.first < l2.first;
         });
    sort(y_val[i].begin(), y_val[i].end(),
         [](pair<ll, char> l1, pair<ll, char> l2) {
           return l1.first < l2.first;
         });
  }
  rep(i, 0, 200001) {
    ll sz = x_val[i].size();
    if (sz < 2)
      continue;
    rep(j, 0, sz - 1) {
      if (x_val[i][j].second == 'U' && x_val[i][j + 1].second == 'D') {
        ll dist = x_val[i][j + 1].first - x_val[i][j].first;
        ans = min(ans, dist * 5);
      }
    }
  }
  rep(i, 0, 200001) {
    ll sz = y_val[i].size();
    if (sz < 2)
      continue;
    rep(j, 0, sz - 1) {
      if (y_val[i][j].second == 'R' && y_val[i][j + 1].second == 'L') {
        ll dist = y_val[i][j + 1].first - y_val[i][j].first;
        ans = min(ans, dist * 5);
      }
    }
  }

  rep(i, 0, N) {
    if (U[i] == 'L') {
      v_plus_l[X[i] + Y[i]].push_back(Y[i]);
      v_minus_l[X[i] - Y[i] + 200000].push_back(Y[i]);
    } else if (U[i] == 'R') {
      v_plus_r[X[i] + Y[i]].push_back(Y[i]);
      v_minus_r[X[i] - Y[i] + 200000].push_back(Y[i]);
    } else if (U[i] == 'U') {
      v_plus_u[X[i] + Y[i]].push_back(X[i]);
      v_minus_u[X[i] - Y[i] + 200000].push_back(X[i]);
    } else {
      v_plus_d[X[i] + Y[i]].push_back(X[i]);
      v_minus_d[X[i] - Y[i] + 200000].push_back(X[i]);
    }
  }

  rep(i, 0, 400001) {
    sort(v_plus_l[i].begin(), v_plus_l[i].end());
    sort(v_plus_r[i].begin(), v_plus_r[i].end());
    sort(v_plus_u[i].begin(), v_plus_u[i].end());
    sort(v_plus_d[i].begin(), v_plus_d[i].end());
    sort(v_minus_l[i].begin(), v_minus_l[i].end());
    sort(v_minus_r[i].begin(), v_minus_r[i].end());
    sort(v_minus_u[i].begin(), v_minus_u[i].end());
    sort(v_minus_d[i].begin(), v_minus_d[i].end());
  }

  rep(i, 0, N) {
    if (U[i] == 'U') {
      ll v = X[i] - Y[i] + 200000;
      ll ps = upper_bound(v_minus_l[v].begin(), v_minus_l[v].end(), Y[i]) -
              v_minus_l[v].begin();
      if (ps != v_minus_l[v].size()) {
        ll dist = v_minus_l[v][ps] - Y[i];
        ans = min(ans, dist * 10);
      }

      v = X[i] + Y[i];
      ps = upper_bound(v_plus_r[v].begin(), v_plus_r[v].end(), Y[i]) -
           v_plus_r[v].begin();
      if (ps != v_plus_r[v].size()) {
        ll dist = v_plus_r[v][ps] - Y[i];
        ans = min(ans, dist * 10);
      }
    } else if (U[i] == 'D') {
      ll v = X[i] - Y[i] + 200000;
      ll ps = lower_bound(v_minus_r[v].begin(), v_minus_r[v].end(), Y[i]) -
              v_minus_r[v].begin();
      if (ps != 0) {
        ll dist = Y[i] - v_minus_r[v][ps - 1];
        ans = min(ans, dist * 10);
      }
      v = X[i] + Y[i];
      ps = lower_bound(v_plus_l[v].begin(), v_plus_l[v].end(), Y[i]) -
           v_plus_l[v].begin();
      if (ps != 0) {
        ll dist = Y[i] - v_plus_l[v][ps - 1];
        ans = min(ans, dist * 10);
      }
    } else if (U[i] == 'R') {
      ll v = X[i] - Y[i] + 200000;
      ll ps = upper_bound(v_minus_d[v].begin(), v_minus_d[v].end(), X[i]) -
              v_minus_d[v].begin();
      if (ps != v_minus_d[v].size()) {
        ll dist = v_minus_d[v][ps] - X[i];
        ans = min(ans, dist * 10);
      }

      v = X[i] + Y[i];
      ps = upper_bound(v_plus_u[v].begin(), v_plus_u[v].end(), X[i]) -
           v_plus_u[v].begin();
      if (ps != v_plus_u[v].size()) {
        ll dist = v_plus_u[v][ps] - X[i];
        ans = min(ans, dist * 10);
      }
    } else if (U[i] == 'L') {
      ll v = X[i] - Y[i] + 200000;
      ll ps = lower_bound(v_minus_u[v].begin(), v_minus_u[v].end(), X[i]) -
              v_minus_u[v].begin();
      if (ps != 0) {
        ll dist = X[i] - v_minus_u[v][ps - 1];
        ans = min(ans, dist * 10);
      }

      v = X[i] + Y[i];
      ps = lower_bound(v_plus_d[v].begin(), v_plus_d[v].end(), X[i]) -
           v_plus_d[v].begin();
      if (ps != 0) {
        ll dist = X[i] - v_plus_d[v][ps - 1];
        ans = min(ans, dist * 10);
      }
    }
  }

  // rep(i,0,N){
  //     if(U[i] == 'U'){
  //         for(auto &e: vl){
  //             if(e.x - X[i] == (e.y - Y[i]) && e.y > Y[i]){
  //                 ll dist = e.y - Y[i];
  //                 ans = min(ans, dist * 10);
  //             }
  //         }
  //         for(auto &e: vr){
  //             if(e.x - X[i] == -(e.y - Y[i]) && e.y > Y[i]){
  //                 ll dist = e.y - Y[i];
  //                 ans = min(ans, dist * 10);
  //             }
  //         }
  //     }else if(U[i] == 'D'){
  //         for(auto &e: vl){
  //             if(e.x - X[i] == -(e.y - Y[i]) && e.y < Y[i]){
  //                 ll dist = Y[i] - e.y;
  //                 ans = min(ans, dist * 10);
  //             }
  //         }
  //         for(auto &e: vr){
  //             if(e.x - X[i] == (e.y - Y[i]) && e.y < Y[i]){
  //                 ll dist = Y[i] - e.y;
  //                 ans = min(ans, dist * 10);
  //             }
  //         }

  //     }else if(U[i] == 'R'){
  //         for(auto &e: vd){
  //             if(e.x - X[i] == (e.y - Y[i]) && e.x > X[i]){
  //                 ll dist = e.x - X[i];
  //                 ans = min(ans, dist * 10);
  //             }
  //         }
  //         for(auto &e: vu){
  //             if(e.x - X[i] == -(e.y - Y[i]) && e.x > X[i]){
  //                 ll dist = e.x - X[i];
  //                 ans = min(ans, dist * 10);
  //             }
  //         }
  //     }else if(U[i] == 'L'){
  //         for(auto &e: vd){
  //             if(e.x - X[i] == -(e.y - Y[i]) && e.x < X[i]){
  //                 ll dist = X[i] - e.x;
  //                 ans = min(ans, dist * 10);
  //             }
  //         }
  //         for(auto &e: vu){
  //             if(e.x - X[i] == (e.y - Y[i]) && e.x < X[i]){
  //                 ll dist = X[i] - e.x;
  //                 ans = min(ans, dist * 10);
  //             }
  //         }
  //     }
  // }

  if (ans == INF) {
    print("SAFE");
  } else {
    print(ans);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}

Error: TLE

Fixed submission:

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n)                                                           \\
  for (ll i = 0; i < n; i++) {                                                 \\
    cout << (x[i]) << " \\n"[i == n - 1];                                       \\
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \\n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
typedef struct {
  ll x;
  ll y;
} P;
vector<pair<ll, char>> x_val[200010], y_val[200010];
ll X[200010], Y[200010];
char U[200010];
vector<P> vu, vd, vr, vl;

void solve() {
  ll N;
  cin >> N;
  rep(i, 0, N) cin >> X[i] >> Y[i] >> U[i];
  ll ans = INF;
  rep(i, 0, N) {
    if (U[i] == 'U') {
      vu.push_back({X[i], Y[i]});
    } else if (U[i] == 'D') {
      vd.push_back({X[i], Y[i]});

    } else if (U[i] == 'R') {
      vr.push_back({X[i], Y[i]});
    } else if (U[i] == 'L') {
      vl.push_back({X[i], Y[i]});
    }
  }
  rep(i, 0, N) {
    if (U[i] == 'U' || U[i] == 'D') {
      x_val[X[i]].push_back({Y[i], U[i]});
    } else {
      y_val[Y[i]].push_back({X[i], U[i]});
    }
  }
  rep(i, 0, 200001) {
    sort(x_val[i].begin(), x_val[i].end(),
         [](pair<ll, char> l1, pair<ll, char> l2) {
           return l1.first < l2.first;
         });
    sort(y_val[i].begin(), y_val[i].end(),
         [](pair<ll, char> l1, pair<ll, char> l2) {
           return l1.first < l2.first;
         });
  }
  rep(i, 0, 200001) {
    ll sz = x_val[i].size();
    if (sz < 2)
      continue;
    rep(j, 0, sz - 1) {
      if (x_val[i][j].second == 'U' && x_val[i][j + 1].second == 'D') {
        ll dist = x_val[i][j + 1].first - x_val[i][j].first;
        ans = min(ans, dist * 5);
      }
    }
  }
  rep(i, 0, 200001) {
    ll sz = y_val[i].size();
    if (sz < 2)
      continue;
    rep(j, 0, sz - 1) {
      if (y_val[i][j].second == 'R' && y_val[i][j + 1].second == 'L') {
        ll dist = y_val[i][j + 1].first - y_val[i][j].first;
        ans = min(ans, dist * 5);
      }
    }
  }

  rep(i, 0, N) {
    if (U[i] == 'U') {
      // for(auto &e: vd){
      //     if(e.x == X[i] && e.y > Y[i]){
      //         ll dist = abs(Y[i] - e.y);
      //         ans = min(ans, dist*5);
      //     }
      // }
      for (auto &e : vl) {
        if (e.x - X[i] == (e.y - Y[i]) && e.y > Y[i]) {
          ll dist = e.y - Y[i];
          ans = min(ans, dist * 10);
        }
      }
      for (auto &e : vr) {
        if (e.x - X[i] == -(e.y - Y[i]) && e.y > Y[i]) {
          ll dist = e.y - Y[i];
          ans = min(ans, dist * 10);
        }
      }
    } else if (U[i] == 'D') {
      // for(auto &e: vu){
      //     if(e.x == X[i] && e.y < Y[i]){
      //         ll dist = abs(Y[i] - e.y);
      //         ans = min(ans, dist*5);
      //     }
      // }
      for (auto &e : vl) {
        if (e.x - X[i] == -(e.y - Y[i]) && e.y < Y[i]) {
          ll dist = Y[i] - e.y;
          ans = min(ans, dist * 10);
        }
      }
      for (auto &e : vr) {
        if (e.x - X[i] == (e.y - Y[i]) && e.y < Y[i]) {
          ll dist = Y[i] - e.y;
          ans = min(ans, dist * 10);
        }
      }

    } else if (U[i] == 'R') {
      // for(auto &e: vl){
      //     if(e.y == Y[i] && e.x > X[i]){
      //         ll dist = abs(X[i] - e.x);
      //         ans = min(ans, dist*5);
      //     }
      // }
      for (auto &e : vd) {
        if (e.x - X[i] == (e.y - Y[i]) && e.x > X[i]) {
          ll dist = e.x - X[i];
          ans = min(ans, dist * 10);
        }
      }
      for (auto &e : vu) {
        if (e.x - X[i] == -(e.y - Y[i]) && e.x > X[i]) {
          ll dist = e.x - X[i];
          ans = min(ans, dist * 10);
        }
      }
    } else if (U[i] == 'L') {
      // for(auto &e: vr){
      //     if(e.y == Y[i] && e.x < X[i]){
      //         ll dist = abs(X[i] - e.x);
      //         ans = min(ans, dist*5);
      //     }
      // }
      for (auto &e : vd) {
        if (e.x - X[i] == -(e.y - Y[i]) && e.x < X[i]) {
          ll dist = X[i] - e.x;
          ans = min(ans, dist * 10);
        }
      }
      for (auto &e : vu) {
        if (e.x - X[i] == (e.y - Y[i]) && e.x < X[i]) {
          ll dist = X[i] - e.x;
          ans = min(ans, dist * 10);
        }
      }
    }
  }

  if (ans == INF) {
    print("SAFE");
  } else {
    print(ans);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}