Problem p01468 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <vector>
using namespace std;

bool ne(int *a, int *b) {
  for (int i = 0; i < 4; i++) {
    if (a[i] != b[i])
      return true;
  }
  return false;
}
double dot(int *a, int *b) {
  return (a[1] - a[0]) * (b[1] - b[0]) + (a[3] - a[2]) * (b[3] - b[2]);
}
double cross(int *a, int *b) {
  return (a[1] - a[0]) * (b[3] - b[2]) - (a[3] - a[2]) * (b[1] - b[0]);
}
double chk(vector<int *> vs) {
  double mn = LONG_MAX;
  for (int i = 0; i < vs.size(); i++)
    for (int j = 0; j < vs.size(); j++)
      if (ne(vs[i], vs[j]))
        for (int k = 0; k < 2; k++) {
          int p1q1[4] = {vs[i][0], vs[j][0 + k], vs[i][2], vs[j][2 + k]};
          int p2q1[4] = {vs[i][1], vs[j][0 + k], vs[i][3], vs[j][2 + k]};
          int p1q2[4] = {vs[i][0], vs[j][1], vs[i][2], vs[j][3]};
          int q1p1[4] = {vs[j][0], vs[i][0], vs[j][2], vs[i][2]};
          int q1p2[4] = {vs[j][0], vs[i][1], vs[j][2], vs[i][3]};
          if (cross(vs[i], p1q1) * cross(vs[i], p1q2) < 0 &&
              cross(vs[j], q1p1) * cross(vs[j], q1p2) < 0) {
            return 0;
          }
          double p1p2_p1p2 = dot(vs[i], vs[i]);
          double p1q1_p1p2 = dot(p1q1, vs[i]);
          if (p1q1_p1p2 <= 0) {
            mn = min(mn, dot(p1q1, p1q1));
          } else if (p1q1_p1p2 >= p1p2_p1p2) {
            mn = min(mn, dot(p2q1, p2q1));
          } else {
            double p1q1_x_p1p2 = cross(vs[i], p1q1);
            mn = min(mn, p1q1_x_p1p2 * p1q1_x_p1p2 / p1p2_p1p2);
          }
        }
  return mn;
}
int main() {
  int N;
  scanf("%d", &N);
  vector<int *> vs(N);
  for (int i = 0; i < N; i++) {
    vs[i] = new int[4];
    scanf("%d %d %d %d", &vs[i][0], &vs[i][2], &vs[i][1], &vs[i][3]);
  }
  double mn = LONG_MAX;
  for (int split = 5; mn == LONG_MAX && split > 0; split /= 2) {
    vector<vector<int *>> area(split * split, vector<int *>());
    if (area.size() == 1) {
      area[0] = vs;
    } else {
      for (int i = 0; i < N; i++) {
        int sx = min(4, split * min(vs[i][0], vs[i][1]) / 100);
        int ex = split * max(vs[i][0], vs[i][1]) / 100;
        int sy = min(4, split * min(vs[i][2], vs[i][3]) / 100);
        int ey = split * max(vs[i][2], vs[i][3]) / 100;
        for (int x = sx; x < split && x <= ex; x++) {
          for (int y = sy; y < split && y <= ey; y++) {
            area[x + split * y].push_back(vs[i]);
          }
        }
      }
    }
    for (int x = 0; x < split; x++) {
      for (int y = 0; y < split; y++) {
        if (area[x + split * y].size()) {
          sort(area[x + split * y].begin(), area[x + split * y].end(),
               [](int *a, int *b) {
                 return min(a[0] + a[2], a[1] + a[3]) >
                        min(b[0] + b[2], b[1] + b[3]);
               });
          if (x > 0)
            area[(x - 1) + split * y].push_back(area[x + split * y][0]);
          if (y > 0)
            area[x + split * (y - 1)].push_back(area[x + split * y][0]);
          if (x * y > 0)
            area[(x - 1) + split * (y - 1)].push_back(area[x + split * y][0]);
        }
      }
    }
    for (int i = 0; i < area.size(); i++) {
      mn = min(mn, chk(area[i]));
    }
  }
  printf("%f\\n", sqrt(mn));
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

class v {
public:
  v(int x0, int y0, int x1, int y1) {
    d = new int[4];
    d[0] = x0;
    d[1] = x1;
    d[2] = y0;
    d[3] = y1;
  }
  int *d;
  double operator*(v v2) {
    return (this->d[1] - this->d[0]) * (v2.d[1] - v2.d[0]) +
           (this->d[3] - this->d[2]) * (v2.d[3] - v2.d[2]);
  }
  double operator&(v v2) {
    return (this->d[1] - this->d[0]) * (v2.d[3] - v2.d[2]) -
           (this->d[3] - this->d[2]) * (v2.d[1] - v2.d[0]);
  }
  bool operator==(v v2) {
    bool b = true;
    for (int i = 0; i < 4; i++)
      b &= this->d[i] == v2.d[i];
    return b;
  }
};

double chk(vector<v> vs) {
  double mn = LONG_MAX;
  for (int i = 0; i < vs.size(); i++)
    for (int j = 0; j < vs.size(); j++)
      if (!(vs[i] == vs[j]))
        for (int k = 0; k < 2; k++) {
          v p1q1 = v(vs[i].d[0], vs[i].d[2], vs[j].d[0 + k], vs[j].d[2 + k]);
          v p2q1 = v(vs[i].d[1], vs[i].d[3], vs[j].d[0 + k], vs[j].d[2 + k]);
          v p1q2 = v(vs[i].d[0], vs[i].d[2], vs[j].d[1], vs[j].d[3]);
          v q1p1 = v(vs[j].d[0], vs[j].d[2], vs[i].d[0], vs[i].d[2]);
          v q1p2 = v(vs[j].d[0], vs[j].d[2], vs[i].d[1], vs[i].d[3]);
          if ((vs[i] & p1q1) * (vs[i] & p1q2) < 0 &&
              (vs[j] & q1p1) * (vs[j] & q1p2) < 0) {
            return 0;
          }
          double p1q1xp1p2 = p1q1 * vs[i];
          if (p1q1xp1p2 <= 0) {
            mn = min(mn, p1q1 * p1q1);
          } else if (p1q1xp1p2 >= vs[i] * vs[i]) {
            mn = min(mn, p2q1 * p2q1);
          } else {
            mn = min(mn, p1q1 * p1q1 - p1q1xp1p2 * p1q1xp1p2 / (vs[i] * vs[i]));
          }
        }
  return mn;
}
int main() {
  int N;
  cin >> N;
  vector<v> vs;
  for (int i = 0; i < N; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    vs.push_back(v(a, b, c, d));
  }
  double mn = LONG_MAX;
  for (int split = 5; mn == LONG_MAX && split > 0; split /= 2) {
    vector<vector<v>> area(split * split, vector<v>());
    if (area.size() == 1) {
      area[0] = vs;
    } else {
      for (int i = 0; i < N; i++) {
        int sx = min(4, split * min(vs[i].d[0], vs[i].d[1]) / 100);
        int ex = split * max(vs[i].d[0], vs[i].d[1]) / 100;
        int sy = min(4, split * min(vs[i].d[2], vs[i].d[3]) / 100);
        int ey = split * max(vs[i].d[2], vs[i].d[3]) / 100;
        for (int x = sx; x < split && x <= ex; x++) {
          for (int y = sy; y < split && y <= ey; y++) {
            area[x + split * y].push_back(vs[i]);
          }
        }
      }
    }
    for (int x = 0; x < split; x++) {
      for (int y = 0; y < split; y++) {
        if (area[x + split * y].size()) {
          sort(area[x + split * y].begin(), area[x + split * y].end(),
               [](v a, v b) {
                 return min(a.d[0] + a.d[2], a.d[1] + a.d[3]) >
                        min(b.d[0] + b.d[2], b.d[1] + b.d[3]);
               });
          if (x > 0)
            area[(x - 1) + split * y].push_back(area[x + split * y][0]);
          if (y > 0)
            area[x + split * (y - 1)].push_back(area[x + split * y][0]);
          if (x * y > 0)
            area[(x - 1) + split * (y - 1)].push_back(area[x + split * y][0]);
        }
      }
    }
    for (int i = 0; i < area.size(); i++) {
      mn = min(mn, chk(area[i]));
    }
  }
  cout.precision(12);
  cout << sqrt(mn) << endl;
}