Problem p03802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;

const int N = 40005, M = 800005;

int n, m, op[N], vi[N];
pi b[N];

vector<int> e[N];
int nc, dfc, bcc, dfn[N], low[N], qu[N], bel[N], tp;
bool in[N];
int id[N << 1];

void build(int x, int l, int r) {
  id[x] = ++nc;
  if (l == r) {
    e[id[x]].pb(op[l]);
    return;
  }
  int ls = x << 1, rs = ls | 1, mid = (l + r) >> 1;
  build(ls, l, mid);
  e[id[x]].pb(id[ls]);
  build(rs, mid + 1, r);
  e[id[x]].pb(id[rs]);
}
void link(int x, int l, int r, int s, int t, int u) {
  if (l == s && r == t) {
    e[u].pb(id[x]);
    return;
  }
  int ls = x << 1, rs = ls | 1, mid = (l + r) >> 1;
  if (t <= mid)
    link(ls, l, mid, s, t, u);
  else if (s > mid)
    link(rs, mid + 1, r, s, t, u);
  else
    link(ls, l, mid, s, mid, u), link(rs, mid + 1, r, mid + 1, t, u);
}
void tarjan(int x) {
  dfn[x] = low[x] = ++dfc, qu[++tp] = x, in[x] = true;
  for (int v : e[x]) {
    if (!dfn[v])
      tarjan(v), low[x] = min(low[x], low[v]);
    else if (in[v])
      low[x] = min(low[x], dfn[v]);
  }
  if (low[x] == dfn[x]) {
    int v;
    ++bcc;
    do
      bel[v = qu[tp]] = bcc, in[v] = false, qu[tp--] = 0;
    while (v != x);
  }
}
bool chk(int x) {
  while (nc)
    dfn[nc] = low[nc] = bel[nc] = 0, e[nc].clear(), nc--;
  nc = m;
  dfc = bcc = 0;
  build(1, 1, m);
  int l = 1, r = 1;
  for (int i = 1; i <= m; i++) {
    while (b[l].fi <= b[i].fi - x && l <= m)
      l++;
    while (b[r].fi < b[i].fi + x && r <= m)
      r++;
    if (l < i)
      link(1, 1, m, l, i - 1, i);
    if (i < r - 1)
      link(1, 1, m, i + 1, r - 1, i);
  }
  for (int i = 1; i <= nc; i++)
    if (!dfn[i])
      tarjan(i);
  for (int i = 1; i <= m; i++)
    if (bel[i] == bel[op[i]])
      return false;
  return true;
}

int main() {
  scanf("%d", &n);
  for (int i = 1, x; i <= n; i++)
    for (int j : {0, 1})
      scanf("%d", &x), b[++m] = {x, i};
  sort(b + 1, b + m + 1);
  for (int i = 1, j; i <= m; i++)
    if (!vi[j = b[i].se])
      vi[j] = i;
    else
      op[vi[j]] = i, op[i] = vi[j];
  int l = 0, r = (b[m].fi - b[1].fi) / (n - 1), mid = (l + r + 1) >> 1;
  while (l < r) {
    if (chk(mid))
      l = mid;
    else
      r = mid - 1;
    mid = (l + r + 1) >> 1;
  }
  printf("%d\n", mid);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;

const int N = 60005;

int n, m, op[N], vi[N];
pi b[N];

vector<int> e[N];
int nc, dfc, bcc, dfn[N], low[N], qu[N], bel[N], tp;
bool in[N];
int id[N << 1];

void build(int x, int l, int r) {
  id[x] = ++nc;
  if (l == r) {
    e[id[x]].pb(op[l]);
    return;
  }
  int ls = x << 1, rs = ls | 1, mid = (l + r) >> 1;
  build(ls, l, mid);
  e[id[x]].pb(id[ls]);
  build(rs, mid + 1, r);
  e[id[x]].pb(id[rs]);
}
void link(int x, int l, int r, int s, int t, int u) {
  if (l == s && r == t) {
    e[u].pb(id[x]);
    return;
  }
  int ls = x << 1, rs = ls | 1, mid = (l + r) >> 1;
  if (t <= mid)
    link(ls, l, mid, s, t, u);
  else if (s > mid)
    link(rs, mid + 1, r, s, t, u);
  else
    link(ls, l, mid, s, mid, u), link(rs, mid + 1, r, mid + 1, t, u);
}
void tarjan(int x) {
  dfn[x] = low[x] = ++dfc, qu[++tp] = x, in[x] = true;
  for (int v : e[x]) {
    if (!dfn[v])
      tarjan(v), low[x] = min(low[x], low[v]);
    else if (in[v])
      low[x] = min(low[x], dfn[v]);
  }
  if (low[x] == dfn[x]) {
    int v;
    ++bcc;
    do
      bel[v = qu[tp]] = bcc, in[v] = false, qu[tp--] = 0;
    while (v != x);
  }
}
bool chk(int x) {
  while (nc)
    dfn[nc] = low[nc] = bel[nc] = 0, e[nc].clear(), nc--;
  nc = m;
  dfc = bcc = 0;
  build(1, 1, m);
  int l = 1, r = 1;
  for (int i = 1; i <= m; i++) {
    while (b[l].fi <= b[i].fi - x && l <= m)
      l++;
    while (b[r].fi < b[i].fi + x && r <= m)
      r++;
    if (l < i)
      link(1, 1, m, l, i - 1, i);
    if (i < r - 1)
      link(1, 1, m, i + 1, r - 1, i);
  }
  for (int i = 1; i <= nc; i++)
    if (!dfn[i])
      tarjan(i);
  for (int i = 1; i <= m; i++)
    if (bel[i] == bel[op[i]])
      return false;
  return true;
}

int main() {
  scanf("%d", &n);
  for (int i = 1, x; i <= n; i++)
    for (int j : {0, 1})
      scanf("%d", &x), b[++m] = {x, i};
  sort(b + 1, b + m + 1);
  for (int i = 1, j; i <= m; i++)
    if (!vi[j = b[i].se])
      vi[j] = i;
    else
      op[vi[j]] = i, op[i] = vi[j];
  int l = 0, r = (b[m].fi - b[1].fi) / (n - 1), mid = (l + r + 1) >> 1;
  while (l < r) {
    if (chk(mid))
      l = mid;
    else
      r = mid - 1;
    mid = (l + r + 1) >> 1;
  }
  printf("%d\n", mid);
  return 0;
}