Problem p03802 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
using namespace std;

int v[80005];
int n, x, y, tot = 0, h[20005];
int scc[20005], dfn[20005], low[20005], C, cnt;
bool vis[20005];
struct Edge {
  int x, next;
} e[5000000];
pair<int, int> a[20005];
stack<int> s;

inline void add_edge(int x, int y) {
  e[++tot].x = y;
  e[tot].next = h[x], h[x] = tot;
}

void tarjan(int x) {
  dfn[x] = low[x] = ++cnt;
  s.push(x), vis[x] = true;
  for (int i = h[x]; ~i; i = e[i].next) {
    if (!dfn[e[i].x]) {
      tarjan(e[i].x), low[x] = min(low[x], low[e[i].x]);
    } else if (vis[e[i].x]) {
      low[x] = min(low[x], dfn[e[i].x]);
    }
  }
  if (dfn[x] == low[x]) {
    C++;
    int tmp;
    for (;;) {
      tmp = s.top();
      vis[tmp] = false, scc[tmp] = C;
      s.pop();
      if (tmp == x)
        break;
    }
  }
}

void build(int o, int l, int r) {
  if (r - l == 1) {
    add_edge(o + n * 2, a[l].second ^ 1);
    return;
  }
  add_edge(o + n * 2, (o << 1) + n * 2);
  add_edge(o + n * 2, (o << 1 | 1) + n * 2);
  int mid = l + r >> 1;
  build(o << 1, l, mid), build(o << 1 | 1, mid, r);
}

void link(int o, int l, int r, int x, int y, int a) {
  if (l >= x && r <= y) {
    add_edge(a, o + n * 2);
    return;
  }
  int mid = l + r >> 1;
  if (x < mid)
    link(o << 1, l, mid, x, y, a);
  if (y > mid)
    link(o << 1 | 1, mid, r, x, y, a);
}

inline pair<int, int> get(int x, int len) {
  int l = 0, r = x;
  pair<int, int> ans;
  while (l < r) {
    int mid = l + r >> 1;
    if (a[x].first - a[mid].first < len)
      r = mid;
    else
      l = mid + 1;
  }
  ans.first = l;
  l = x, r = n * 2 - 1;
  while (l < r) {
    int mid = l + r + 1 >> 1;
    if (a[mid].first - a[x].first < len)
      l = mid;
    else
      r = mid - 1;
  }
  ans.second = l;
  return ans;
}

inline bool check(int d) {
  memset(scc, 0, sizeof(scc));
  memset(dfn, 0, sizeof(dfn));
  memset(low, 0, sizeof(low));
  memset(h, -1, sizeof(h)), tot = 0;
  build(1, 0, n * 2);
  for (int i = 0; i < n * 2; i++) {
    int id = a[i].second;
    pair<int, int> x = get(i, d);
    if (i < x.second)
      link(1, 0, n * 2, i + 1, x.second + 1, id);
    if (x.first < i)
      link(1, 0, n * 2, x.first, i, id);
  }
  C = cnt = 0;
  for (int i = 0; i < n * 2; i++)
    if (!dfn[i])
      tarjan(i);
  for (int i = 0; i < n * 2; i++)
    if (scc[a[i].second] == scc[a[i].second ^ 1])
      return false;
  return true;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &x, &y);
    a[2 * i + 1] = make_pair(x, 2 * i + 1);
    a[2 * i] = make_pair(y, 2 * i);
  }
  sort(a, a + n * 2);
  int l = 0, r = 1000000000;
  while (l < r) {
    int mid = l + r + 1 >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid - 1;
  }
  printf("%d", l);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
using namespace std;

int v[100005];
int n, x, y, tot = 0, h[100005];
int scc[100005], dfn[100005], low[100005], C, cnt;
bool vis[100005];
struct Edge {
  int x, next;
} e[5000000];
pair<int, int> a[20005];
stack<int> s;

inline void add_edge(int x, int y) {
  e[++tot].x = y;
  e[tot].next = h[x], h[x] = tot;
}

void tarjan(int x) {
  dfn[x] = low[x] = ++cnt;
  s.push(x), vis[x] = true;
  for (int i = h[x]; ~i; i = e[i].next) {
    if (!dfn[e[i].x]) {
      tarjan(e[i].x), low[x] = min(low[x], low[e[i].x]);
    } else if (vis[e[i].x]) {
      low[x] = min(low[x], dfn[e[i].x]);
    }
  }
  if (dfn[x] == low[x]) {
    C++;
    int tmp;
    for (;;) {
      tmp = s.top();
      vis[tmp] = false, scc[tmp] = C;
      s.pop();
      if (tmp == x)
        break;
    }
  }
}

void build(int o, int l, int r) {
  if (r - l == 1) {
    add_edge(o + n * 2, a[l].second ^ 1);
    return;
  }
  add_edge(o + n * 2, (o << 1) + n * 2);
  add_edge(o + n * 2, (o << 1 | 1) + n * 2);
  int mid = l + r >> 1;
  build(o << 1, l, mid), build(o << 1 | 1, mid, r);
}

void link(int o, int l, int r, int x, int y, int a) {
  if (l >= x && r <= y) {
    add_edge(a, o + n * 2);
    return;
  }
  int mid = l + r >> 1;
  if (x < mid)
    link(o << 1, l, mid, x, y, a);
  if (y > mid)
    link(o << 1 | 1, mid, r, x, y, a);
}

inline pair<int, int> get(int x, int len) {
  int l = 0, r = x;
  pair<int, int> ans;
  while (l < r) {
    int mid = l + r >> 1;
    if (a[x].first - a[mid].first < len)
      r = mid;
    else
      l = mid + 1;
  }
  ans.first = l;
  l = x, r = n * 2 - 1;
  while (l < r) {
    int mid = l + r + 1 >> 1;
    if (a[mid].first - a[x].first < len)
      l = mid;
    else
      r = mid - 1;
  }
  ans.second = l;
  return ans;
}

inline bool check(int d) {
  memset(scc, 0, sizeof(scc));
  memset(dfn, 0, sizeof(dfn));
  memset(low, 0, sizeof(low));
  memset(h, -1, sizeof(h)), tot = 0;
  build(1, 0, n * 2);
  for (int i = 0; i < n * 2; i++) {
    int id = a[i].second;
    pair<int, int> x = get(i, d);
    if (i < x.second)
      link(1, 0, n * 2, i + 1, x.second + 1, id);
    if (x.first < i)
      link(1, 0, n * 2, x.first, i, id);
  }
  C = cnt = 0;
  for (int i = 0; i < n * 2; i++)
    if (!dfn[i])
      tarjan(i);
  for (int i = 0; i < n * 2; i++)
    if (scc[a[i].second] == scc[a[i].second ^ 1])
      return false;
  return true;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &x, &y);
    a[2 * i + 1] = make_pair(x, 2 * i + 1);
    a[2 * i] = make_pair(y, 2 * i);
  }
  sort(a, a + n * 2);
  int l = 0, r = 1000000000;
  while (l < r) {
    int mid = l + r + 1 >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid - 1;
  }
  printf("%d", l);
}