Problem p03802 (C++)

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

typedef pair<int, int> pii;

#define fi first
#define se second

const int MAXN = 1024;
const int MAXX = 1e9 + 5;

pii f[2 * MAXN];
int p[MAXN][2];
vector<int> ed[8 * MAXN], edinv[8 * MAXN];
int scc[8 * MAXN], dfsv[8 * MAXN], dfsvn;
bool chk[8 * MAXN];
int ppp;

void mkedge(int x, int y) { // x -> y
  // printf("mkedge(%d, %d)\n", x, y);
  ed[x].push_back(y);
  ed[(y + 4 * MAXN) % (8 * MAXN)].push_back((x + 4 * MAXN) % (8 * MAXN));
  edinv[y].push_back(x);
  edinv[(x + 4 * MAXN) % (8 * MAXN)].push_back((y + 4 * MAXN) % (8 * MAXN));
}

void con(int idx, int l, int r, int x, int p, int q) {
  // if(idx == 1) printf("con(x = %d, p = %d, q = %d)\n", x, p, q);
  if (p <= l && r <= q) {
    mkedge(x + 2 * MAXN, idx + 4 * MAXN);
  } else if (l <= q && p <= r) {
    int m = (l + r) / 2;
    con(idx * 2, l, m, x, p, q);
    con(idx * 2 + 1, m + 1, r, x, p, q);
  }
}

void mkdfsv(int x) {
  chk[x] = true;
  for (auto a : ed[x])
    if (!chk[a])
      mkdfsv(a);
  dfsv[dfsvn++] = x;
}

void mkscc(int x, int s) {
  scc[x] = s;
  for (auto a : edinv[x])
    if (scc[a] == -1)
      mkscc(a, s);
}

int main() {
  int N;

  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> f[2 * i].fi >> f[2 * i + 1].fi;

  for (int i = 0; i < 2 * N; i++)
    f[i].se = i / 2;
  sort(f, f + 2 * N);
  for (int i = 0; i < N; i++)
    p[i][0] = -1;
  for (int i = 0; i < 2 * N; i++) {
    if (p[f[i].se][0] == -1)
      p[f[i].se][0] = i;
    else
      p[f[i].se][1] = i;
  }

  // for(int i = 0; i < N; i++) printf("%d %d\n", p[i][0], p[i][1]);

  int s = 0, e = MAXX;
  while (s < e) {
    // if(++ppp < 100) printf("s = %d, e = %d\n", s, e);
    for (int i = 0; i < 8 * MAXN; i++) {
      ed[i].clear();
      edinv[i].clear();
      scc[i] = -1;
      chk[i] = false;
    }
    dfsvn = 0;

    for (int i = 0; i < N; i++) {
      mkedge(p[i][0] + 2 * MAXN, p[i][1] + 6 * MAXN);
      mkedge(p[i][1] + 6 * MAXN, p[i][0] + 2 * MAXN);
    }
    for (int i = 2; i < 4 * MAXN; i++)
      mkedge(i, i / 2);
    int m = (s + e + 1) / 2;
    int x = 0, y = 0;
    for (int i = 0; i < 2 * N; i++) {
      while (f[x].fi <= f[i].fi - m)
        x++;
      con(1, 0, 2 * MAXN - 1, i, x, i - 1);
      while (y < 2 * N && f[y].fi < f[i].fi + m)
        y++;
      con(1, 0, 2 * MAXN - 1, i, i + 1, y - 1);
    }
    /*
    for(int i = 0; i < 8 * MAXN; i++) {
            printf("ed[%d] = [", i);
            for(auto a : ed[i]) printf("%d ", a);
            printf("]\n");
    }
    */
    for (int i = 0; i < 8 * MAXN; i++)
      if (!chk[i])
        mkdfsv(i);
    for (int i = 8 * MAXN - 1; i >= 0; i--)
      if (scc[dfsv[i]] == -1)
        mkscc(dfsv[i], i);
    // for(int i = 0; i < 8 * MAXN; i++) printf("scc[%d] = %d\n", i, scc[i]);
    bool b = true;
    for (int i = 0; i < 4 * MAXN; i++)
      if (scc[i] == scc[i + 4 * MAXN])
        b = false;
    if (b)
      s = m;
    else
      e = m - 1;
  }

  cout << s;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;

#define fi first
#define se second

const int MAXN = 1 << 14;
const int MAXX = 1e9 + 5;

pii f[2 * MAXN];
int p[MAXN][2];
vector<int> ed[8 * MAXN], edinv[8 * MAXN];
int scc[8 * MAXN], dfsv[8 * MAXN], dfsvn;
bool chk[8 * MAXN];
int ppp;

void mkedge(int x, int y) { // x -> y
  // printf("mkedge(%d, %d)\n", x, y);
  ed[x].push_back(y);
  ed[(y + 4 * MAXN) % (8 * MAXN)].push_back((x + 4 * MAXN) % (8 * MAXN));
  edinv[y].push_back(x);
  edinv[(x + 4 * MAXN) % (8 * MAXN)].push_back((y + 4 * MAXN) % (8 * MAXN));
}

void con(int idx, int l, int r, int x, int p, int q) {
  // if(idx == 1) printf("con(x = %d, p = %d, q = %d)\n", x, p, q);
  if (p <= l && r <= q) {
    mkedge(x + 2 * MAXN, idx + 4 * MAXN);
  } else if (l <= q && p <= r) {
    int m = (l + r) / 2;
    con(idx * 2, l, m, x, p, q);
    con(idx * 2 + 1, m + 1, r, x, p, q);
  }
}

void mkdfsv(int x) {
  chk[x] = true;
  for (auto a : ed[x])
    if (!chk[a])
      mkdfsv(a);
  dfsv[dfsvn++] = x;
}

void mkscc(int x, int s) {
  scc[x] = s;
  for (auto a : edinv[x])
    if (scc[a] == -1)
      mkscc(a, s);
}

int main() {
  int N;

  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> f[2 * i].fi >> f[2 * i + 1].fi;

  for (int i = 0; i < 2 * N; i++)
    f[i].se = i / 2;
  sort(f, f + 2 * N);
  for (int i = 0; i < N; i++)
    p[i][0] = -1;
  for (int i = 0; i < 2 * N; i++) {
    if (p[f[i].se][0] == -1)
      p[f[i].se][0] = i;
    else
      p[f[i].se][1] = i;
  }

  // for(int i = 0; i < N; i++) printf("%d %d\n", p[i][0], p[i][1]);

  int s = 0, e = MAXX;
  while (s < e) {
    // if(++ppp < 100) printf("s = %d, e = %d\n", s, e);
    for (int i = 0; i < 8 * MAXN; i++) {
      ed[i].clear();
      edinv[i].clear();
      scc[i] = -1;
      chk[i] = false;
    }
    dfsvn = 0;

    for (int i = 0; i < N; i++) {
      mkedge(p[i][0] + 2 * MAXN, p[i][1] + 6 * MAXN);
      mkedge(p[i][1] + 6 * MAXN, p[i][0] + 2 * MAXN);
    }
    for (int i = 2; i < 4 * MAXN; i++)
      mkedge(i, i / 2);
    int m = (s + e + 1) / 2;
    int x = 0, y = 0;
    for (int i = 0; i < 2 * N; i++) {
      while (f[x].fi <= f[i].fi - m)
        x++;
      con(1, 0, 2 * MAXN - 1, i, x, i - 1);
      while (y < 2 * N && f[y].fi < f[i].fi + m)
        y++;
      con(1, 0, 2 * MAXN - 1, i, i + 1, y - 1);
    }
    /*
    for(int i = 0; i < 8 * MAXN; i++) {
            printf("ed[%d] = [", i);
            for(auto a : ed[i]) printf("%d ", a);
            printf("]\n");
    }
    */
    for (int i = 0; i < 8 * MAXN; i++)
      if (!chk[i])
        mkdfsv(i);
    for (int i = 8 * MAXN - 1; i >= 0; i--)
      if (scc[dfsv[i]] == -1)
        mkscc(dfsv[i], i);
    // for(int i = 0; i < 8 * MAXN; i++) printf("scc[%d] = %d\n", i, scc[i]);
    bool b = true;
    for (int i = 0; i < 4 * MAXN; i++)
      if (scc[i] == scc[i + 4 * MAXN])
        b = false;
    if (b)
      s = m;
    else
      e = m - 1;
  }

  cout << s;
  return 0;
}