Problem p03802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 40009
#define M 10000009
using namespace std;

int n, m, tp, cnt, trtot, tot, dfsclk, fst[N], pnt[M], nxt[M];
int q[N], b[N][2], p[N][2], pos[N], low[N], scc[N], id[N];
struct node {
  int x, y;
} a[N];
bool operator<(node u, node v) { return u.x < v.x || u.x == v.x && u.y < v.y; }
bool operator<=(node u, node v) {
  return u.x < v.x || u.x == v.x && u.y <= v.y;
}
void dfs(int x) {
  int i, y;
  low[x] = pos[x] = ++dfsclk;
  q[++tp] = x;
  for (i = fst[x]; i; i = nxt[i]) {
    y = pnt[i];
    if (!pos[y]) {
      dfs(y);
      low[x] = min(low[x], low[y]);
    } else if (!scc[y])
      low[x] = min(low[x], pos[y]);
  }
  if (pos[x] == low[x])
    for (cnt++;;) {
      scc[q[tp]] = cnt;
      if (q[tp--] == x)
        break;
    }
}
void add(int x, int y) {
  pnt[++tot] = y;
  nxt[tot] = fst[x];
  fst[x] = tot;
}
void ins(int x, int y) {
  add(x, y);
  add(y ^ 1, x ^ 1);
}
void build(int k, int l, int r) {
  p[k][0] = ++trtot;
  p[k][1] = ++trtot;
  if (l == r) {
    id[l] = k;
    return;
  }
  int mid = l + r >> 1;
  build(k << 1, l, mid);
  build(k << 1 | 1, mid + 1, r);
  ins(p[k][1], p[k << 1][1]);
  ins(p[k][1], p[k << 1 | 1][1]);
}
void ins(int k, int l, int r, node x, node y, int z) {
  if (y < x || y < a[l] || a[r] < x)
    return;
  if (x <= a[l] && a[r] <= y) {
    ins(p[z][0], p[k][1]);
    return;
  }
  int mid = l + r >> 1;
  ins(k << 1, l, mid, x, y, z);
  ins(k << 1 | 1, mid + 1, r, x, y, z);
}
bool check(int t) {
  tot = 0;
  memset(fst, 0, sizeof(fst));
  trtot = 1;
  build(1, 1, m);
  int i, x, y;
  for (i = 1; i <= m; i++) {
    ins(1, 1, m, (node){a[i].x - t + 1, -m}, (node){a[i].x, a[i].y - 1}, id[i]);
    ins(1, 1, m, (node){a[i].x, a[i].y + 1}, (node){a[i].x + t - 1, m}, id[i]);
  }
  for (i = 1; i <= n; i++) {
    x = id[b[i][0]];
    y = id[b[i][1]];
    ins(p[x][0], p[y][1]);
    ins(p[x][1], p[y][0]);
  }
  memset(pos, 0, sizeof(pos));
  memset(scc, 0, sizeof(scc));
  tp = cnt = dfsclk = 0;
  for (i = 2; i <= trtot; i++)
    if (!pos[i])
      dfs(i);
  for (i = 2; i <= trtot; i++)
    if (scc[i] == scc[i ^ 1])
      return 0;
  return 1;
}
int main() {
  scanf("%d", &n);
  int i, x, y;
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &x, &y);
    if (x > y)
      swap(x, y);
    a[++m] = (node){x, i};
    a[++m] = (node){y, i + n};
  }
  sort(a + 1, a + m + 1);
  for (i = 1; i <= m; i++)
    if (a[i].y > n)
      b[a[i].y - n][1] = i;
    else
      b[a[i].y][0] = i;
  int l = 0, r = 1000000000, mid;
  while (l < r) {
    mid = l + r + 1 >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid - 1;
  }
  printf("%d\n", l);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define N 100009
#define M 20000009
using namespace std;

int n, m, tp, cnt, trtot, tot, dfsclk, fst[N], pnt[M], nxt[M];
int q[N], b[N][2], p[N][2], pos[N], low[N], scc[N], id[N];
struct node {
  int x, y;
} a[N];
bool operator<(node u, node v) { return u.x < v.x || u.x == v.x && u.y < v.y; }
bool operator<=(node u, node v) {
  return u.x < v.x || u.x == v.x && u.y <= v.y;
}
void dfs(int x) {
  int i, y;
  low[x] = pos[x] = ++dfsclk;
  q[++tp] = x;
  for (i = fst[x]; i; i = nxt[i]) {
    y = pnt[i];
    if (!pos[y]) {
      dfs(y);
      low[x] = min(low[x], low[y]);
    } else if (!scc[y])
      low[x] = min(low[x], pos[y]);
  }
  if (pos[x] == low[x])
    for (cnt++;;) {
      scc[q[tp]] = cnt;
      if (q[tp--] == x)
        break;
    }
}
void add(int x, int y) {
  pnt[++tot] = y;
  nxt[tot] = fst[x];
  fst[x] = tot;
}
void ins(int x, int y) {
  add(x, y);
  add(y ^ 1, x ^ 1);
}
void build(int k, int l, int r) {
  p[k][0] = ++trtot;
  p[k][1] = ++trtot;
  if (l == r) {
    id[l] = k;
    return;
  }
  int mid = l + r >> 1;
  build(k << 1, l, mid);
  build(k << 1 | 1, mid + 1, r);
  ins(p[k][1], p[k << 1][1]);
  ins(p[k][1], p[k << 1 | 1][1]);
}
void ins(int k, int l, int r, node x, node y, int z) {
  if (y < x || y < a[l] || a[r] < x)
    return;
  if (x <= a[l] && a[r] <= y) {
    ins(p[z][0], p[k][1]);
    return;
  }
  int mid = l + r >> 1;
  ins(k << 1, l, mid, x, y, z);
  ins(k << 1 | 1, mid + 1, r, x, y, z);
}
bool check(int t) {
  tot = 0;
  memset(fst, 0, sizeof(fst));
  trtot = 1;
  build(1, 1, m);
  int i, x, y;
  for (i = 1; i <= m; i++) {
    ins(1, 1, m, (node){a[i].x - t + 1, -m}, (node){a[i].x, a[i].y - 1}, id[i]);
    ins(1, 1, m, (node){a[i].x, a[i].y + 1}, (node){a[i].x + t - 1, m}, id[i]);
  }
  for (i = 1; i <= n; i++) {
    x = id[b[i][0]];
    y = id[b[i][1]];
    ins(p[x][0], p[y][1]);
    ins(p[x][1], p[y][0]);
  }
  memset(pos, 0, sizeof(pos));
  memset(scc, 0, sizeof(scc));
  tp = cnt = dfsclk = 0;
  for (i = 2; i <= trtot; i++)
    if (!pos[i])
      dfs(i);
  for (i = 2; i <= trtot; i++)
    if (scc[i] == scc[i ^ 1])
      return 0;
  return 1;
}
int main() {
  scanf("%d", &n);
  int i, x, y;
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &x, &y);
    if (x > y)
      swap(x, y);
    a[++m] = (node){x, i};
    a[++m] = (node){y, i + n};
  }
  sort(a + 1, a + m + 1);
  for (i = 1; i <= m; i++)
    if (a[i].y > n)
      b[a[i].y - n][1] = i;
    else
      b[a[i].y][0] = i;
  int l = 0, r = 1000000000, mid;
  while (l < r) {
    mid = l + r + 1 >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid - 1;
  }
  printf("%d\n", l);
  return 0;
}