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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

// int dy[]={0, 0, 1, -1, 0};
// int dx[]={1, -1, 0, 0, 0};

int main() {
  while (1) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      return 0;
    vector<P> v(n);
    vector<vector<bool>> b(10010, vector<bool>(10010, 0));
    REP(i, n) {
      scanf("%d%d", &v[i].first, &v[i].second);
      b[v[i].first][v[i].second] = 1;
    }

    int ans = 0;
    REP(i, n) {
      FOR(j, i + 1, n) {
        int x = v[i].first, y = v[i].second;
        int x2 = v[j].first, y2 = v[j].second;
        int xd = x - x2;
        int yd = y - y2;

        if ((x + yd >= 0 && y - xd >= 0 && x2 + yd >= 0 && y2 - xd >= 0) &&
            (b[x + yd][y - xd] && b[x2 + yd][y2 - xd])) {
          ans = max(ans, xd * xd + yd * yd);
        }
        if ((x - yd >= 0 && y + xd >= 0 && x2 - yd >= 0 && y2 + xd >= 0) &&
            (b[x - yd][y + xd] && b[x2 - yd][y2 + xd])) {
          ans = max(ans, xd * xd + yd * yd);
        }
      }
    }

    printf("%d\\n", ans);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

// int dy[]={0, 0, 1, -1, 0};
// int dx[]={1, -1, 0, 0, 0};

int main() {
  int n;
  scanf("%d", &n);
  vector<P> v(n);
  vector<vector<bool>> b(100000, vector<bool>(100000, 0));
  REP(i, n) {
    scanf("%d%d", &v[i].first, &v[i].second);
    b[v[i].first][v[i].second] = 1;
  }

  int ans = 0;
  REP(i, n) {
    FOR(j, i + 1, n) {
      int x = v[i].first, y = v[i].second;
      int x2 = v[j].first, y2 = v[j].second;
      int xd = x - x2;
      int yd = y - y2;

      if ((x + yd >= 0 && y - xd >= 0 && x2 + yd >= 0 && y2 - xd >= 0) &&
          (b[x + yd][y - xd] && b[x2 + yd][y2 - xd])) {
        ans = max(ans, xd * xd + yd * yd);
      }
      if ((x - yd >= 0 && y + xd >= 0 && x2 - yd >= 0 && y2 + xd >= 0) &&
          (b[x - yd][y + xd] && b[x2 - yd][y2 + xd])) {
        ans = max(ans, xd * xd + yd * yd);
      }
    }
  }

  printf("%d\\n", ans);
}