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

#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;

class range {
private:
  struct Iterator {
    int val;
    int operator*() { return val; }
    bool operator!=(Iterator &itr) { return val < itr.val; }
    void operator++() { ++val; }
  };
  Iterator i, n;

public:
  range(int n) : i({0}), n({n}) {}
  range(int i, int n) : i({i}), n({n}) {}
  Iterator &begin() { return i; }
  Iterator &end() { return n; }
};

#define X real()
#define Y imag()

typedef int Real;
typedef complex<Real> Point;

double nearestTwoPoints(vector<Point> &vp) {
  random_shuffle(vp.begin(), vp.end());
  double d = sqrt((double)norm(vp[0] - vp[1]));
  unordered_map<int, vector<unsigned short>> mp;
  for (int i : range(2))
    mp[(int)floor(vp[i].X / d) * 76647 + (int)floor(vp[i].Y / d)].emplace_back(
        i);
  for (int i : range(2, vp.size())) {
    double dd = 1e9;
    int x = floor(vp[i].X / d), y = floor(vp[i].Y / d);
    for (int xx : range(x - 1, x + 2))
      for (int yy : range(y - 1, y + 2)) {
        for (int j : mp[(int)xx * 76647 + (int)yy])
          dd = min(dd, sqrt((double)norm(vp[i] - vp[j])));
      }
    if (dd < d) {
      d = dd;
      mp.clear();
      for (int j : range(i + 1))
        mp[(int)floor(vp[j].X / d) * 76647 + (int)floor(vp[j].Y / d)]
            .emplace_back(j);
    } else {
      mp[(int)x * 76647 + (int)y].emplace_back(i);
    }
  }
  return d;
}

int main() {
  int n;
  cin >> n;
  vector<Point> p(n);
  for (int i : range(n)) {
    Real x, y;
    cin >> x >> y;
    p[i] = Point(x, y);
  }
  double res = nearestTwoPoints(p);
  cout << (int)(res * res + 0.5) << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

class range {
private:
  struct Iterator {
    int val;
    int operator*() { return val; }
    bool operator!=(Iterator &itr) { return val < itr.val; }
    void operator++() { ++val; }
  };
  Iterator i, n;

public:
  range(int n) : i({0}), n({n}) {}
  range(int i, int n) : i({i}), n({n}) {}
  Iterator &begin() { return i; }
  Iterator &end() { return n; }
};

#define X real()
#define Y imag()

typedef int Real;
typedef complex<Real> Point;

double nearestTwoPoints(vector<Point> &vp) {
  random_shuffle(vp.begin(), vp.end());
  double d = sqrt((double)norm(vp[0] - vp[1]));
  map<int, vector<int>> mp;
  for (int i : range(2))
    mp[(int)floor(vp[i].X / d) * 65536 + (int)floor(vp[i].Y / d)].emplace_back(
        i);
  for (int i : range(2, vp.size())) {
    double dd = 1e9;
    int x = floor(vp[i].X / d), y = floor(vp[i].Y / d);
    for (int xx : range(x - 1, x + 2))
      for (int yy : range(y - 1, y + 2)) {
        for (int j : mp[(int)xx * 65536 + (int)yy])
          dd = min(dd, sqrt((double)norm(vp[i] - vp[j])));
      }
    if (dd < d) {
      d = dd;
      mp.clear();
      for (int j : range(i + 1))
        mp[(int)floor(vp[j].X / d) * 65536 + (int)floor(vp[j].Y / d)]
            .emplace_back(j);
    } else {
      mp[(int)x * 65536 + (int)y].emplace_back(i);
    }
  }
  return d;
}

int main() {
  int n;
  cin >> n;
  vector<Point> p;
  for (int i : range(n)) {
    Real x, y;
    cin >> x >> y;
    p.emplace_back(x, y);
  }
  double res = nearestTwoPoints(p);
  cout << (int)(res * res + 0.5) << endl;
}