Problem p03802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define maxn 10005
using namespace std;
typedef long long LL;
int read() {
  char c;
  int sum = 0, f = 1;
  c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum * f;
}
int n;
struct node {
  int x, id, rev;
  friend bool operator<(node a, node b) { return a.x < b.x; }
} d[maxn * 2];
int a[maxn], b[maxn], c[maxn * 2];
int leaf[maxn * 2], lson[maxn * 100], rson[maxn * 100], tot;
int head[maxn * 2], to[maxn * 4], nex[maxn * 4], cnt, num;
int root, top, sum, cur, s[maxn * 2], dfn[maxn * 2], low[maxn * 2],
    bel[maxn * 2];
bool vis[maxn * 2];
void add(int u, int v) {
  to[++cnt] = v;
  nex[cnt] = head[u];
  head[u] = cnt;
}
void build(int &x, int l, int r) {
  if (!x)
    x = ++tot;
  if (l == r) {
    leaf[l] = x;
    return;
  }
  int mid = (l + r) >> 1;
  build(lson[x], l, mid);
  build(rson[x], mid + 1, r);
}
void connect(int x, int l, int r) {
  if (l == r)
    return;
  add(x, lson[x]);
  add(x, rson[x]);
  int mid = (l + r) >> 1;
  connect(lson[x], l, mid);
  connect(rson[x], mid + 1, r);
}
void insert(int x, int l, int r, int ql, int qr, int id) {
  if (ql > qr)
    return;
  if (l == ql && qr == r) {
    add(id, x);
    return;
  }
  int mid = (l + r) >> 1;
  if (qr <= mid)
    insert(lson[x], l, mid, ql, qr, id);
  else if (ql > mid)
    insert(rson[x], mid + 1, r, ql, qr, id);
  else
    insert(lson[x], l, mid, ql, mid, id),
        insert(rson[x], mid + 1, r, mid + 1, qr, id);
}
void ins(int id, int x, int dis) {
  int L, R, l, r, mid;
  l = x;
  r = 2 * n;
  while (l < r) {
    mid = (l + r + 1) / 2;
    if (c[mid] - c[x] < dis)
      l = mid;
    else
      r = mid - 1;
  }
  R = l;
  l = 1;
  r = x;
  while (l < r) {
    mid = (l + r) / 2;
    if (c[x] - c[mid] < dis)
      r = mid;
    else
      l = mid + 1;
  }
  L = l;
  insert(root, 1, 2 * n, L, x - 1, id);
  insert(root, 1, 2 * n, x + 1, R, id);
}
void tarjan(int x) {
  s[++top] = x;
  dfn[x] = low[x] = ++cur;
  vis[x] = 1;
  for (int i = head[x]; i; i = nex[i]) {
    if (!dfn[to[i]]) {
      tarjan(to[i]);
      low[x] = min(low[x], low[to[i]]);
    } else if (vis[to[i]])
      low[x] = min(low[x], dfn[to[i]]);
  }
  if (dfn[x] == low[x]) {
    sum++;
    do {
      vis[s[top]] = 0;
      bel[s[top]] = sum;
      top--;
    } while (s[top + 1] != x);
  }
}
bool check(int x) {
  cnt = 0;
  memset(head, 0, sizeof(head));
  connect(root, 1, 2 * n);
  for (int i = 1; i <= n; i++) {
    add(leaf[b[i]], i * 2 - 1);
    add(leaf[a[i]], i * 2);
    ins(i * 2 - 1, a[i], x);
    ins(i * 2, b[i], x);
  }
  top = cur = sum = 0;
  memset(vis, 0, sizeof(vis));
  memset(dfn, 0, sizeof(dfn));
  memset(low, 0, sizeof(low));
  for (int i = 1; i <= num; i++)
    if (!dfn[i])
      tarjan(i);
  for (int i = 1; i <= n; i++)
    if (bel[i * 2 - 1] == bel[i * 2])
      return false;
  return true;
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read(), b[i] = read();
    d[i * 2 - 1].x = a[i];
    d[i * 2 - 1].rev = 0;
    d[i * 2].x = b[i];
    d[i * 2].rev = 1;
    d[i * 2 - 1].id = d[i * 2].id = i;
  }
  sort(d + 1, d + 2 * n + 1);
  for (int i = 1; i <= 2 * n; i++) {
    if (!d[i].rev)
      a[d[i].id] = i;
    else
      b[d[i].id] = i;
    c[i] = d[i].x;
  }
  tot = 2 * n;
  build(root, 1, 2 * n);
  num = tot;
  int l = 0, r = 1000000000, mid;
  while (l < r) {
    mid = (l + r + 1) / 2;
    if (check(mid))
      l = mid;
    else
      r = mid - 1;
  }
  printf("%d\n", l);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define maxn 10005
using namespace std;
typedef long long LL;
int read() {
  char c;
  int sum = 0, f = 1;
  c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum * f;
}
int n;
struct node {
  int x, id, rev;
  friend bool operator<(node a, node b) { return a.x < b.x; }
} d[maxn * 2];
int a[maxn], b[maxn], c[maxn * 2];
int leaf[maxn * 2], lson[maxn * 10], rson[maxn * 10], tot;
int head[maxn * 10], to[maxn * 100], nex[maxn * 100], cnt, num;
int root, top, sum, cur, s[maxn * 10], dfn[maxn * 10], low[maxn * 10],
    bel[maxn * 10];
bool vis[maxn * 10];
void add(int u, int v) {
  to[++cnt] = v;
  nex[cnt] = head[u];
  head[u] = cnt;
}
void build(int &x, int l, int r) {
  if (!x)
    x = ++tot;
  if (l == r) {
    leaf[l] = x;
    return;
  }
  int mid = (l + r) >> 1;
  build(lson[x], l, mid);
  build(rson[x], mid + 1, r);
}
void connect(int x, int l, int r) {
  if (l == r)
    return;
  add(x, lson[x]);
  add(x, rson[x]);
  int mid = (l + r) >> 1;
  connect(lson[x], l, mid);
  connect(rson[x], mid + 1, r);
}
void insert(int x, int l, int r, int ql, int qr, int id) {
  if (ql > qr)
    return;
  if (l == ql && qr == r) {
    add(id, x);
    return;
  }
  int mid = (l + r) >> 1;
  if (qr <= mid)
    insert(lson[x], l, mid, ql, qr, id);
  else if (ql > mid)
    insert(rson[x], mid + 1, r, ql, qr, id);
  else
    insert(lson[x], l, mid, ql, mid, id),
        insert(rson[x], mid + 1, r, mid + 1, qr, id);
}
void ins(int id, int x, int dis) {
  int L, R, l, r, mid;
  l = x;
  r = 2 * n;
  while (l < r) {
    mid = (l + r + 1) / 2;
    if (c[mid] - c[x] < dis)
      l = mid;
    else
      r = mid - 1;
  }
  R = l;
  l = 1;
  r = x;
  while (l < r) {
    mid = (l + r) / 2;
    if (c[x] - c[mid] < dis)
      r = mid;
    else
      l = mid + 1;
  }
  L = l;
  insert(root, 1, 2 * n, L, x - 1, id);
  insert(root, 1, 2 * n, x + 1, R, id);
}
void tarjan(int x) {
  s[++top] = x;
  dfn[x] = low[x] = ++cur;
  vis[x] = 1;
  for (int i = head[x]; i; i = nex[i]) {
    if (!dfn[to[i]]) {
      tarjan(to[i]);
      low[x] = min(low[x], low[to[i]]);
    } else if (vis[to[i]])
      low[x] = min(low[x], dfn[to[i]]);
  }
  if (dfn[x] == low[x]) {
    sum++;
    do {
      vis[s[top]] = 0;
      bel[s[top]] = sum;
      top--;
    } while (s[top + 1] != x);
  }
}
bool check(int x) {
  cnt = 0;
  memset(head, 0, sizeof(head));
  connect(root, 1, 2 * n);
  for (int i = 1; i <= n; i++) {
    add(leaf[b[i]], i * 2 - 1);
    add(leaf[a[i]], i * 2);
    ins(i * 2 - 1, a[i], x);
    ins(i * 2, b[i], x);
  }
  top = cur = sum = 0;
  memset(vis, 0, sizeof(vis));
  memset(dfn, 0, sizeof(dfn));
  memset(low, 0, sizeof(low));
  for (int i = 1; i <= num; i++)
    if (!dfn[i])
      tarjan(i);
  for (int i = 1; i <= n; i++)
    if (bel[i * 2 - 1] == bel[i * 2])
      return false;
  return true;
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read(), b[i] = read();
    d[i * 2 - 1].x = a[i];
    d[i * 2 - 1].rev = 0;
    d[i * 2].x = b[i];
    d[i * 2].rev = 1;
    d[i * 2 - 1].id = d[i * 2].id = i;
  }
  sort(d + 1, d + 2 * n + 1);
  for (int i = 1; i <= 2 * n; i++) {
    if (!d[i].rev)
      a[d[i].id] = i;
    else
      b[d[i].id] = i;
    c[i] = d[i].x;
  }
  tot = 2 * n;
  build(root, 1, 2 * n);
  num = tot;
  int l = 0, r = 1000000000, mid;
  while (l < r) {
    mid = (l + r + 1) / 2;
    if (check(mid))
      l = mid;
    else
      r = mid - 1;
  }
  printf("%d\n", l);
  return 0;
}