Problem p03802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define maxn 10005
#define ls x << 1
#define rs x << 1 | 1
#define mid (l + r >> 1)
using namespace std;
int read() {
  int rt = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9') {
    rt = rt * 10 + c - '0';
    c = getchar();
  }
  return rt;
}

struct Edge {
  int to, next;
  Edge() {}
  Edge(int to, int next) : to(to), next(next) {}
} ed[maxn * 40];
int head[maxn * 8], cnt;
void add(int a, int b) {
  ed[++cnt] = Edge(b, head[a]);
  head[a] = cnt;
}

struct node {
  int val, pos;
  node() {}
  node(int val, int pos) : val(val), pos(pos) {}
} nd[maxn * 2];
bool cmp(node a, node b) {
  if (a.val == b.val)
    return a.pos < b.pos;
  return a.val < b.val;
}

int tr[maxn * 8], tot;
void build(int l, int r, int x) {
  if (l == r) {
    tr[x] = nd[l].pos;
    return;
  }
  tr[x] = ++tot;
  build(l, mid, ls);
  build(mid + 1, r, rs);
  add(tr[x], tr[ls]);
  add(tr[x], tr[rs]);
}
void Add(int l, int r, int x, int L, int R, int u) {
  if (L <= l && r <= R) {
    add(u, tr[x]);
    return;
  }
  if (L <= mid)
    Add(l, mid, ls, L, R, u);
  if (R > mid)
    Add(mid + 1, r, rs, L, R, u);
}

int dfn[maxn * 8], low[maxn * 8], sta[maxn * 8], col[maxn * 8];
int num, dfs, top;
bool vis[maxn * 8];
void tarjan(int x) {
  vis[x] = 1;
  sta[++top] = x;
  dfn[x] = low[x] = ++dfs;
  for (int i = head[x]; i; i = ed[i].next) {
    int to = ed[i].to;
    if (!dfn[to]) {
      tarjan(to);
      low[x] = min(low[x], low[to]);
    } else if (vis[to])
      low[x] = min(low[x], dfn[to]);
  }
  if (low[x] == dfn[x]) {
    vis[x] = 0;
    col[x] = ++num;
    while (sta[top] != x) {
      col[sta[top]] = num;
      vis[sta[top--]] = 0;
    }
    top--;
  }
}

int n;
int val[maxn][2];
bool check(int x) {
  memset(head, 0, sizeof(head));
  memset(dfn, 0, sizeof(dfn));
  cnt = 0;
  tot = 2 * n;
  dfs = num = top = 0;
  build(1, 2 * n, 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 1; j++) {
      int l =
          lower_bound(nd + 1, nd + 1 + 2 * n, node(val[i][j] - x + 1, 0), cmp) -
          nd;
      int r = upper_bound(nd + 1, nd + 1 + n * 2,
                          node(val[i][j] + x - 1, 2 * n + 1), cmp) -
              nd - 1;
      int m = lower_bound(nd + 1, nd + 1 + n * 2,
                          node(val[i][j], i + (j ^ 1) * n), cmp) -
              nd;
      if (l < m)
        Add(1, 2 * n, 1, l, m - 1, i + j * n);
      if (r > m)
        Add(1, 2 * n, 1, m + 1, r, i + j * n);
    }
  }
  for (int i = 1; i <= tot; i++) {
    if (!dfn[i])
      tarjan(i);
  }
  for (int i = 1; i <= n; i++)
    if (col[i] == col[i + n])
      return 0;
  return 1;
}
int main() {
  n = read();
  int maxx = 0;
  for (int i = 1; i <= n; i++) {
    val[i][0] = nd[i * 2 - 1].val = read();
    nd[i * 2 - 1].pos = i + n; // 存反点
    val[i][1] = nd[i * 2].val = read();
    nd[i * 2].pos = i;
    maxx = max(nd[i * 2].val, max(maxx, nd[i * 2 - 1].val));
  }
  sort(nd + 1, nd + 1 + 2 * n, cmp);
  int l = 0, r = maxx, ans;
  while (l <= r) {
    if (check(mid)) {
      ans = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define maxn 20005
#define ls x << 1
#define rs x << 1 | 1
#define mid (l + r >> 1)
using namespace std;
int read() {
  int rt = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9') {
    rt = rt * 10 + c - '0';
    c = getchar();
  }
  return rt;
}

struct Edge {
  int to, next;
  Edge() {}
  Edge(int to, int next) : to(to), next(next) {}
} ed[maxn * 40];
int head[maxn * 8], cnt;
void add(int a, int b) {
  ed[++cnt] = Edge(b, head[a]);
  head[a] = cnt;
}

struct node {
  int val, pos;
  node() {}
  node(int val, int pos) : val(val), pos(pos) {}
} nd[maxn * 2];
bool cmp(node a, node b) {
  if (a.val == b.val)
    return a.pos < b.pos;
  return a.val < b.val;
}

int tr[maxn * 8], tot;
void build(int l, int r, int x) {
  if (l == r) {
    tr[x] = nd[l].pos;
    return;
  }
  tr[x] = ++tot;
  build(l, mid, ls);
  build(mid + 1, r, rs);
  add(tr[x], tr[ls]);
  add(tr[x], tr[rs]);
}
void Add(int l, int r, int x, int L, int R, int u) {
  if (L <= l && r <= R) {
    add(u, tr[x]);
    return;
  }
  if (L <= mid)
    Add(l, mid, ls, L, R, u);
  if (R > mid)
    Add(mid + 1, r, rs, L, R, u);
}

int dfn[maxn * 8], low[maxn * 8], sta[maxn * 8], col[maxn * 8];
int num, dfs, top;
bool vis[maxn * 8];
void tarjan(int x) {
  vis[x] = 1;
  sta[++top] = x;
  dfn[x] = low[x] = ++dfs;
  for (int i = head[x]; i; i = ed[i].next) {
    int to = ed[i].to;
    if (!dfn[to]) {
      tarjan(to);
      low[x] = min(low[x], low[to]);
    } else if (vis[to])
      low[x] = min(low[x], dfn[to]);
  }
  if (low[x] == dfn[x]) {
    vis[x] = 0;
    col[x] = ++num;
    while (sta[top] != x) {
      col[sta[top]] = num;
      vis[sta[top--]] = 0;
    }
    top--;
  }
}

int n;
int val[maxn][2];
bool check(int x) {
  memset(head, 0, sizeof(head));
  memset(dfn, 0, sizeof(dfn));
  cnt = 0;
  tot = 2 * n;
  dfs = num = top = 0;
  build(1, 2 * n, 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 1; j++) {
      int l =
          lower_bound(nd + 1, nd + 1 + 2 * n, node(val[i][j] - x + 1, 0), cmp) -
          nd;
      int r = upper_bound(nd + 1, nd + 1 + n * 2,
                          node(val[i][j] + x - 1, 2 * n + 1), cmp) -
              nd - 1;
      int m = lower_bound(nd + 1, nd + 1 + n * 2,
                          node(val[i][j], i + (j ^ 1) * n), cmp) -
              nd;
      if (l < m)
        Add(1, 2 * n, 1, l, m - 1, i + j * n);
      if (r > m)
        Add(1, 2 * n, 1, m + 1, r, i + j * n);
    }
  }
  for (int i = 1; i <= tot; i++) {
    if (!dfn[i])
      tarjan(i);
  }
  for (int i = 1; i <= n; i++)
    if (col[i] == col[i + n])
      return 0;
  return 1;
}
int main() {
  n = read();
  int maxx = 0;
  for (int i = 1; i <= n; i++) {
    val[i][0] = nd[i * 2 - 1].val = read();
    nd[i * 2 - 1].pos = i + n; // 存反点
    val[i][1] = nd[i * 2].val = read();
    nd[i * 2].pos = i;
    maxx = max(nd[i * 2].val, max(maxx, nd[i * 2 - 1].val));
  }
  sort(nd + 1, nd + 1 + 2 * n, cmp);
  int l = 0, r = maxx, ans;
  while (l <= r) {
    if (check(mid)) {
      ans = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  printf("%d\n", ans);
  return 0;
}