Problem p03802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define dbg(...)                                                               \
  do {                                                                         \
    cerr << __LINE__ << ": ";                                                  \
    dbgprint(#__VA_ARGS__, __VA_ARGS__);                                       \
  } while (0);

using namespace std;

namespace std {
template <class S, class T> struct hash<pair<S, T>> {
  size_t operator()(const pair<S, T> &p) const {
    return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second);
  }
};
template <class T> struct hash<vector<T>> {
  size_t operator()(const vector<T> &v) const {
    size_t h = 0;
    for (auto i : v)
      h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1;
    return h;
  }
};
} // namespace std
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[ ";
  rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? " ]" : ", ");
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{ ";
  for (const auto &i : v)
    os << i << ", ";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &v) {
  os << "{";
  for (const auto &i : v)
    os << " " << i.first << ": " << i.second << ",";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
void dbgprint(const string &fmt) { cerr << endl; }
template <class H, class... T>
void dbgprint(const string &fmt, const H &h, const T &...r) {
  cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " ";
  dbgprint(fmt.substr(fmt.find(",") + 1), r...);
}
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int N;
vector<vi> e;

void rec(int from, int a, int b, int k, int l, int r) {
  if (b <= a || b <= l || a >= r)
    return;
  if (a <= l && r <= b) {
    // dbg(from, k);
    e[from].pb(k);
    return;
  }
  rec(from, a, b, 2 * k + 1, l, (l + r) / 2);
  rec(from, a, b, 2 * k + 2, (l + r) / 2, r);
}

void visit(const vector<vi> &g, int v, vector<vector<int>> &scc, stack<int> &S,
           vector<bool> &inS, vector<int> &low, vector<int> &num, int &time) {
  low[v] = num[v] = ++time;
  S.push(v);
  inS[v] = true;
  for (int e : g[v]) {
    int w = e;
    if (num[w] == 0) {
      visit(g, w, scc, S, inS, low, num, time);
      low[v] = min(low[v], low[w]);
    } else if (inS[w])
      low[v] = min(low[v], num[w]);
  }
  if (low[v] == num[v]) {
    scc.push_back(vector<int>());
    while (1) {
      int w = S.top();
      S.pop();
      inS[w] = false;
      scc.back().push_back(w);
      if (v == w)
        break;
    }
  }
}
void stronglyConnectedComponents(const vector<vi> &g,
                                 vector<vector<int>> &scc) {
  const int n = g.size();
  vector<int> num(n), low(n);
  stack<int> S;
  vector<bool> inS(n);
  int time = 0;
  rep(u, n) if (num[u] == 0) visit(g, u, scc, S, inS, low, num, time);
}

int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int n;
  cin >> n;
  vector<pi> v;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    v.emplace_back(x, i);
    v.emplace_back(y, i + n);
  }
  sort(all(v));
  vi pos(2 * n);
  rep(i, 2 * n) pos[v[i].second] = i;
  rep(i, n) swap(pos[i], pos[i + n]);

  // rep(i, 2 * n) dbg(i, v[i]); rep(i, 2 * n) dbg(i, pos[i]);

  for (N = 1; N < 2 * n; N *= 2)
    ;
  int lo = 0, hi = inf, mid;
  while (lo + 1 < hi) {
    mid = (lo + hi) / 2;

    e.clear();
    e.resize(2 * N - 1);

    for (int i = N - 2; i >= 0; i--) {
      e[i].pb(i * 2 + 1);
      e[i].pb(i * 2 + 2);
    }
    rep(i, 2 * n) {
      int p = pos[v[i].second];
      int l = v[p].first - mid, r = v[p].first + mid;

      // dbg(i, v[i], l, r);

      l = lower_bound(all(v), pi(l, inf)) - v.begin();
      r = lower_bound(all(v), pi(r, 0)) - v.begin();

      // dbg(i, v[i], l, r);

      rec(i + N - 1, l, p, 0, 0, N);
      rec(i + N - 1, p + 1, r, 0, 0, N);
    }

    vi to(N);
    vector<vi> scc;
    stronglyConnectedComponents(e, scc);

    rep(i, scc.size()) for (int j : scc[i]) if (j >= N - 1) to[j - N - 1] = i;
    rep(i, 2 * n) if (to[i] == to[pos[v[i].second]]) {
      hi = mid;
      goto NEXT;
    }
    lo = mid;
  NEXT:;

    // dbg(scc); dbg(to);
  }
  cout << lo << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define dbg(...)                                                               \
  do {                                                                         \
    cerr << __LINE__ << ": ";                                                  \
    dbgprint(#__VA_ARGS__, __VA_ARGS__);                                       \
  } while (0);

using namespace std;

namespace std {
template <class S, class T> struct hash<pair<S, T>> {
  size_t operator()(const pair<S, T> &p) const {
    return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second);
  }
};
template <class T> struct hash<vector<T>> {
  size_t operator()(const vector<T> &v) const {
    size_t h = 0;
    for (auto i : v)
      h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1;
    return h;
  }
};
} // namespace std
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[ ";
  rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? " ]" : ", ");
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{ ";
  for (const auto &i : v)
    os << i << ", ";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &v) {
  os << "{";
  for (const auto &i : v)
    os << " " << i.first << ": " << i.second << ",";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
void dbgprint(const string &fmt) { cerr << endl; }
template <class H, class... T>
void dbgprint(const string &fmt, const H &h, const T &...r) {
  cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " ";
  dbgprint(fmt.substr(fmt.find(",") + 1), r...);
}
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int N;
vector<vi> e;

void rec(int from, int a, int b, int k, int l, int r) {
  if (b <= a || b <= l || a >= r)
    return;
  if (a <= l && r <= b) {
    // dbg(from, k);
    e[from].pb(k);
    return;
  }
  rec(from, a, b, 2 * k + 1, l, (l + r) / 2);
  rec(from, a, b, 2 * k + 2, (l + r) / 2, r);
}

void visit(const vector<vi> &g, int v, vector<vector<int>> &scc, stack<int> &S,
           vector<bool> &inS, vector<int> &low, vector<int> &num, int &time) {
  low[v] = num[v] = ++time;
  S.push(v);
  inS[v] = true;
  for (int e : g[v]) {
    int w = e;
    if (num[w] == 0) {
      visit(g, w, scc, S, inS, low, num, time);
      low[v] = min(low[v], low[w]);
    } else if (inS[w])
      low[v] = min(low[v], num[w]);
  }
  if (low[v] == num[v]) {
    scc.push_back(vector<int>());
    while (1) {
      int w = S.top();
      S.pop();
      inS[w] = false;
      scc.back().push_back(w);
      if (v == w)
        break;
    }
  }
}
void stronglyConnectedComponents(const vector<vi> &g,
                                 vector<vector<int>> &scc) {
  const int n = g.size();
  vector<int> num(n), low(n);
  stack<int> S;
  vector<bool> inS(n);
  int time = 0;
  rep(u, n) if (num[u] == 0) visit(g, u, scc, S, inS, low, num, time);
}

int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int n;
  cin >> n;
  vector<pi> v;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    v.emplace_back(x, i);
    v.emplace_back(y, i + n);
  }
  sort(all(v));
  vi pos(2 * n);
  rep(i, 2 * n) pos[v[i].second] = i;
  rep(i, n) swap(pos[i], pos[i + n]);

  // rep(i, 2 * n) dbg(i, v[i]); rep(i, 2 * n) dbg(i, pos[i]);

  for (N = 1; N < 2 * n; N *= 2)
    ;
  int lo = 0, hi = inf, mid;
  while (lo + 1 < hi) {
    mid = (lo + hi) / 2;

    e.clear();
    e.resize(2 * N - 1);

    for (int i = N - 2; i >= 0; i--) {
      e[i].pb(i * 2 + 1);
      e[i].pb(i * 2 + 2);
    }
    rep(i, 2 * n) {
      int p = pos[v[i].second];
      int l = v[p].first - mid, r = v[p].first + mid;

      // dbg(i, v[i], l, r);

      l = lower_bound(all(v), pi(l, inf)) - v.begin();
      r = lower_bound(all(v), pi(r, 0)) - v.begin();

      // dbg(i, v[i], l, r);

      rec(i + N - 1, l, p, 0, 0, N);
      rec(i + N - 1, p + 1, r, 0, 0, N);
    }

    vi to(N);
    vector<vi> scc;
    stronglyConnectedComponents(e, scc);

    rep(i, scc.size()) for (int j : scc[i]) if (j >= N - 1) to[j - N + 1] = i;
    rep(i, 2 * n) if (to[i] == to[pos[v[i].second]]) {
      hi = mid;
      goto NEXT;
    }
    lo = mid;
  NEXT:;

    // dbg(scc); dbg(to);
  }
  cout << lo << endl;
  return 0;
}