Problem p03802 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <vector>
#define pb(x) push_back(x)
using namespace std;
inline int rd(int f = 1, int x = 0, char ch = ' ') {
  while (!isdigit(ch = getchar()))
    if (ch == '-')
      f = -1;
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f * x;
}
const int N = 2e4 + 5, L = 16;
int n, st[L][N], l2[N], ans, t, ti, top, s[N], dfn[N], low[N], ins[N], scc[N],
    cnt;
vector<int> e[N * L];
struct V {
  int x, v;
} a[N];
bool operator<(V a, V b) { return a.v < b.v; }
int opp(int x) { return x <= n ? x + n : x - n; }
void add(int x, int l, int r) {
  if (l > r)
    return;
  int k = l2[r - l + 1];
  e[x].pb(st[k][l]), e[x].pb(st[k][r - (1 << k) + 1]);
}
void tarjan(int x) {
  dfn[x] = low[x] = ++ti, ins[x] = 1, s[++top] = x;
  for (int y : e[x])
    if (!dfn[y])
      tarjan(y), low[x] = min(low[x], low[y]);
    else if (ins[y])
      low[x] = min(low[x], dfn[y]);
  if (low[x] == dfn[x]) {
    int z;
    ++cnt;
    do
      z = s[top--], ins[z] = 0, scc[z] = cnt;
    while (z != x);
  }
}
bool chk(int d) {
  memset(dfn + 1, 0, sizeof(int) * t), ti = cnt = 0;
  for (int i = 1; i <= n << 1; ++i)
    e[i].clear();
  for (int i = 1, j = i; i <= n << 1; ++i) {
    while (j < i && a[i].v - d >= a[j].v)
      ++j;
    add(a[i].x, j, i - 1);
  }
  for (int i = n << 1, j = i; i; --i) {
    while (j > i && a[j].v >= a[i].v + d)
      --j;
    add(a[i].x, i + 1, j);
  }
  for (int i = 1; i <= t; ++i)
    if (!dfn[i])
      tarjan(i);
  for (int i = 1; i <= n; ++i)
    if (scc[i] == scc[i + n])
      return false;
  return true;
}
int main() {
  n = rd(), t = n << 1;
  for (int i = 1; i <= n; ++i)
    a[i].v = rd(), a[i].x = i, a[i + n].v = rd(), a[i + n].x = i + n;
  sort(a + 1, a + n * 2 + 1), l2[0] = -1;
  for (int i = 1; i <= n << 1; ++i)
    st[0][i] = opp(a[i].x), l2[i] = l2[i >> 1] + 1;
  for (int i = 1, k = l2[n << 1]; i <= k; ++i)
    for (int j = 1; j + (1 << i) - 1 <= n << 1; ++j)
      st[i][j] = ++t, e[t].pb(st[i - 1][j]),
      e[t].pb(st[i - 1][j + (1 << (i - 1))]);
  for (int l = 0, r = a[n << 1].v - a[1].v, x; l <= r;)
    x = (l + r) >> 1, chk(x) ? (l = x + 1, ans = x) : (r = x - 1);
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <vector>
#define pb(x) push_back(x)
using namespace std;
inline int rd(int f = 1, int x = 0, char ch = ' ') {
  while (!isdigit(ch = getchar()))
    if (ch == '-')
      f = -1;
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f * x;
}
const int N = 2e4 + 5, L = 16;
int n, st[L][N], l2[N], ans, t, ti, top, s[N * L], dfn[N * L], low[N * L],
    ins[N * L], scc[N * L], cnt;
vector<int> e[N * L];
struct V {
  int x, v;
} a[N];
bool operator<(V a, V b) { return a.v < b.v; }
int opp(int x) { return x <= n ? x + n : x - n; }
void add(int x, int l, int r) {
  if (l > r)
    return;
  int k = l2[r - l + 1];
  e[x].pb(st[k][l]), e[x].pb(st[k][r - (1 << k) + 1]);
}
void tarjan(int x) {
  dfn[x] = low[x] = ++ti, ins[x] = 1, s[++top] = x;
  for (int y : e[x])
    if (!dfn[y])
      tarjan(y), low[x] = min(low[x], low[y]);
    else if (ins[y])
      low[x] = min(low[x], dfn[y]);
  if (low[x] == dfn[x]) {
    int z;
    ++cnt;
    do
      z = s[top--], ins[z] = 0, scc[z] = cnt;
    while (z != x);
  }
}
bool chk(int d) {
  memset(dfn + 1, 0, sizeof(int) * t), ti = cnt = 0;
  for (int i = 1; i <= n << 1; ++i)
    e[i].clear();
  for (int i = 1, j = i; i <= n << 1; ++i) {
    while (j < i && a[i].v - d >= a[j].v)
      ++j;
    add(a[i].x, j, i - 1);
  }
  for (int i = n << 1, j = i; i; --i) {
    while (j > i && a[j].v >= a[i].v + d)
      --j;
    add(a[i].x, i + 1, j);
  }
  for (int i = 1; i <= t; ++i)
    if (!dfn[i])
      tarjan(i);
  for (int i = 1; i <= n; ++i)
    if (scc[i] == scc[i + n])
      return false;
  return true;
}
int main() {
  n = rd(), t = n << 1;
  for (int i = 1; i <= n; ++i)
    a[i].v = rd(), a[i].x = i, a[i + n].v = rd(), a[i + n].x = i + n;
  sort(a + 1, a + n * 2 + 1), l2[0] = -1;
  for (int i = 1; i <= n << 1; ++i)
    st[0][i] = opp(a[i].x), l2[i] = l2[i >> 1] + 1;
  for (int i = 1, k = l2[n << 1]; i <= k; ++i)
    for (int j = 1; j + (1 << i) - 1 <= n << 1; ++j)
      st[i][j] = ++t, e[t].pb(st[i - 1][j]),
      e[t].pb(st[i - 1][j + (1 << (i - 1))]);
  for (int l = 0, r = a[n << 1].v - a[1].v, x; l <= r;)
    x = (l + r) >> 1, chk(x) ? (l = x + 1, ans = x) : (r = x - 1);
  printf("%d\n", ans);
  return 0;
}