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

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;
#define MAX_X 5001
#define MAX_Y 5001

bool exists[MAX_Y][MAX_X];

struct Point {
  int x, y;
  Point(int x = 0, int y = 0) : x(x), y(y) {}
  Point operator+(const Point &o) const { return Point(x + o.x, y + o.y); }
  Point operator-(const Point &o) const { return Point(x - o.x, y - o.y); }
};

Point rotate_right(const Point &a) { return Point(a.y, -a.x); }

Point rotate_left(const Point &a) { return Point(-a.y, a.x); }

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    Point pole[n];
    memset(exists, 0, sizeof(exists));
    for (int i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      exists[y][x] = true;
      pole[i] = Point(x, y);
    }
    int max_area = 0;
    for (int i = 0; i < n; i++) {
      //			cout << "i: " << i << endl;
      for (int j = 0; j < i; j++) {
        Point p = pole[j] + rotate_right(pole[i] - pole[j]);
        if (0 <= p.y && p.y < MAX_Y && 0 <= p.x && p.x < MAX_X &&
            exists[p.y][p.x]) {
          Point q = p + rotate_right(pole[j] - p);
          if (0 <= q.y && q.y < MAX_Y && 0 <= q.x && q.x < MAX_X &&
              exists[q.y][q.x]) {
            max_area =
                max((p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y),
                    max_area);
          }
        }

        p = rotate_left(pole[i] - pole[j]);
        if (0 <= p.y && p.y < MAX_Y && 0 <= p.x && p.x < MAX_X &&
            exists[p.y][p.x]) {
          Point q = p + rotate_left(pole[j] - p);
          if (0 <= q.y && q.y < MAX_Y && 0 <= q.x && q.x < MAX_X &&
              exists[q.y][q.x]) {
            max_area =
                max((p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y),
                    max_area);
          }
        }
      }
    }
    cout << max_area << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;
#define MAX_X 5001
#define MAX_Y 5001

bool exists[MAX_Y][MAX_X];

struct Point {
  int x, y;
  Point(int x = 0, int y = 0) : x(x), y(y) {}
  Point operator+(const Point &o) const { return Point(x + o.x, y + o.y); }
  Point operator-(const Point &o) const { return Point(x - o.x, y - o.y); }
};

Point rotate_right(const Point &a) { return Point(a.y, -a.x); }

Point rotate_left(const Point &a) { return Point(-a.y, a.x); }

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    Point pole[n];
    memset(exists, 0, sizeof(exists));
    for (int i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      exists[y][x] = true;
      pole[i] = Point(x, y);
    }
    int max_area = 0;
    for (int i = 0; i < n; i++) {
      //			cout << "i: " << i << endl;
      for (int j = 0; j < i; j++) {
        Point p = pole[j] + rotate_right(pole[i] - pole[j]);
        if (0 <= p.y && p.y <= MAX_Y && 0 <= p.x && p.x <= MAX_X &&
            exists[p.y][p.x]) {
          Point q = p + rotate_right(pole[j] - p);
          if (0 <= q.y && q.y <= MAX_Y && 0 <= q.x && q.x <= MAX_X &&
              exists[q.y][q.x]) {
            max_area =
                max((p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y),
                    max_area);
          }
        }

        p = rotate_left(pole[i] - pole[j]);
        if (0 <= p.y && p.y <= MAX_Y && 0 <= p.x && p.x <= MAX_X &&
            exists[p.y][p.x]) {
          Point q = p + rotate_left(pole[j] - p);
          if (0 <= q.y && q.y <= MAX_Y && 0 <= q.x && q.x <= MAX_X &&
              exists[q.y][q.x]) {
            max_area =
                max((p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y),
                    max_area);
          }
        }
      }
    }
    cout << max_area << endl;
  }
  return 0;
}