Problem p00475 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
pair<int, int> X1[100000], X2[100000], X0[100000];
int n;
int solve_0(int _x1, int _x2, int _y1, int _y2) {
  int minx1 = 1000000000, maxx1 = -1000000000, miny1 = 1000000000,
      maxy1 = -1000000000;
  int minx2 = 1000000000, maxx2 = -1000000000, miny2 = 1000000000,
      maxy2 = -1000000000;
  for (int i = 0; i < n; i++) {
    if (_x1 <= X1[i].first && X1[i].first <= _x2 && _y1 <= X1[i].second &&
        X1[i].second <= _y2) {
      minx1 = min(minx1, X1[i].first);
      maxx1 = max(maxx1, X1[i].first);
      miny1 = min(miny1, X1[i].second);
      maxy1 = max(maxy1, X1[i].second);
    } else {
      minx2 = min(minx2, X1[i].first);
      maxx2 = max(maxx2, X1[i].first);
      miny2 = min(miny2, X1[i].second);
      maxy2 = max(maxy2, X1[i].second);
    }
  }
  int D1 = max(maxx1 - minx1, maxy1 - miny1);
  int D2 = max(maxx2 - minx2, maxy2 - miny2);
  return max(D1, D2);
}
int sub_1(int p) {
  int miny1 = 1000000000, maxy1 = -1000000000;
  for (int i = 0; i < n; i++) {
    if (X1[i].first <= X1[0].first + p) {
      miny1 = min(miny1, X1[i].second);
      maxy1 = max(maxy1, X1[i].second);
    }
  }
  int C1 = solve_0(X1[0].first, X1[0].first + p, miny1, miny1 + p);
  int C2 = solve_0(X1[0].first, X1[0].first + p, maxy1 - p, maxy1);
  return min(C1, C2);
}
int solve_1() {
  int maxn = 1000000000;
  if (n <= 10000) {
    for (int i = 0; i < n; i++) {
      int _size = X1[i].first - X1[0].first;
      maxn = min(maxn, sub_1(_size));
    }
  } else {
    int minid = 0, maxid = 0;
    for (int i = 0; i < n + 100; i += 100) {
      int I = i;
      if (I >= n)
        I = n - 1;
      int _size = X1[I].first - X1[0].first;
      int DD = sub_1(_size);
      if (maxn > DD) {
        minid = i;
        maxid = i;
        maxn = DD;
      } else if (maxn == DD) {
        maxid = i;
      }
    }
    for (int i = minid - 100; i < maxid + 100; i++) {
      int I = i;
      if (I < 0)
        I = 0;
      if (I >= n)
        I = n - 1;
      int _size = X1[I].first - X1[0].first;
      maxn = min(maxn, sub_1(_size));
    }
  }
  return maxn;
}
int sub_2(int p) {
  int minx1 = 1000000000, maxx1 = -1000000000;
  for (int i = 0; i < n; i++) {
    if (X2[i].second <= X2[0].second + p) {
      minx1 = min(minx1, X2[i].first);
      maxx1 = max(maxx1, X2[i].first);
    }
  }
  int C1 = solve_0(minx1, minx1 + p, X2[0].second, X2[0].second + p);
  int C2 = solve_0(maxx1 - p, maxx1, X2[0].second, X2[0].second + p);
  return min(C1, C2);
}
int solve_2() {
  int maxn = 1000000000;
  if (n <= 10000) {
    for (int i = 0; i < n; i++) {
      int _size = X2[i].second - X2[0].second;
      maxn = min(maxn, sub_2(_size));
    }
  } else {
    int minid = 0, maxid = 0;
    for (int i = 0; i < n + 100; i += 100) {
      int I = i;
      if (I >= n)
        I = n - 1;
      int _size = X2[I].second - X2[0].second;
      int DD = sub_2(_size);
      if (maxn > DD) {
        minid = i;
        maxid = i;
        maxn = DD;
      } else if (maxn == DD) {
        maxid = i;
      }
    }
    for (int i = minid - 100; i < maxid + 100; i++) {
      int I = i;
      if (I < 0)
        I = 0;
      if (I >= n)
        I = n - 1;
      int _size = X2[I].second - X2[0].second;
      maxn = min(maxn, sub_2(_size));
    }
  }
  return maxn;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> X0[i].first >> X0[i].second;
  }
  for (int i = 0; i < n; i++) {
    X1[i].first = X0[i].first - X0[i].second;
    X2[i].second = X1[i].first;
    X1[i].second = X0[i].second + X0[i].first;
    X2[i].first = X1[i].second;
  }
  sort(X1, X1 + n);
  sort(X2, X2 + n);
  for (int i = 0; i < n; i++)
    swap(X2[i].first, X2[i].second);
  cout << min(solve_1(), solve_2()) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
pair<int, int> X1[100000], X2[100000], X0[100000];
int n;
int solve_0(int _x1, int _x2, int _y1, int _y2) {
  int minx1 = 1000000000, maxx1 = -1000000000, miny1 = 1000000000,
      maxy1 = -1000000000;
  int minx2 = 1000000000, maxx2 = -1000000000, miny2 = 1000000000,
      maxy2 = -1000000000;
  for (int i = 0; i < n; i++) {
    if (_x1 <= X1[i].first && X1[i].first <= _x2 && _y1 <= X1[i].second &&
        X1[i].second <= _y2) {
      minx1 = min(minx1, X1[i].first);
      maxx1 = max(maxx1, X1[i].first);
      miny1 = min(miny1, X1[i].second);
      maxy1 = max(maxy1, X1[i].second);
    } else {
      minx2 = min(minx2, X1[i].first);
      maxx2 = max(maxx2, X1[i].first);
      miny2 = min(miny2, X1[i].second);
      maxy2 = max(maxy2, X1[i].second);
    }
  }
  int D1 = max(maxx1 - minx1, maxy1 - miny1);
  int D2 = max(maxx2 - minx2, maxy2 - miny2);
  return max(D1, D2);
}
int sub_1(int p) {
  int miny1 = 1000000000, maxy1 = -1000000000;
  for (int i = 0; i < n; i++) {
    if (X1[i].first <= X1[0].first + p) {
      miny1 = min(miny1, X1[i].second);
      maxy1 = max(maxy1, X1[i].second);
    }
  }
  int C1 = solve_0(X1[0].first, X1[0].first + p, miny1, miny1 + p);
  int C2 = solve_0(X1[0].first, X1[0].first + p, maxy1 - p, maxy1);
  return min(C1, C2);
}
int solve_1() {
  int maxn = 1000000000;
  for (int i = 0; i < n; i++) {
    int _size = X1[i].first - X1[0].first;
    maxn = min(maxn, sub_1(_size));
  }
  return maxn;
}
int sub_2(int p) {
  int minx1 = 1000000000, maxx1 = -1000000000;
  for (int i = 0; i < n; i++) {
    if (X2[i].second <= X2[0].second + p) {
      minx1 = min(minx1, X2[i].first);
      maxx1 = max(maxx1, X2[i].first);
    }
  }
  int C1 = solve_0(minx1, minx1 + p, X2[0].second, X2[0].second + p);
  int C2 = solve_0(maxx1 - p, maxx1, X2[0].second, X2[0].second + p);
  return min(C1, C2);
}
int solve_2() {
  int maxn = 1000000000;
  for (int i = 0; i < n; i++) {
    int _size = X2[i].second - X2[0].second;
    maxn = min(maxn, sub_2(_size));
  }
  return maxn;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> X0[i].first >> X0[i].second;
  }
  for (int i = 0; i < n; i++) {
    X1[i].first = X0[i].first - X0[i].second;
    X2[i].second = X1[i].first;
    X1[i].second = X0[i].second + X0[i].first;
    X2[i].first = X1[i].second;
  }
  sort(X1, X1 + n);
  sort(X2, X2 + n);
  for (int i = 0; i < n; i++)
    swap(X2[i].first, X2[i].second);
  cout << min(solve_1(), solve_2()) << endl;
  return 0;
}