Problem p03802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define FOR(i, x, y) for (int i = (x); i < (y); ++i)
#define REP(i, x, y) for (int i = (x); i < (y); ++i)
#define MP make_pair
#define PB push_back
#define PH push
#define fst first
#define snd second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;

const int siz = 16384;
const int IN = 0;

int n, tot = 0;
int x[siz], y[siz], a[siz][2];
vector<pii> vec;
vector<int> g[siz << 2];

class SegmentTree {
private:
  inline void build(int x, int l, int r) {
    ind[x] = tot++;
    g[ind[x]].clear();
    if (x != 1) {
      if (tp == IN)
        g[ind[x >> 1]].PB(ind[x]);
      else
        g[ind[x]].PB(ind[x >> 1]);
    }
    int md = l + r >> 1;
    if (l == r)
      return;
    build(x << 1, l, md);
    build(x << 1 | 1, md + 1, r);
    return;
  }

public:
  int tp;
  int ind[siz << 1];
  inline int size() { return siz; }
  inline void init(int _tp) {
    tp = _tp;
    build(1, 0, size() - 1);
    return;
  }
  inline void update(int x, int l, int r, int s, int t, int u) {
    if (s > t)
      return;
    if (l >= s && r <= t) {
      //			printf("%d %s %d\n", u, (tp ? "->" : "<-"),
      //ind[x]);
      if (tp == IN)
        g[u].PB(ind[x]);
      else
        g[ind[x]].PB(u);
      return;
    }
    int md = l + r >> 1;
    if (s <= md)
      update(x << 1, l, md, s, t, u);
    if (t > md)
      update(x << 1 | 1, md + 1, r, s, t, u);
    return;
  }
} segi, sego;

inline int id(int u, int i) {
  if (i & 1)
    return sego.ind[siz + u];
  return segi.ind[siz + u];
}

namespace Tarjan {
int clk, top, cnt;
int dfn[siz << 2], ins[siz << 2], stk[siz << 2], low[siz << 2], col[siz << 2];
inline void init() {
  clk = top = cnt = 0;
  memset(dfn, -1, sizeof(dfn));
  //		memset(col, -1, sizeof(col));
  return;
}
inline void dfs(int u) {
  //		printf("u = %d\n", u);
  low[u] = dfn[u] = clk++;
  stk[++top] = u;
  ins[u] = true;
  FOR(i, 0, g[u].size()) {
    int v = g[u][i];
    //			printf("u = %d v = %d\n", u, v);
    if (!~dfn[v]) {
      dfs(v);
      low[u] = min(low[u], low[v]);
    } else if (ins[v])
      low[u] = min(low[u], dfn[v]);
  }
  if (low[u] == dfn[u]) {
    for (int v = stk[top]; top && v != u; v = stk[--top]) {
      col[v] = cnt;
      ins[v] = false;
      //				printf("%d ", v);
    }
    //			printf("%d\n", u);
    col[stk[top--]] = cnt++;
    ins[u] = false;
  }
  return;
}
inline bool work() {
  init();
  FOR(i, 0, tot) if (!~dfn[i]) {
    //			printf("i = %d\n", i);
    dfs(i);
  }
  //		printf("cnt = %d\n", cnt);
  //		FOR(i, 0, vec.size())
  //			printf("%d %d\n", col[id(i, 0)], col[id(i, 1)]);
  FOR(i, 0, vec.size()) if (col[id(i, 0)] == col[id(i, 1)]) return false;
  return true;
}
} // namespace Tarjan

inline void OR(int u, int v) { // u or v = true
  //	printf("u = %d v = %d (%d %d) (%d %d)\n", u, v, id(u, 0), id(u, 1),
  //id(v, 0), id(v, 1));
  g[id(u, 0)].PB(id(v, 1));
  g[id(v, 0)].PB(id(u, 1));
  return;
}

inline void AND(int u, int l, int r) {                // u and (l ... r) = false
  segi.update(1, 0, sego.size() - 1, l, r, id(u, 1)); //(u, 1) -> (l...r, 0)
  sego.update(1, 0, segi.size() - 1, l, r, id(u, 0)); //(l...r, 1) -> (u, 0)
  return;
}

inline bool check(int md) {
  //	printf("md = %d\n", md);
  tot = 0;
  segi.init(0);
  sego.init(1);
  FOR(i, 0, n) {
    //		printf("i = %d\n", i);
    OR(a[i][0], a[i][1]);
  }
  //	puts("ok");
  FOR(i, 0, vec.size()) {
    int val = vec[i].fst;
    int r = min((ll)vec.size() - 1,
                (ll)(lower_bound(vec.begin(), vec.end(), MP(val + md, 0)) -
                     vec.begin() - 1));
    //		printf("i = %d r = %d\n", i, r);
    AND(i, i + 1, r);
  }
  //	printf("md = %d\n", md);
  return Tarjan::work();
}

int main() {
  scanf("%d", &n);
  FOR(i, 0, n) {
    scanf("%d%d", x + i, y + i);
    vec.PB(MP(x[i], i << 1));
    vec.PB(MP(y[i], i << 1 | 1));
  }
  sort(vec.begin(), vec.end());
  FOR(i, 0, vec.size()) {
    int j = vec[i].snd;
    a[j >> 1][j & 1] = i;
  }
  //	FOR(i, 0, n)
  //		printf("%d %d\n", a[i][0], a[i][1]);
  int lb = 0, rb = 1e9 + 7;
  for (; lb + 1 < rb;) {
    int md = lb + rb >> 1;
    if (check(md))
      lb = md;
    else
      rb = md;
  }
  printf("%d\n", lb);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define FOR(i, x, y) for (int i = (x); i < (y); ++i)
#define REP(i, x, y) for (int i = (x); i < (y); ++i)
#define MP make_pair
#define PB push_back
#define PH push
#define fst first
#define snd second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;

const int siz = 32768;
const int IN = 0;

int n, tot = 0;
int x[siz], y[siz], a[siz][2];
vector<pii> vec;
vector<int> g[siz << 2];

class SegmentTree {
private:
  inline void build(int x, int l, int r) {
    ind[x] = tot++;
    g[ind[x]].clear();
    if (x != 1) {
      if (tp == IN)
        g[ind[x >> 1]].PB(ind[x]);
      else
        g[ind[x]].PB(ind[x >> 1]);
    }
    int md = l + r >> 1;
    if (l == r)
      return;
    build(x << 1, l, md);
    build(x << 1 | 1, md + 1, r);
    return;
  }

public:
  int tp;
  int ind[siz << 1];
  inline int size() { return siz; }
  inline void init(int _tp) {
    tp = _tp;
    build(1, 0, size() - 1);
    return;
  }
  inline void update(int x, int l, int r, int s, int t, int u) {
    if (s > t)
      return;
    if (l >= s && r <= t) {
      //			printf("%d %s %d\n", u, (tp ? "->" : "<-"),
      //ind[x]);
      if (tp == IN)
        g[u].PB(ind[x]);
      else
        g[ind[x]].PB(u);
      return;
    }
    int md = l + r >> 1;
    if (s <= md)
      update(x << 1, l, md, s, t, u);
    if (t > md)
      update(x << 1 | 1, md + 1, r, s, t, u);
    return;
  }
} segi, sego;

inline int id(int u, int i) {
  if (i & 1)
    return sego.ind[siz + u];
  return segi.ind[siz + u];
}

namespace Tarjan {
int clk, top, cnt;
int dfn[siz << 2], ins[siz << 2], stk[siz << 2], low[siz << 2], col[siz << 2];
inline void init() {
  clk = top = cnt = 0;
  memset(dfn, -1, sizeof(dfn));
  //		memset(col, -1, sizeof(col));
  return;
}
inline void dfs(int u) {
  //		printf("u = %d\n", u);
  low[u] = dfn[u] = clk++;
  stk[++top] = u;
  ins[u] = true;
  FOR(i, 0, g[u].size()) {
    int v = g[u][i];
    //			printf("u = %d v = %d\n", u, v);
    if (!~dfn[v]) {
      dfs(v);
      low[u] = min(low[u], low[v]);
    } else if (ins[v])
      low[u] = min(low[u], dfn[v]);
  }
  if (low[u] == dfn[u]) {
    for (int v = stk[top]; top && v != u; v = stk[--top]) {
      col[v] = cnt;
      ins[v] = false;
      //				printf("%d ", v);
    }
    //			printf("%d\n", u);
    col[stk[top--]] = cnt++;
    ins[u] = false;
  }
  return;
}
inline bool work() {
  init();
  FOR(i, 0, tot) if (!~dfn[i]) {
    //			printf("i = %d\n", i);
    dfs(i);
  }
  //		printf("cnt = %d\n", cnt);
  //		FOR(i, 0, vec.size())
  //			printf("%d %d\n", col[id(i, 0)], col[id(i, 1)]);
  FOR(i, 0, vec.size()) if (col[id(i, 0)] == col[id(i, 1)]) return false;
  return true;
}
} // namespace Tarjan

inline void OR(int u, int v) { // u or v = true
  //	printf("u = %d v = %d (%d %d) (%d %d)\n", u, v, id(u, 0), id(u, 1),
  //id(v, 0), id(v, 1));
  g[id(u, 0)].PB(id(v, 1));
  g[id(v, 0)].PB(id(u, 1));
  return;
}

inline void AND(int u, int l, int r) {                // u and (l ... r) = false
  segi.update(1, 0, sego.size() - 1, l, r, id(u, 1)); //(u, 1) -> (l...r, 0)
  sego.update(1, 0, segi.size() - 1, l, r, id(u, 0)); //(l...r, 1) -> (u, 0)
  return;
}

inline bool check(int md) {
  //	printf("md = %d\n", md);
  tot = 0;
  segi.init(0);
  sego.init(1);
  FOR(i, 0, n) {
    //		printf("i = %d\n", i);
    OR(a[i][0], a[i][1]);
  }
  //	puts("ok");
  FOR(i, 0, vec.size()) {
    int val = vec[i].fst;
    int r = min((ll)vec.size() - 1,
                (ll)(lower_bound(vec.begin(), vec.end(), MP(val + md, 0)) -
                     vec.begin() - 1));
    //		printf("i = %d r = %d\n", i, r);
    AND(i, i + 1, r);
  }
  //	printf("md = %d\n", md);
  return Tarjan::work();
}

int main() {
  scanf("%d", &n);
  FOR(i, 0, n) {
    scanf("%d%d", x + i, y + i);
    vec.PB(MP(x[i], i << 1));
    vec.PB(MP(y[i], i << 1 | 1));
  }
  sort(vec.begin(), vec.end());
  FOR(i, 0, vec.size()) {
    int j = vec[i].snd;
    a[j >> 1][j & 1] = i;
  }
  //	FOR(i, 0, n)
  //		printf("%d %d\n", a[i][0], a[i][1]);
  int lb = 0, rb = 1e9 + 7;
  for (; lb + 1 < rb;) {
    int md = lb + rb >> 1;
    if (check(md))
      lb = md;
    else
      rb = md;
  }
  printf("%d\n", lb);
  return 0;
}