Problem p03802 (C++)

Buggy submission (status: Runtime Error):
%:pragma GCC optimize(2)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

const int N = 10005 * 5;
const int M = N * 505;

int fir[N], ne[M], to[M], cnt = 1, block[N], n, x[N], y[N], tot, Size, whe[N],
                          mi[N], mx[N];
int dfn[N], low[N], scc_no[N], stk[N], top, dfs_clock, col, du, Lc[N], Rc[N],
    L[N], R[N], rt, all;

struct NODE {
  int id, wh, val;
  friend bool operator<(NODE xxx, NODE yyy) { return xxx.val < yyy.val; }
} P[N];

#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)

void add(int x, int y) {
  ne[++cnt] = fir[x];
  fir[x] = cnt;
  to[cnt] = y;
}

void link(int x, int y) {
  add(x, y);
  add(y ^ 1, x ^ 1);
}

int abs(int x) { return (x < 0) ? -x : x; }

int build(int l, int r) {
  L[++all] = l;
  R[all] = r;
  if (l == r) {
    link(lc(all), P[l].id ^ 1);
    return all;
  }
  int cur = all;
  int mid = (l + r) >> 1;
  Lc[cur] = build(l, mid);
  Rc[cur] = build(mid + 1, r);
  link(lc(cur), lc(Lc[cur]));
  link(lc(cur), lc(Rc[cur]));
  return cur;
}

void addtag(int no, int wh, int l, int r) {
  if (L[no] == l && R[no] == r) {
    link(wh, lc(no));
    return;
  }
  int mid = (L[no] + R[no]) >> 1;
  if (l > mid)
    addtag(Rc[no], wh, l, r);
  else if (r <= mid)
    addtag(Lc[no], wh, l, r);
  else {
    addtag(Lc[no], wh, l, mid);
    addtag(Rc[no], wh, mid + 1, r);
  }
}

void Build(int cur) {
  memset(fir, 0, sizeof(fir));
  cnt = 0;
  tot = du;
  all = n;
  rt = build(1, du);
  int l = 1, r = 1;
  for (int i = 1; i <= du; i++) {
    while (abs(P[l].val - P[i].val) >= cur && l < i)
      l++;
    while (r < du && abs(P[r + 1].val - P[i].val) < cur)
      r++;
    if (l <= i - 1)
      addtag(rt, P[i].id, l, i - 1);
    if (i + 1 <= r)
      addtag(rt, P[i].id, i + 1, r);
  }
}

#define Foreachson(i, x) for (int i = fir[x]; i; i = ne[i])
bool die = 0;

void dfs(int x) {
  if (die)
    return;
  low[x] = dfn[x] = ++dfs_clock;
  stk[++top] = x;
  Foreachson(i, x) {
    int V = to[i];
    if (dfn[V]) {
      if (!scc_no[V])
        low[x] = min(low[x], low[V]);
    } else {
      dfs(V);
      low[x] = min(low[x], low[V]);
    }
  }
  if (low[x] == dfn[x]) {
    col++;
    for (;;) {
      scc_no[stk[top--]] = col;
      if (scc_no[stk[top + 1]] == scc_no[stk[top + 1] ^ 1]) {
        die = 1;
        return;
      }
      if (stk[top + 1] == x)
        break;
    }
  }
}

bool SAT(void) {
  die = 0;
  memset(dfn, 0, sizeof(dfn));
  memset(low, 0, sizeof(low));
  memset(scc_no, 0, sizeof(scc_no));
  top = col = dfs_clock = 0;
  for (int i = 2; i <= rc(tot); i++) {
    if (!dfn[i])
      dfs(i);
    if (die)
      return 0;
  }
  for (int i = 1; i <= rc(tot); i++) {
    if (scc_no[lc(i)] == scc_no[rc(i)])
      return 0;
  }
  return 1;
}

bool pd(int x) {
  Build(x);
  return SAT();
}

int main(void) {
  int Max = 0;
  //	freopen("A.in" , "r" , stdin);
  //	freopen("A.out" , "w" , stdout);
  scanf("%d", &n);
  if (n == 1) {
    puts("0");
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &x[i], &y[i]);
    P[++tot].id = lc(i);
    P[tot].wh = i;
    P[tot].val = x[i];
    P[++tot].id = rc(i);
    P[tot].wh = i;
    P[tot].val = y[i];
    Max = max(Max, x[i]);
    Max = max(Max, y[i]);
  }
  du = tot;
  sort(P + 1, P + tot + 1);
  memset(mi, 127, sizeof(mi));
  int l = 0, r = Max * 2 / n, ans = 0;
  //	if(n <= 1000) r = 2e9;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (pd(mid)) {
      l = mid + 1;
      ans = mid;
    } else
      r = mid - 1;
  }
  printf("%d\n", ans);
}

Fixed submission:
%:pragma GCC optimize(2)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

const int N = 10005 * 10;
const int M = N * 505;

int fir[N], ne[M], to[M], cnt = 1, block[N], n, x[N], y[N], tot, Size, whe[N],
                          mi[N], mx[N];
int dfn[N], low[N], scc_no[N], stk[N], top, dfs_clock, col, du, Lc[N], Rc[N],
    L[N], R[N], rt, all;

struct NODE {
  int id, wh, val;
  friend bool operator<(NODE xxx, NODE yyy) { return xxx.val < yyy.val; }
} P[N];

#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)

void add(int x, int y) {
  ne[++cnt] = fir[x];
  fir[x] = cnt;
  to[cnt] = y;
}

void link(int x, int y) {
  add(x, y);
  add(y ^ 1, x ^ 1);
}

int abs(int x) { return (x < 0) ? -x : x; }

int build(int l, int r) {
  L[++all] = l;
  R[all] = r;
  if (l == r) {
    link(lc(all), P[l].id ^ 1);
    return all;
  }
  int cur = all;
  int mid = (l + r) >> 1;
  Lc[cur] = build(l, mid);
  Rc[cur] = build(mid + 1, r);
  link(lc(cur), lc(Lc[cur]));
  link(lc(cur), lc(Rc[cur]));
  return cur;
}

void addtag(int no, int wh, int l, int r) {
  if (L[no] == l && R[no] == r) {
    link(wh, lc(no));
    return;
  }
  int mid = (L[no] + R[no]) >> 1;
  if (l > mid)
    addtag(Rc[no], wh, l, r);
  else if (r <= mid)
    addtag(Lc[no], wh, l, r);
  else {
    addtag(Lc[no], wh, l, mid);
    addtag(Rc[no], wh, mid + 1, r);
  }
}

void Build(int cur) {
  memset(fir, 0, sizeof(fir));
  cnt = 0;
  tot = du;
  all = n;
  rt = build(1, du);
  int l = 1, r = 1;
  for (int i = 1; i <= du; i++) {
    while (abs(P[l].val - P[i].val) >= cur && l < i)
      l++;
    while (r < du && abs(P[r + 1].val - P[i].val) < cur)
      r++;
    if (l <= i - 1)
      addtag(rt, P[i].id, l, i - 1);
    if (i + 1 <= r)
      addtag(rt, P[i].id, i + 1, r);
  }
}

#define Foreachson(i, x) for (int i = fir[x]; i; i = ne[i])
bool die = 0;

void dfs(int x) {
  if (die)
    return;
  low[x] = dfn[x] = ++dfs_clock;
  stk[++top] = x;
  Foreachson(i, x) {
    int V = to[i];
    if (dfn[V]) {
      if (!scc_no[V])
        low[x] = min(low[x], low[V]);
    } else {
      dfs(V);
      low[x] = min(low[x], low[V]);
    }
  }
  if (low[x] == dfn[x]) {
    col++;
    for (;;) {
      scc_no[stk[top--]] = col;
      if (scc_no[stk[top + 1]] == scc_no[stk[top + 1] ^ 1]) {
        die = 1;
        return;
      }
      if (stk[top + 1] == x)
        break;
    }
  }
}

bool SAT(void) {
  die = 0;
  memset(dfn, 0, sizeof(dfn));
  memset(low, 0, sizeof(low));
  memset(scc_no, 0, sizeof(scc_no));
  top = col = dfs_clock = 0;
  for (int i = 2; i <= rc(tot); i++) {
    if (!dfn[i])
      dfs(i);
    if (die)
      return 0;
  }
  for (int i = 1; i <= rc(tot); i++) {
    if (scc_no[lc(i)] == scc_no[rc(i)])
      return 0;
  }
  return 1;
}

bool pd(int x) {
  Build(x);
  return SAT();
}

int main(void) {
  int Max = 0;
  //	freopen("A.in" , "r" , stdin);
  //	freopen("A.out" , "w" , stdout);
  scanf("%d", &n);
  if (n == 1) {
    puts("0");
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &x[i], &y[i]);
    P[++tot].id = lc(i);
    P[tot].wh = i;
    P[tot].val = x[i];
    P[++tot].id = rc(i);
    P[tot].wh = i;
    P[tot].val = y[i];
    Max = max(Max, x[i]);
    Max = max(Max, y[i]);
  }
  du = tot;
  sort(P + 1, P + tot + 1);
  memset(mi, 127, sizeof(mi));
  int l = 0, r = Max * 2 / n, ans = 0;
  //	if(n <= 1000) r = 2e9;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (pd(mid)) {
      l = mid + 1;
      ans = mid;
    } else
      r = mid - 1;
  }
  printf("%d\n", ans);
}