Problem p03802 (C++)

Buggy submission (status: Runtime Error):
// think twice code once
// Start:[2017-02-19 09:07:31]
// End  :[]
/*{{{*/
#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x), _ = (y); i <= _; ++i)
#define per(i, x, y) for (int i = (x), _ = (y); i >= _; --i)
#define dprintf(...) fprintf(stderr, __VA_ARGS__)
#define disp(x) cout << #x << " = " << x << "; "
#ifdef __linux__
#define getchar getchar_unlocked
#define LLFORMAT "%lld"
#else
#define LLFORMAT "%I64d"
#endif

typedef long long LL;

template <class T> bool chkmin(T &a, T b) {
  return a > b ? a = b, true : false;
}
template <class T> bool chkmax(T &a, T b) {
  return a < b ? a = b, true : false;
}

template <class T> void read(T &a) {
  char c = getchar();
  T f = 1;
  a = 0;
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    a = a * 10 + c - '0';
  a *= f;
}
/*}}}*/

const int maxN = int(1e4 + 5) << 3, oo = 0x3f3f3f3f;

typedef pair<int, int> pii;

#define x first
#define y second
#define mp make_pair

struct Edge {
  int v, next;
} G[maxN << 1];

int N;
int totnode;
int st[maxN], e;
int dfn[maxN], low[maxN], stk[maxN], sccid[maxN], dfs_clock, tp, scc_cnt;
pii a[maxN];

void addedge(int u, int v) {
  //	printf(" addedge "); disp(u), disp(v), puts("");
  G[++e] = (Edge){v, st[u]};
  st[u] = e;
}

namespace seg {
#define mid ((l + r) >> 1)
#define LC (now << 1)
#define RC (LC | 1)

void build(int now, int l, int r) {
  if (l == r) {
    addedge(now + (N << 2), a[l].y + (N << 1));
    return;
  }
  ++totnode;
  addedge(now + (N << 2), LC + (N << 2));
  addedge(now + (N << 2), RC + (N << 2));
  build(LC, l, mid), build(RC, mid + 1, r);
}

void add(int now, int l, int r, int ql, int qr, int x) {
  //		dprintf("ql = %d qr = %d\n", ql, qr);
  if (ql <= l && qr >= r) {
    addedge(x, now + (N << 2));
    return;
  }
  if (ql <= mid)
    add(LC, l, mid, ql, qr, x);
  if (qr > mid)
    add(RC, mid + 1, r, ql, qr, x);
}

#undef mid
#undef LC
#undef RC
} // namespace seg

void dfs(int u) {
  dfn[u] = low[u] = ++dfs_clock;
  stk[++tp] = u;

  for (int e = st[u]; e; e = G[e].next) {
    int v = G[e].v;
    if (dfn[v] == 0) {
      dfs(v);
      chkmin(low[u], low[v]);
    } else if (sccid[v] == 0)
      chkmin(low[u], dfn[v]);
  }

  if (low[u] == dfn[u]) {
    for (++scc_cnt;;) {
      int x = stk[tp--];
      sccid[x] = scc_cnt;
      if (x == u)
        break;
    }
  }
}

bool tarjan() {
  memset(dfn, 0, sizeof(dfn));
  memset(low, 0, sizeof(low));
  memset(sccid, 0, sizeof(sccid));
  scc_cnt = dfs_clock = tp = 0;

  rep(i, 1, totnode + (N << 2)) if (dfn[i] == 0) dfs(i);

  rep(i, 1, (N << 1)) if (sccid[i] == sccid[((i - 1) ^ 1) + 1]) return 0;
  return 1;
}

bool check(int d) {
  memset(st, 0, sizeof(st));
  totnode = e = 0;
  seg::build(1, 1, (N << 1));
  rep(i, 1, (N << 1)) {
    //		disp(i), disp(((i - 1) ^ 1) + 1), puts("");
    addedge(i + (N << 1), ((i - 1) ^ 1) + 1);
    addedge(((i - 1) ^ 1) + 1, i + (N << 1));
  }

  //	rep(i, 1, (N << 1)) disp(a[i].x), puts("");

  rep(i, 1, (N << 1)) {
    //		printf("while i = %d ------------------\n", i);
    int rb =
        upper_bound(a + 1, a + (N << 1) + 1, mp(a[i].x + d - 1, +oo)) - a - 1;
    int lb = upper_bound(a + 1, a + (N << 1) + 1, mp(a[i].x - d, +oo)) - a;
    //		disp(a[i].y), disp(lb), disp(rb), puts("");
    if (lb < i) {
      seg::add(1, 1, (N << 1), lb, i - 1, a[i].y);
      //			printf("%d -- > [%d,%d]\n", i, lb, i - 1);
    }
    if (rb > i && rb <= (N << 1)) {
      seg::add(1, 1, (N << 1), i + 1, rb, a[i].y);
      //			printf("%d -- > [%d,%d]\n", i, i + 1, rb);
    }
  }

  return tarjan();
}

int main() {
#ifdef Leeson
  freopen("tmp.in", "r", stdin);
  freopen("tmp.out", "w", stdout);
#endif

  read(N);
  rep(i, 1, N) {
    int x, y;
    read(x), read(y);
    a[(i << 1) - 1] = mp(x, (i << 1) - 1);
    a[(i << 1)] = mp(y, (i << 1));
  }

  sort(a + 1, a + (N << 1) + 1);

  //	disp(check(1));

  int l = -1, r = 1e9 + 1;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid;
  }

  cout << l << endl;

  return 0;
}


Fixed submission:
// think twice code once
// Start:[2017-02-19 09:07:31]
// End  :[]
/*{{{*/
#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x), _ = (y); i <= _; ++i)
#define per(i, x, y) for (int i = (x), _ = (y); i >= _; --i)
#define dprintf(...) fprintf(stderr, __VA_ARGS__)
#define disp(x) cout << #x << " = " << x << "; "
#ifdef __linux__
#define getchar getchar_unlocked
#define LLFORMAT "%lld"
#else
#define LLFORMAT "%I64d"
#endif

typedef long long LL;

template <class T> bool chkmin(T &a, T b) {
  return a > b ? a = b, true : false;
}
template <class T> bool chkmax(T &a, T b) {
  return a < b ? a = b, true : false;
}

template <class T> void read(T &a) {
  char c = getchar();
  T f = 1;
  a = 0;
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    a = a * 10 + c - '0';
  a *= f;
}
/*}}}*/

const int maxN = int(1e5 + 5) << 3, oo = 0x3f3f3f3f;

typedef pair<int, int> pii;

#define x first
#define y second
#define mp make_pair

struct Edge {
  int v, next;
} G[maxN << 1];

int N;
int totnode;
int st[maxN], e;
int dfn[maxN], low[maxN], stk[maxN], sccid[maxN], dfs_clock, tp, scc_cnt;
pii a[maxN];

void addedge(int u, int v) {
  //	printf(" addedge "); disp(u), disp(v), puts("");
  G[++e] = (Edge){v, st[u]};
  st[u] = e;
}

namespace seg {
#define mid ((l + r) >> 1)
#define LC (now << 1)
#define RC (LC | 1)

void build(int now, int l, int r) {
  if (l == r) {
    addedge(now + (N << 2), a[l].y + (N << 1));
    return;
  }
  ++totnode;
  addedge(now + (N << 2), LC + (N << 2));
  addedge(now + (N << 2), RC + (N << 2));
  build(LC, l, mid), build(RC, mid + 1, r);
}

void add(int now, int l, int r, int ql, int qr, int x) {
  //		dprintf("ql = %d qr = %d\n", ql, qr);
  if (ql <= l && qr >= r) {
    addedge(x, now + (N << 2));
    return;
  }
  if (ql <= mid)
    add(LC, l, mid, ql, qr, x);
  if (qr > mid)
    add(RC, mid + 1, r, ql, qr, x);
}

#undef mid
#undef LC
#undef RC
} // namespace seg

void dfs(int u) {
  dfn[u] = low[u] = ++dfs_clock;
  stk[++tp] = u;

  for (int e = st[u]; e; e = G[e].next) {
    int v = G[e].v;
    if (dfn[v] == 0) {
      dfs(v);
      chkmin(low[u], low[v]);
    } else if (sccid[v] == 0)
      chkmin(low[u], dfn[v]);
  }

  if (low[u] == dfn[u]) {
    for (++scc_cnt;;) {
      int x = stk[tp--];
      sccid[x] = scc_cnt;
      if (x == u)
        break;
    }
  }
}

bool tarjan() {
  memset(dfn, 0, sizeof(dfn));
  memset(low, 0, sizeof(low));
  memset(sccid, 0, sizeof(sccid));
  scc_cnt = dfs_clock = tp = 0;

  rep(i, 1, totnode + (N << 2)) if (dfn[i] == 0) dfs(i);

  rep(i, 1, (N << 1)) if (sccid[i] == sccid[((i - 1) ^ 1) + 1]) return 0;
  return 1;
}

bool check(int d) {
  memset(st, 0, sizeof(st));
  totnode = e = 0;
  seg::build(1, 1, (N << 1));
  rep(i, 1, (N << 1)) {
    //		disp(i), disp(((i - 1) ^ 1) + 1), puts("");
    addedge(i + (N << 1), ((i - 1) ^ 1) + 1);
    addedge(((i - 1) ^ 1) + 1, i + (N << 1));
  }

  //	rep(i, 1, (N << 1)) disp(a[i].x), puts("");

  rep(i, 1, (N << 1)) {
    //		printf("while i = %d ------------------\n", i);
    int rb =
        upper_bound(a + 1, a + (N << 1) + 1, mp(a[i].x + d - 1, +oo)) - a - 1;
    int lb = upper_bound(a + 1, a + (N << 1) + 1, mp(a[i].x - d, +oo)) - a;
    //		disp(a[i].y), disp(lb), disp(rb), puts("");
    if (lb < i) {
      seg::add(1, 1, (N << 1), lb, i - 1, a[i].y);
      //			printf("%d -- > [%d,%d]\n", i, lb, i - 1);
    }
    if (rb > i && rb <= (N << 1)) {
      seg::add(1, 1, (N << 1), i + 1, rb, a[i].y);
      //			printf("%d -- > [%d,%d]\n", i, i + 1, rb);
    }
  }

  return tarjan();
}

int main() {
#ifdef Leeson
  freopen("tmp.in", "r", stdin);
  freopen("tmp.out", "w", stdout);
#endif

  read(N);
  rep(i, 1, N) {
    int x, y;
    read(x), read(y);
    a[(i << 1) - 1] = mp(x, (i << 1) - 1);
    a[(i << 1)] = mp(y, (i << 1));
  }

  sort(a + 1, a + (N << 1) + 1);

  //	disp(check(1));

  int l = -1, r = 1e9 + 1;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid;
  }

  cout << l << endl;

  return 0;
}