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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

pint pillers[3010];
vint spa[5010];

int main() {
  int n;
  while (n = in()) {
    memset(pillers, 0, sizeof(pillers));
    int i, j;
    for (i = 0; i < 5010; i++)
      spa[i].clear();
    for (i = 0; i < n; i++) {
      cin >> pillers[i].first >> pillers[i].second;
      spa[pillers[i].first].push_back(pillers[i].second);
    }
    for (i = 0; i < 5010; i++)
      sort(spa[i].begin(), spa[i].end());
    int mx = 0;
    for (i = 0; i < n; i++) {
      for (j = i + 1; j < n; j++) {
        if (i == j)
          continue;
        int x = pillers[i].first, y = pillers[i].second;
        int xx = pillers[j].first, yy = pillers[j].second;
        if (0 <= x + (y - yy) && x + (y - yy) <= 5000 && 0 <= xx + (y - yy) &&
            xx + (y - yy) <= 5000 &&
            find(spa[x + (y - yy)].begin(), spa[x + (y - yy)].end(),
                 y + (xx - x)) != spa[x + (y - yy)].end() &&
            find(spa[xx + (y - yy)].begin(), spa[xx + (y - yy)].end(),
                 yy + (xx - x)) != spa[xx + (y - yy)].end()) {
          // cout<<i<<" "<<j<<endl;
          chmax(mx, (x - xx) * (x - xx) + (y - yy) * (y - yy));
        }
      }
    }
    cout << mx << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

pint pillers[3010];
int spa[5010][5010];

int main() {
  int n;
  while (n = in()) {
    memset(pillers, 0, sizeof(pillers));
    memset(spa, 0, sizeof(spa));
    int i, j;
    for (i = 0; i < n; i++) {
      cin >> pillers[i].first >> pillers[i].second;
      spa[pillers[i].first][pillers[i].second] = 1;
    }
    int mx = 0;
    for (i = 0; i < n; i++) {
      for (j = i + 1; j < n; j++) {
        if (i == j)
          continue;
        int x = pillers[i].first, y = pillers[i].second;
        int xx = pillers[j].first, yy = pillers[j].second;
        if (0 <= x + (y - yy) && x + (y - yy) <= 5000 && 0 <= y + (xx - x) &&
            y + (xx - x) <= 5000 && 0 <= xx + (y - yy) &&
            xx + (y - yy) <= 5000 && 0 <= yy + (xx - x) &&
            yy + (xx - x) <= 5000 && spa[x + (y - yy)][y + (xx - x)] &&
            spa[xx + (y - yy)][yy + (xx - x)]) {
          // cout<<i<<" "<<j<<endl;
          chmax(mx, (x - xx) * (x - xx) + (y - yy) * (y - yy));
        }
      }
    }
    cout << mx << endl;
  }
  return 0;
}