Problem p03802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, m, pp[10007][2], h[100007], svh[100007], to[300007], nxt[300007],
    pos[20007], cnt;
int dfn[100007], low[100007], col[100007], tot, st[100007], tp;
bool vis[100007];
pair<int, int> p[20007];
int read() {
  int num = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    num = num * 10 + c - '0', c = getchar();
  return num;
}
int add(int a, int b) {
  cnt++;
  to[cnt] = b;
  nxt[cnt] = h[a];
  h[a] = cnt;
  return 0;
}
int mtree(int w, int l, int r) {
  if (l == r) {
    add(w + 20000, pp[p[l].second][pp[p[l].second][0] == l]);
    return 0;
  }
  int mid = (l + r) / 2;
  mtree(2 * w, l, mid);
  mtree(2 * w + 1, mid + 1, r);
  add(w + 20000, 2 * w + 20000);
  add(w + 20000, 2 * w + 20001);
  return 0;
}
int tadd(int w, int l, int r, int x, int y, int v) {
  if (x <= l && r <= y) {
    add(v, w + 20000);
    return 0;
  }
  int mid = (l + r) / 2;
  if (x <= mid)
    tadd(2 * w, l, mid, x, y, v);
  if (y > mid)
    tadd(2 * w + 1, mid + 1, r, x, y, v);
  return 0;
}
int dfs(int w) {
  dfn[w] = low[w] = ++tot;
  st[++tp] = w;
  vis[w] = 1;
  for (int i = h[w]; i; i = nxt[i])
    if (!dfn[to[i]])
      dfs(to[i]), low[w] = min(low[w], low[to[i]]);
    else if (vis[to[i]])
      low[w] = min(low[w], dfn[to[i]]);
  if (low[w] == dfn[w]) {
    while (st[tp] != w) {
      vis[st[tp]] = 0;
      col[st[tp]] = w;
      tp--;
    }
    vis[w] = 0;
    col[w] = w;
    tp--;
  }
  return 0;
}
int check(int mid) {
  int sv = cnt;
  memcpy(h, svh, sizeof h);
  for (int i = 1; i <= m; i++) {
    int l = upper_bound(pos + 1, pos + m + 1, p[i].first - mid) - pos;
    int r = lower_bound(pos + 1, pos + m + 1, p[i].first + mid) - pos - 1;
    if (l != i)
      tadd(1, 1, m, l, i - 1, i);
    if (r != i)
      tadd(1, 1, m, i + 1, r, i);
  }
  memset(dfn, 0, sizeof dfn);
  memset(low, 0, sizeof low);
  for (int i = 1; i <= 100000; i++)
    if (!dfn[i])
      dfs(i);
  cnt = sv;
  for (int i = 1; i <= n; i++)
    if (col[pp[i][0]] == col[pp[i][1]])
      return 0;
  return 1;
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    p[++m].first = read();
    p[++m].first = read();
    p[m - 1].second = p[m].second = i;
  }
  sort(p + 1, p + m + 1);
  for (int i = 1; i <= m; i++)
    if (pp[p[i].second][0])
      pp[p[i].second][1] = i;
    else
      pp[p[i].second][0] = i;
  mtree(1, 1, m);
  memcpy(svh, h, sizeof svh);
  for (int i = 1; i <= m; i++)
    pos[i] = p[i].first;
  int l = 0, r = 1e9;
  while (l != r) {
    int mid = (l + r + 1) / 2;
    if (check(mid))
      l = mid;
    else
      r = mid - 1;
  }
  cout << l << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, m, pp[100007][2], h[1000007], svh[1000007], to[3000007], nxt[3000007],
    pos[200007], cnt;
int dfn[1000007], low[1000007], col[1000007], tot, st[1000007], tp;
bool vis[1000007];
pair<int, int> p[200007];
int read() {
  int num = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    num = num * 10 + c - '0', c = getchar();
  return num;
}
int add(int a, int b) {
  cnt++;
  to[cnt] = b;
  nxt[cnt] = h[a];
  h[a] = cnt;
  return 0;
}
int mtree(int w, int l, int r) {
  if (l == r) {
    add(w + 20000, pp[p[l].second][pp[p[l].second][0] == l]);
    return 0;
  }
  int mid = (l + r) / 2;
  mtree(2 * w, l, mid);
  mtree(2 * w + 1, mid + 1, r);
  add(w + 20000, 2 * w + 20000);
  add(w + 20000, 2 * w + 20001);
  return 0;
}
int tadd(int w, int l, int r, int x, int y, int v) {
  if (x <= l && r <= y) {
    add(v, w + 20000);
    return 0;
  }
  int mid = (l + r) / 2;
  if (x <= mid)
    tadd(2 * w, l, mid, x, y, v);
  if (y > mid)
    tadd(2 * w + 1, mid + 1, r, x, y, v);
  return 0;
}
int dfs(int w) {
  dfn[w] = low[w] = ++tot;
  st[++tp] = w;
  vis[w] = 1;
  for (int i = h[w]; i; i = nxt[i])
    if (!dfn[to[i]])
      dfs(to[i]), low[w] = min(low[w], low[to[i]]);
    else if (vis[to[i]])
      low[w] = min(low[w], dfn[to[i]]);
  if (low[w] == dfn[w]) {
    while (st[tp] != w) {
      vis[st[tp]] = 0;
      col[st[tp]] = w;
      tp--;
    }
    vis[w] = 0;
    col[w] = w;
    tp--;
  }
  return 0;
}
int check(int mid) {
  int sv = cnt;
  memcpy(h, svh, sizeof h);
  for (int i = 1; i <= m; i++) {
    int l = upper_bound(pos + 1, pos + m + 1, p[i].first - mid) - pos;
    int r = lower_bound(pos + 1, pos + m + 1, p[i].first + mid) - pos - 1;
    if (l != i)
      tadd(1, 1, m, l, i - 1, i);
    if (r != i)
      tadd(1, 1, m, i + 1, r, i);
  }
  memset(dfn, 0, sizeof dfn);
  memset(low, 0, sizeof low);
  for (int i = 1; i <= 100000; i++)
    if (!dfn[i])
      dfs(i);
  cnt = sv;
  for (int i = 1; i <= n; i++)
    if (col[pp[i][0]] == col[pp[i][1]])
      return 0;
  return 1;
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    p[++m].first = read();
    p[++m].first = read();
    p[m - 1].second = p[m].second = i;
  }
  sort(p + 1, p + m + 1);
  for (int i = 1; i <= m; i++)
    if (pp[p[i].second][0])
      pp[p[i].second][1] = i;
    else
      pp[p[i].second][0] = i;
  mtree(1, 1, m);
  memcpy(svh, h, sizeof svh);
  for (int i = 1; i <= m; i++)
    pos[i] = p[i].first;
  int l = 0, r = 1e9;
  while (l != r) {
    int mid = (l + r + 1) / 2;
    if (check(mid))
      l = mid;
    else
      r = mid - 1;
  }
  cout << l << endl;
  return 0;
}