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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define INF 1 << 30
#define LINF 1LL << 60

pll ps[3001];
bool m[5001][5001];

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  while (cin >> N, N) {
    for (int i = 0; i < 5001; i++)
      for (int j = 0; j < 5001; j++)
        m[i][j] = false;
    vector<pll> ps(N);
    for (int i = 0; i < N; i++) {
      cin >> ps[i].first >> ps[i].second;
      m[ps[i].first][ps[i].second] = true;
    }
    ll res = 0;
    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
        ll x1 = ps[i].first, y1 = ps[i].second, x2 = ps[j].first,
           y2 = ps[j].second;

        ll area = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
        if (area <= res)
          continue;
        ll x3 = x1 + (y1 - y2), y3 = y1 + (x2 - x1);
        if (x3 >= 0 && x3 <= 5000 && y3 >= 0 && y3 <= 5000) {
          if (m[x3][y3]) {
            ll x4 = x2 + (x3 - x1), y4 = y2 + (y3 - y1);
            if (x4 >= 0 && x4 <= 5000 && y4 >= 0 && y4 <= 5000) {
              if (m[x4][y4]) {
                res = area;
                continue;
              }
            }
          }
        }
        x3 = x1 - (y1 - y2), y3 = y1 - (x2 - x1);
        if (x3 >= 0 && x3 <= 5000 && y3 >= 0 && y3 <= 5000) {
          if (m[x3][y3]) {
            ll x4 = x2 + (x3 - x1), y4 = y2 + (y3 - y1);
            if (x4 >= 0 && x4 <= 5000 && y4 >= 0 && y4 <= 5000) {
              if (m[x4][y4]) {
                res = area;
                continue;
              }
            }
          }
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define INF 1 << 30
#define LINF 1LL << 60

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  while (cin >> N, N) {
    vector<pll> ps(N);
    for (int i = 0; i < N; i++) {
      cin >> ps[i].first >> ps[i].second;
    }

    ll res = 0;
    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
        ll x1 = ps[i].first, y1 = ps[i].second, x2 = ps[j].first,
           y2 = ps[j].second;

        ll area = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
        if (area <= res)
          continue;
        ll x3 = x1 + (y1 - y2), y3 = y1 + (x2 - x1);
        if (x3 >= 0 && x3 <= 5000 && y3 >= 0 && y3 <= 5000) {
          if (find(ps.begin(), ps.end(), make_pair(x3, y3)) != ps.end()) {
            ll x4 = x2 + (x3 - x1), y4 = y2 + (y3 - y1);
            if (x4 >= 0 && x4 <= 5000 && y4 >= 0 && y4 <= 5000) {
              if (find(ps.begin(), ps.end(), make_pair(x4, y4)) != ps.end()) {
                res = area;
                continue;
              }
            }
          }
        }
        x3 = x1 - (y1 - y2), y3 = y1 - (x2 - x1);
        if (x3 >= 0 && x3 <= 5000 && y3 >= 0 && y3 <= 5000) {
          if (find(ps.begin(), ps.end(), make_pair(x3, y3)) != ps.end()) {
            ll x4 = x2 + (x3 - x1), y4 = y2 + (y3 - y1);
            if (x4 >= 0 && x4 <= 5000 && y4 >= 0 && y4 <= 5000) {
              if (find(ps.begin(), ps.end(), make_pair(x4, y4)) != ps.end()) {
                res = area;
                continue;
              }
            }
          }
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}