Problem p03802 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define For(i, j, k) for (int i = j; i <= k; ++i)
#define Forr(i, j, k) for (int i = j; i >= k; --i)
#define ll long long
using namespace std;
const int N = 10010, INF = 0x3f3f3f3f;
template <class T> void read(T &x) {
  x = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c))
    x = x * 10 + c - 48, c = getchar();
}
int n, X[N << 1], K, Lim, id[N << 1], sz[N << 2], m, Low;
vector<int> G[N << 2];
inline void add(int x, int y) { G[y].push_back(x); }
inline void Add(int x, int y) { add(x, y), add(y, x); }
inline void link(int x, int y) { Add(x * 2 + 1, y * 2), Add(y * 2 + 1, x * 2); }
inline void Link(int x, int y) { add(x * 2 + 1, y * 2), add(y * 2 + 1, x * 2); }
inline bool cmp(int a, int b) { return X[a] < X[b]; }
int pre[N << 2], sccno[N << 2], scc[N << 2], scc_cnt, low[N << 2], dfs_cnt;
stack<int> s;
void dfs(int u) {
  low[u] = pre[u] = ++dfs_cnt;
  sccno[u] = 1;
  s.push(u);
  int S = G[u].size();
  For(i, 0, S - 1) {
    int v = G[u][i];
    if (!pre[v])
      dfs(v), low[u] = min(low[u], low[v]);
    else if (sccno[v])
      low[u] = min(low[u], pre[v]);
  }
  if (pre[u] == low[u]) {
    ++scc_cnt;
    int w;
    do {
      w = s.top();
      s.pop();
      scc[w] = scc_cnt;
      sccno[w] = 0;
    } while (w != u);
  }
}
bool check(int dis) {
  For(i, 0, m) G[i].resize(sz[i]);
  For(i, 0, 2 * n - 1) {
    For(j, i + 1, 2 * n - 1) {
      if (X[id[j]] - X[id[i]] >= dis)
        break;
      if (abs(id[i] - id[j]) == n)
        continue;
      Link(id[i], id[j]);
    }
  }
  dfs_cnt = 0;
  scc_cnt = 0;
  For(i, 0, m) pre[i] = 0, scc[i] = 0;
  For(i, 0, m) if (!pre[i]) dfs(i);
  For(i, 0, 2 * n - 1) if (scc[i * 2] == scc[i * 2 + 1]) return 0;
  return 1;
}
inline bool Check(int dis) {
  int cnt = 0, last = -INF;
  For(i, 0, 2 * n - 1) {
    if (X[id[i]] - last >= dis)
      cnt++, last = X[id[i]];
    if (cnt == n)
      break;
  }
  return cnt == n;
}
void solveLim() {
  int L = 0, R = Lim, ans = 0;
  while (L <= R) {
    int Mid = (L + R) >> 1;
    if (Check(Mid))
      ans = Mid, L = Mid + 1;
    else
      R = Mid - 1;
  }
  Lim = ans;
}
int main() {
  read(n);
  Low = INF;
  For(i, 0, n - 1) {
    read(X[i]), read(X[i + n]);
    link(i, i + n);
    Lim = max(Lim, max(X[i], X[i + n]));
    if (i)
      Low = min(Low, X[i] - X[i - 1]);
  }
  m = 4 * n - 1;
  For(i, 0, 2 * n - 1) id[i] = i;
  For(i, 0, m) sz[i] = G[i].size();
  sort(id, id + 2 * n, cmp);
  solveLim();
  int L = Low, R = Lim, ans = 0;
  while (L <= R) {
    int Mid = (L + R) >> 1;
    if (check(Mid))
      L = Mid + 1, ans = Mid;
    else
      R = Mid - 1;
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define For(i, j, k) for (int i = j; i <= k; ++i)
#define Forr(i, j, k) for (int i = j; i >= k; --i)
#define ll long long
using namespace std;
const int N = 10010, INF = 0x3f3f3f3f;
template <class T> void read(T &x) {
  x = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c))
    x = x * 10 + c - 48, c = getchar();
}
int n, X[N << 1], K, Lim, id[N << 1], sz[N << 2], m, Low;
vector<int> G[N << 2];
inline void add(int x, int y) { G[y].push_back(x); }
inline void Add(int x, int y) { add(x, y), add(y, x); }
inline void link(int x, int y) { Add(x * 2 + 1, y * 2), Add(y * 2 + 1, x * 2); }
inline void Link(int x, int y) { add(x * 2 + 1, y * 2), add(y * 2 + 1, x * 2); }
inline bool cmp(int a, int b) { return X[a] < X[b]; }
int pre[N << 2], sccno[N << 2], scc[N << 2], scc_cnt, low[N << 2], dfs_cnt;
stack<int> s;
void dfs(int u) {
  low[u] = pre[u] = ++dfs_cnt;
  sccno[u] = 1;
  s.push(u);
  int S = G[u].size();
  For(i, 0, S - 1) {
    int v = G[u][i];
    if (!pre[v])
      dfs(v), low[u] = min(low[u], low[v]);
    else if (sccno[v])
      low[u] = min(low[u], pre[v]);
  }
  if (pre[u] == low[u]) {
    ++scc_cnt;
    int w;
    do {
      w = s.top();
      s.pop();
      scc[w] = scc_cnt;
      sccno[w] = 0;
    } while (w != u);
  }
}
bool check(int dis) {
  For(i, 0, m) G[i].resize(sz[i]);
  For(i, 0, 2 * n - 1) {
    For(j, max(i - 10, 0), min(i + 10, 2 * n - 1)) {
      if (j == i)
        continue;
      if (abs(X[id[j]] - X[id[i]]) >= dis)
        continue;
      if (abs(id[i] - id[j]) == n)
        continue;
      Link(id[i], id[j]);
    }
  }
  dfs_cnt = 0;
  scc_cnt = 0;
  For(i, 0, m) pre[i] = 0, scc[i] = 0;
  For(i, 0, m) if (!pre[i]) dfs(i);
  For(i, 0, 2 * n - 1) if (scc[i * 2] == scc[i * 2 + 1]) return 0;
  return 1;
}
inline bool Check(int dis) {
  int cnt = 0, last = -INF;
  For(i, 0, 2 * n - 1) {
    if (X[id[i]] - last >= dis)
      cnt++, last = X[id[i]];
    if (cnt == n)
      break;
  }
  return cnt == n;
}
void solveLim() {
  int L = 0, R = Lim, ans = 0;
  while (L <= R) {
    int Mid = (L + R) >> 1;
    if (Check(Mid))
      ans = Mid, L = Mid + 1;
    else
      R = Mid - 1;
  }
  Lim = ans;
}
int main() {
  read(n);
  Low = INF;
  For(i, 0, n - 1) {
    read(X[i]), read(X[i + n]);
    link(i, i + n);
    Lim = max(Lim, max(X[i], X[i + n]));
    if (i)
      Low = min(Low, X[i] - X[i - 1]);
  }
  m = 4 * n - 1;
  For(i, 0, 2 * n - 1) id[i] = i;
  For(i, 0, m) sz[i] = G[i].size();
  sort(id, id + 2 * n, cmp);
  solveLim();
  int L = Low, R = Lim, ans = 0;
  while (L <= R) {
    int Mid = (L + R) >> 1;
    if (check(Mid))
      L = Mid + 1, ans = Mid;
    else
      R = Mid - 1;
  }
  printf("%d\n", ans);
  return 0;
}