Problem p03802 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back

typedef long long ll;
typedef long double ld;

using namespace std;
const int MAXN = 12000;
int en[MAXN * 20];
int was[MAXN * 20];
vector<int> eds[MAXN * 20];
int n;
vector<pair<ll, int>> vv;
ll x[MAXN];
ll y[MAXN];

void st(int v) {
  en[v] = 1;
  for (int u : eds[v]) {
    if (!en[u])
      st(u);
  }
}

void dfs1(int v) {
  was[v] = 1;
  for (int u : eds[v]) {
    if (!was[u])
      dfs1(u);
  }
  if (v < 2 * n) {
    if (!en[v] && !en[v ^ 1]) {
      st(v);
    }
  }
}

void add(int v, int tl, int tr, int l, int r, int x) {
  if (r <= tl || tr <= l)
    return;
  if (l <= tl && tr <= r) {
    eds[x].push_back(v + 2 * n);
    eds[v + 2 * n + 4 * vv.size()].push_back(x ^ 1);
    return;
  }
  int m = (tl + tr) >> 1;
  add(v * 2 + 1, tl, m, l, r, x);
  add(v * 2 + 2, m, tr, l, r, x);
}

void build(int v, int tl, int tr) {
  if (tl + 1 == tr) {
    eds[v + 2 * n].push_back(vv[tl].second);
    eds[vv[tl].second ^ 1].push_back(v + 2 * n + 4 * vv.size());
    return;
  }
  int m = (tl + tr) >> 1;
  eds[v + 2 * n].push_back(2 * v + 1 + 2 * n);
  eds[v + 2 * n].push_back(2 * v + 2 + 2 * n);
  eds[2 * v + 1 + 2 * n + 4 * vv.size()].push_back(v + 2 * n + 4 * vv.size());
  eds[2 * v + 2 + 2 * n + 4 * vv.size()].push_back(v + 2 * n + 4 * vv.size());
  build(v * 2 + 1, tl, m);
  build(v * 2 + 2, m, tr);
}

int check(ll d) {
  for (int i = 0; i < 2 * n + 8 * vv.size(); ++i)
    eds[i].clear();
  build(0, 0, vv.size());
  for (int i = 0; i < n; ++i) {
    int l = lower_bound(vv.begin(), vv.end(), make_pair(x[i] - d + 1, 0)) -
            vv.begin();
    int r = lower_bound(vv.begin(), vv.end(), make_pair(x[i], 2 * i + 1)) -
            vv.begin();
    if (l < r)
      add(0, 0, vv.size(), l, r, 2 * i);
    l = lower_bound(vv.begin(), vv.end(), make_pair(x[i], 2 * i + 2)) -
        vv.begin();
    r = lower_bound(vv.begin(), vv.end(), make_pair(x[i] + d, 0)) - vv.begin();
    if (l < r)
      add(0, 0, vv.size(), l, r, 2 * i);
    l = lower_bound(vv.begin(), vv.end(), make_pair(y[i] - d + 1, 0)) -
        vv.begin();
    r = lower_bound(vv.begin(), vv.end(), make_pair(y[i], 2 * i)) - vv.begin();
    if (l < r)
      add(0, 0, vv.size(), l, r, 2 * i + 1);
    l = lower_bound(vv.begin(), vv.end(), make_pair(y[i], 2 * i + 1)) -
        vv.begin();
    r = lower_bound(vv.begin(), vv.end(), make_pair(y[i] + d, 0)) - vv.begin();
    if (l < r)
      add(0, 0, vv.size(), l, r, 2 * i + 1);
  }
  for (int i = 0; i < 2 * n + 8 * vv.size(); ++i)
    en[i] = 0, was[i] = 0;
  for (int i = 0; i < 2 * n; ++i) {
    if (!was[i])
      dfs1(i);
  }
  for (int i = 0; i < n; ++i) {
    if (en[2 * i] && en[2 * i + 1])
      return 0;
  }
  return 1;
}

ll solve() {
  vv.clear();
  for (int i = 0; i < n; ++i)
    vv.push_back(make_pair(x[i], 2 * i + 1));
  for (int i = 0; i < n; ++i)
    vv.push_back(make_pair(y[i], 2 * i));
  sort(vv.begin(), vv.end());
  ll l = 0;
  ll r = 1e9 + 100;
  while (r - l > 1) {
    ll mid = (l + r) >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid;
  }
  return l;
}

ll x2[MAXN];
ll y2[MAXN];

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    scanf("%lld%lld", x + i, y + i);
  ll ans = solve();
  for (int i = 0; i < 5; ++i) {
    vector<int> prm;
    for (int i = 0; i < n; ++i)
      prm.push_back(i);
    random_shuffle(prm.begin(), prm.end());
    for (int i = 0; i < n; ++i)
      x2[prm[i]] = x[i], y2[prm[i]] = y[i];
    for (int i = 0; i < n; ++i)
      x[i] = x2[i], y[i] = y2[i];
    ans = max(ans, solve());
  }
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back

typedef long long ll;
typedef long double ld;

using namespace std;
const int MAXN = 12000;
int en[MAXN * 20];
int was[MAXN * 20];
vector<int> eds[MAXN * 20];
int n;
vector<pair<ll, int>> vv;
ll x[MAXN];
ll y[MAXN];

void st(int v) {
  en[v] = 1;
  for (int u : eds[v]) {
    if (!en[u])
      st(u);
  }
}

void dfs1(int v) {
  was[v] = 1;
  for (int u : eds[v]) {
    if (!was[u])
      dfs1(u);
  }
  if (v < 2 * n) {
    if (!en[v] && !en[v ^ 1]) {
      st(v);
    }
  }
}

void add(int v, int tl, int tr, int l, int r, int x) {
  if (r <= tl || tr <= l)
    return;
  if (l <= tl && tr <= r) {
    eds[x].push_back(v + 2 * n);
    eds[v + 2 * n + 4 * vv.size()].push_back(x ^ 1);
    return;
  }
  int m = (tl + tr) >> 1;
  add(v * 2 + 1, tl, m, l, r, x);
  add(v * 2 + 2, m, tr, l, r, x);
}

void build(int v, int tl, int tr) {
  if (tl + 1 == tr) {
    eds[v + 2 * n].push_back(vv[tl].second);
    eds[vv[tl].second ^ 1].push_back(v + 2 * n + 4 * vv.size());
    return;
  }
  int m = (tl + tr) >> 1;
  eds[v + 2 * n].push_back(2 * v + 1 + 2 * n);
  eds[v + 2 * n].push_back(2 * v + 2 + 2 * n);
  eds[2 * v + 1 + 2 * n + 4 * vv.size()].push_back(v + 2 * n + 4 * vv.size());
  eds[2 * v + 2 + 2 * n + 4 * vv.size()].push_back(v + 2 * n + 4 * vv.size());
  build(v * 2 + 1, tl, m);
  build(v * 2 + 2, m, tr);
}

int check(ll d) {
  for (int i = 0; i < 2 * n + 8 * vv.size(); ++i)
    eds[i].clear();
  build(0, 0, vv.size());
  for (int i = 0; i < n; ++i) {
    int l = lower_bound(vv.begin(), vv.end(), make_pair(x[i] - d + 1, 0)) -
            vv.begin();
    int r = lower_bound(vv.begin(), vv.end(), make_pair(x[i], 2 * i + 1)) -
            vv.begin();
    if (l < r)
      add(0, 0, vv.size(), l, r, 2 * i);
    l = lower_bound(vv.begin(), vv.end(), make_pair(x[i], 2 * i + 2)) -
        vv.begin();
    r = lower_bound(vv.begin(), vv.end(), make_pair(x[i] + d, 0)) - vv.begin();
    if (l < r)
      add(0, 0, vv.size(), l, r, 2 * i);
    l = lower_bound(vv.begin(), vv.end(), make_pair(y[i] - d + 1, 0)) -
        vv.begin();
    r = lower_bound(vv.begin(), vv.end(), make_pair(y[i], 2 * i)) - vv.begin();
    if (l < r)
      add(0, 0, vv.size(), l, r, 2 * i + 1);
    l = lower_bound(vv.begin(), vv.end(), make_pair(y[i], 2 * i + 1)) -
        vv.begin();
    r = lower_bound(vv.begin(), vv.end(), make_pair(y[i] + d, 0)) - vv.begin();
    if (l < r)
      add(0, 0, vv.size(), l, r, 2 * i + 1);
  }
  for (int i = 0; i < 2 * n + 8 * vv.size(); ++i)
    en[i] = 0, was[i] = 0;
  for (int i = 0; i < 2 * n; ++i) {
    if (!was[i])
      dfs1(i);
  }
  for (int i = 0; i < n; ++i) {
    if (en[2 * i] && en[2 * i + 1])
      return 0;
  }
  return 1;
}

ll solve() {
  vv.clear();
  for (int i = 0; i < n; ++i)
    vv.push_back(make_pair(x[i], 2 * i + 1));
  for (int i = 0; i < n; ++i)
    vv.push_back(make_pair(y[i], 2 * i));
  sort(vv.begin(), vv.end());
  ll l = 0;
  ll r = 1e9 + 100;
  while (r - l > 1) {
    ll mid = (l + r) >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid;
  }
  return l;
}

ll x2[MAXN];
ll y2[MAXN];

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    scanf("%lld%lld", x + i, y + i);
  ll ans = solve();
  for (int i = 0; i < 3; ++i) {
    vector<int> prm;
    for (int i = 0; i < n; ++i)
      prm.push_back(i);
    random_shuffle(prm.begin(), prm.end());
    for (int i = 0; i < n; ++i)
      x2[prm[i]] = x[i], y2[prm[i]] = y[i];
    for (int i = 0; i < n; ++i)
      x[i] = x2[i], y[i] = y2[i];
    ans = max(ans, solve());
  }
  cout << ans << "\n";
  return 0;
}