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

#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <vector>
#define all(n) n.begin(), n.end()
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define S second
#define pb push_back
#define INF (1 << 27)
#define EPS 1e-10
#define RANGE(a) (0 <= a && a <= 5000)

using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef unsigned long long ull;
typedef long long ll;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
int N;
vector<P> vec;
bool exist[5001][5001];

int is_rectangle(int, int);

int main() {

  while (true) {
    int mex = 0;

    cin >> N;
    if (N == 0)
      break;
    rep(i, 5001) rep(j, 5001) exist[i][j] = false;
    vec.clear();

    rep(i, N) {
      int a, b;
      cin >> a >> b;

      vec.pb(P(a, b));
      exist[a][b] = true;
    }

    rep(i, N) {
      rep(j, N) {

        if (i == j)
          continue;
        int vol = is_rectangle(i, j);
        if (vol) {
          mex = max(mex, vol);
        }
      }
    }

    cout << mex << endl;
  }

  return 0;
}

int is_rectangle(int i, int j) {

  P p, q;
  int vlm, dif_x, dif_y;

  dif_x = vec[j].F - vec[i].F, dif_y = vec[j].S - vec[i].S;
  p.F = vec[i].F - dif_y;
  p.S = vec[i].S + dif_x;
  q.F = p.F + dif_x;
  q.S = p.S + dif_y;

  if ((RANGE(p.F) && RANGE(p.S) && RANGE(q.F) && RANGE(q.S)) &&
      exist[p.F][p.S] && exist[q.F][q.S]) {
    int x, y;
    x = vec[i].F - vec[j].F, y = vec[i].S - vec[j].S;

    return x * x + y * y;
  }
  p.F = vec[i].F + dif_y;
  p.S = vec[i].S - dif_x;
  q.F = p.F - dif_x;
  q.S = p.S - dif_y;
  if ((RANGE(p.F) && RANGE(p.S) && RANGE(q.F) && RANGE(q.S)) &&
      exist[p.F][p.S] && exist[q.F][q.S]) {
    int x, y;
    x = vec[i].F - vec[j].F, y = vec[i].S - vec[j].S;

    return x * x + y * y;
  }
  return -1;
}

Error: MLE

Fixed submission:

#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <vector>
#define all(n) n.begin(), n.end()
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define S second
#define pb push_back
#define INF (1 << 27)
#define EPS 1e-10
#define mypow2(a) (a * a)

using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef unsigned long long ull;
typedef long long ll;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
int N;
vector<P> vec;
map<P, bool> exist;

int is_rectangle(int, int);
double dist(P, P);

int main() {

  while (true) {
    int mex = 0;

    cin >> N;
    if (N == 0)
      break;
    vec.clear();
    exist.clear();
    rep(i, N) {
      int a, b;
      cin >> a >> b;
      vec.pb(P(a, b));
      exist[P(a, b)] = true;
    }
    rep(i, N) {
      rep(j, N) {
        if (i == j)
          continue;
        int vol = is_rectangle(i, j);
        if (vol) {
          mex = max(mex, vol);
        }
      }
    }

    cout << mex << endl;
  }

  return 0;
}

int is_rectangle(int i, int j) {

  P p, q;
  int vlm, dif_x, dif_y;

  dif_x = vec[j].F - vec[i].F, dif_y = vec[j].S - vec[i].S;
  p.F = vec[i].F - dif_y;
  p.S = vec[i].S + dif_x;
  q.F = p.F + dif_x;
  q.S = p.S + dif_y;
  if (exist[p] && exist[q]) {
    int x, y;
    x = vec[i].F - vec[j].F, y = vec[i].S - vec[j].S;
    return x * x + y * y;
  }
  p.F = vec[i].F + dif_y;
  p.S = vec[i].S - dif_x;
  q.F = p.F - dif_x;
  q.S = p.S - dif_y;
  if (exist[p] && exist[q]) {
    int x, y;
    x = vec[i].F - vec[j].F, y = vec[i].S - vec[j].S;

    return x * x + y * y;
  }
  return -1;
}

double dist(P a, P b) {
  return sqrt(pow((double)(a.F - b.F), 2) + pow((double)(a.S - b.S), 2));
}