Problem p00441 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toStr(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;

bool existPt[5001][5001];

bool validSuffix(int x) { return !(x < 0 || 5000 < x); }

bool existPillar(pii a, pii b) {
  int dx = b.first - a.first, dy = b.second - a.second;
  if (validSuffix(a.first + dy) && validSuffix(a.second - dx) &&
      validSuffix(b.first + dy) && validSuffix(b.second - dx)) {
    return existPt[a.first + dy][a.second - dx] &&
           existPt[b.first + dy][b.second - dx];
  } else {
    return false;
  }
}

int area(pii a, pii b) {
  int dx = b.first - a.first, dy = b.second - a.second;
  return dx * dx + dy * dy;
}

int main() {
  int n;
  while (cin >> n, n) {
    memset(existPt, false, sizeof(existPt));
    int x, y;
    vector<pii> pts(n);
    REP(i, n) {
      cin >> x >> y;
      pts[i] = pii(x, y);
      existPt[x][y] = true;
    }

    int ans = 0;
    REP(i, n) {
      REP(j, n) {
        if (existPillar(pts[i], pts[j])) {
          ans = max(ans, area(pts[i], pts[j]));
        }
      }
    }

    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toStr(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;

#include <complex>
typedef complex<double> P;
#define EQ(x, y) (abs((x) - (y)) < EPS)

P rotate(P t, P p, double r) {
  double ta = cos(r) * (t.real() - p.real()) - sin(r) * (t.imag() - p.imag()) +
              p.real();
  double tb = sin(r) * (t.real() - p.real()) + cos(r) * (t.imag() - p.imag()) +
              p.imag();
  return P(ta, tb);
}

P mid(P a, P b) {
  return P((a.real() + b.real()) / 2, (a.imag() + b.imag()) / 2);
}

bool existPt[5001][5001];

bool isPillar(P a) {
  int far = floor(a.real()), fai = floor(a.imag());
  if (!EQ(a.real(), far) || !EQ(a.imag(), fai)) {
    return false;
  }
  return existPt[far][fai];
}

bool isSquare(P a, P b) {
  P midpoint = mid(a, b);
  P pa = rotate(a, midpoint, PI / 2), pb = rotate(b, midpoint, PI / 2);
  return (isPillar(pa) && isPillar(pb));
}

int area(P a, P b) { return abs(a - b) * abs(a - b) / 2; }

int main() {
  int n;
  while (cin >> n, n) {
    memset(existPt, false, sizeof(existPt));
    int x, y;
    vector<P> pts(n);
    REP(i, n) {
      cin >> x >> y;
      pts[i] = P(x, y);
      existPt[x][y] = true;
    }

    int ans = 0;
    REP(i, n) {
      FOR(j, i + 1, n - 1) {
        if (isSquare(pts[i], pts[j])) {
          ans = max(ans, area(pts[i], pts[j]));
        }
      }
    }

    cout << ans << endl;
  }
}