Problem p02605 (C++) — buggy submission, verdict: Runtime Error

#pragma target("avx")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef unordered_map<ll, ll> U_MAP;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> rpq;
constexpr ll INF = 1e9, MOD = 1e9 + 7, ohara = 1e6 + 10;
constexpr ll LINF = 1e18;

#define rep(i, n) for (ll(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (ll i = (a); i < (b); i++)
#define rrrep(i, a, b) for (ll i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl
#define doublecout(a) cout << fixed << setprecision(15) << a << endl;
#define fi first
#define se second
#define m_p make_pair
#define p_b push_back
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

//------ Believe yourself as a genius!!!!!! ------

int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
// int dy[]={-1,0,1,-1,1,-1,0,1};int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"), s[2 * 100000 + 100];
ll n, cnt, ans, b, c, d, tmp, m, h, w, x[2 * 100000 + 100], y[2 * 100000 + 100],
    sum, k, q;
vector<pair<ll, string>> dat1[4 * 100000 + 100];
pair<P, string> p[ohara], p2[ohara];
vector<ll> sp1[2 * 100000 + 100];
vector<ll> sp2[2 * 100000 + 100];

void solve() {
  rep(i, 4 * 100000 + 1) dat1[i].clear();
  rep(i, n) {
    if (s[i] == "U")
      dat1[x[i] + y[i]].p_b({x[i], "U"});
    else if (s[i] == "R")
      dat1[x[i] + y[i]].p_b({x[i], "R"});
  }
  cnt = LINF / 10;
  rep(i, 4 * 100000 + 1) {
    sort(all(dat1[i]));
    rep(j, Size(dat1[i]) - 1) {
      if (dat1[i][j].se == "R" && dat1[i][j + 1].se == "U") {
        cnt = min(cnt, dat1[i][j + 1].fi - dat1[i][j].fi);
      }
    }
  }
  ans = min(ans, cnt * 10);
}

int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  ans = LINF;
  rep(i, n) {
    cin >> x[i] >> y[i] >> s[i];
    p[i] = m_p(m_p(x[i], y[i]), s[i]);
    p2[i] = m_p(m_p(y[i], x[i]), s[i]);
  }
  sort(p, p + n);
  sort(p2, p2 + n);
  rep(i, n) {
    x[i] = p2[i].fi.se;
    y[i] = p2[i].fi.fi;
    s[i] = p2[i].se;
  }
  rep(i, n) {
    if (s[i] == "U")
      sp1[x[i]].p_b(y[i]);
  }
  rep(i, n) {
    x[i] = p[i].fi.fi;
    y[i] = p[i].fi.se;
    s[i] = p[i].se;
  }
  rep(i, n) {
    if (s[i] == "R")
      sp2[y[i]].p_b(x[i]);
  }
  rep(i, n) {
    if (s[i] == "D") {
      ll le = 0, ri = Size(sp1[x[i]]) - 1, mid;
      if (ri == -1)
        continue;
      while (ri - le > 1) {
        mid = (le + ri) / 2;
        if (sp1[x[i]][mid] < y[i])
          le = mid;
        else
          ri = mid;
      }
      cnt = LINF;
      if ((y[i] - sp1[x[i]][le]) >= 0)
        cnt = (y[i] - sp1[x[i]][le]) * 10 / 2;
      if ((y[i] - sp1[x[i]][ri]) >= 0)
        cnt = min(cnt, (y[i] - sp1[x[i]][ri]) * 10 / 2);
      ans = min(ans, cnt);
    }
  }
  rep(i, n) {
    if (s[i] == "L") {
      ll le = 0, ri = Size(sp2[y[i]]) - 1, mid;
      if (ri == -1)
        continue;
      while (ri - le > 1) {
        mid = (le + ri) / 2;
        if (sp2[y[i]][mid] < x[i])
          le = mid;
        else
          ri = mid;
      }
      cnt = LINF;
      if ((x[i] - sp2[y[i]][le]) >= 0)
        cnt = (x[i] - sp2[y[i]][le]) * 10 / 2;
      if ((x[i] - sp2[y[i]][ri]) >= 0)
        cnt = min(cnt, (x[i] - sp2[y[i]][ri]) * 10 / 2);
      ans = min(ans, cnt);
    }
  }
  solve();
  rep(i, n) {
    x[i] = p[i].fi.se * -1 + 200000;
    y[i] = p[i].fi.fi;
    s[i] = p[i].se;
    if (s[i] == "R")
      s[i] = "U";
    else if (s[i] == "D")
      s[i] = "R";
    else
      s[i] = "D";
  }
  solve();
  rep(i, n) {
    x[i] = p[i].fi.fi * -1 + 200000;
    y[i] = p[i].fi.se * -1 + 200000;
    s[i] = p[i].se;
    if (s[i] == "L")
      s[i] = "R";
    else if (s[i] == "D")
      s[i] = "U";
    else
      s[i] = "D";
  }
  solve();
  rep(i, n) {
    x[i] = p[i].fi.se;
    y[i] = p[i].fi.fi * -1 + 200000;
    s[i] = p[i].se;
    if (s[i] == "U")
      s[i] = "R";
    else if (s[i] == "L")
      s[i] = "U";
    else
      s[i] = "D";
  }
  solve();
  if (ans == LINF)
    Cout("SAFE");
  else
    Cout(ans);
  return 0;
}


Error: TLE

Fixed submission:

#pragma target("avx")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef unordered_map<ll, ll> U_MAP;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> rpq;
constexpr ll INF = 1e9, MOD = 1e9 + 7, ohara = 1e6 + 10;
constexpr ll LINF = 1e18;

#define rep(i, n) for (ll(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (ll i = (a); i < (b); i++)
#define rrrep(i, a, b) for (ll i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl
#define doublecout(a) cout << fixed << setprecision(15) << a << endl;
#define fi first
#define se second
#define m_p make_pair
#define p_b push_back
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

//------ Believe yourself as a genius!!!!!! ------

int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
// int dy[]={-1,0,1,-1,1,-1,0,1};int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"), s[2 * 100000 + 1];
ll n, cnt, ans, b, c, d, tmp, m, h, w, x[2 * 100000 + 1], y[2 * 100000 + 1],
    sum, k, q;
vector<int> dat1[4 * 100000 + 1], dat2[4 * 100000 + 1], dat31[2 * 100000 + 1],
    dat32[2 * 100000 + 1];
vector<int> dat41[2 * 100000 + 1], dat42[2 * 100000 + 1];

int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  ans = LINF;
  rep(i, n) cin >> x[i] >> y[i] >> s[i];
  rep(i, n) {
    if (s[i] == "U")
      dat1[x[i] + y[i]].p_b(x[i]);
  }
  sort(dat1, dat1 + 4 * 100000);
  rep(i, n) {
    if (s[i] == "R") {
      ll nowx = x[i];
      ll le = 0, ri = Size(dat1[x[i] + y[i]]) - 1, mid;
      if (ri == -1)
        continue;
      while (ri - le > 1) {
        mid = (le + ri) / 2;
        if (dat1[x[i] + y[i]][mid] < nowx)
          le = mid;
        else
          ri = mid;
      }
      cnt = abs(dat1[x[i] + y[i]][le] - nowx);
      cnt = min(cnt, abs(dat1[x[i] + y[i]][ri] - nowx));
      ans = min(ans, cnt);
    }
  }
  rep(i, n) {
    if (s[i] == "D")
      dat2[x[i] + y[i]].p_b(x[i]);
  }
  sort(dat2, dat2 + 4 * 100000);
  rep(i, n) {
    if (s[i] == "L") {
      ll nowx = x[i];
      ll le = 0, ri = Size(dat2[x[i] + y[i]]) - 1, mid;
      if (ri == -1)
        continue;
      while (ri - le > 1) {
        mid = (le + ri) / 2;
        if (dat2[x[i] + y[i]][mid] < nowx)
          le = mid;
        else
          ri = mid;
      }
      cnt = abs(dat2[x[i] + y[i]][le] - nowx);
      cnt = min(cnt, abs(dat2[x[i] + y[i]][ri] - nowx));
      ans = min(ans, cnt);
    }
  }

  //////////
  rep(i, n) {
    if (s[i] == "D") {
      if (x[i] >= y[i])
        dat31[abs(x[i] - y[i])].p_b(x[i]);
      else
        dat32[abs(x[i] - y[i])].p_b(x[i]);
    }
  }
  sort(dat31, dat31 + 4 * 100000);
  sort(dat32, dat32 + 4 * 100000);
  rep(i, n) {
    if (s[i] == "R") {
      ll nowx = x[i];
      if (x[i] >= y[i]) {
        ll le = 0, ri = Size(dat31[abs(x[i] - y[i])]) - 1, mid;
        if (ri == -1)
          continue;
        while (ri - le > 1) {
          mid = (le + ri) / 2;
          if (dat31[abs(x[i] - y[i])][mid] < nowx)
            le = mid;
          else
            ri = mid;
        }
        cnt = abs(dat31[abs(x[i] - y[i])][le] - nowx);
        cnt = min(cnt, abs(dat31[abs(x[i] - y[i])][ri] - nowx));
        ans = min(ans, cnt);
      } else {
        ll le = 0, ri = Size(dat32[abs(x[i] - y[i])]) - 1, mid;
        if (ri == -1)
          continue;
        while (ri - le > 1) {
          mid = (le + ri) / 2;
          if (dat32[abs(x[i] - y[i])][mid] < nowx)
            le = mid;
          else
            ri = mid;
        }
        cnt = abs(dat32[abs(x[i] - y[i])][le] - nowx);
        cnt = min(cnt, abs(dat32[abs(x[i] - y[i])][ri] - nowx));
        ans = min(ans, cnt);
      }
    }
  }

  ////
  rep(i, n) {
    if (s[i] == "U") {
      if (x[i] >= y[i])
        dat41[abs(x[i] - y[i])].p_b(x[i]);
      else
        dat42[abs(x[i] - y[i])].p_b(x[i]);
    }
  }
  sort(dat41, dat41 + 4 * 100000);
  sort(dat42, dat42 + 4 * 100000);
  rep(i, n) {
    if (s[i] == "L") {
      ll nowx = x[i];
      if (x[i] >= y[i]) {
        ll le = 0, ri = Size(dat41[abs(x[i] - y[i])]) - 1, mid;
        if (ri == -1)
          continue;
        while (ri - le > 1) {
          mid = (le + ri) / 2;
          if (dat41[abs(x[i] - y[i])][mid] < nowx)
            le = mid;
          else
            ri = mid;
        }
        cnt = abs(dat41[abs(x[i] - y[i])][le] - nowx);
        cnt = min(cnt, abs(dat41[abs(x[i] - y[i])][ri] - nowx));
        ans = min(ans, cnt);
      } else {
        ll le = 0, ri = Size(dat42[abs(x[i] - y[i])]) - 1, mid;
        if (ri == -1)
          continue;
        while (ri - le > 1) {
          mid = (le + ri) / 2;
          if (dat42[abs(x[i] - y[i])][mid] < nowx)
            le = mid;
          else
            ri = mid;
        }
        cnt = abs(dat42[abs(x[i] - y[i])][le] - nowx);
        cnt = min(cnt, abs(dat42[abs(x[i] - y[i])][ri] - nowx));
        ans = min(ans, cnt);
      }
    }
  }
  if (ans == LINF)
    Cout("SAFE");
  else
    Cout(ans);
  return 0;
}