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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
bool pillar[5050][5050];

int main() {
  int a;
  while (cin >> a, a) {
    vector<P> v;
    for (int i = 0; i < 5005; i++)
      for (int j = 0; j < 5005; j++)
        pillar[i][j] = false;

    for (int i = 0; i < a; i++) {
      int b, c;
      cin >> b >> c;
      v.push_back(P(b, c));
      pillar[b][c] = true;
    }

    int ans = 0;

    for (int i = 0; i < a; i++) {
      for (int j = 0; j < a; j++) {
        if (i == j)
          continue;
        int dx = v[i].first - v[j].first;
        int dy = v[i].second - v[j].second;

        if (dx >= 0 && dy >= 0) {
          if (v[i].first + dy >= 0 && v[i].second - dx >= 0 &&
              v[i].first + dy <= 5002 && v[i].second - dx <= 5002) {
            if (v[j].first + dy >= 0 && v[j].second - dx >= 0 &&
                v[j].first + dy <= 5002 && v[j].second - dx <= 5002) {
              if (pillar[v[i].first + dy][v[i].second - dx]) {
                if (pillar[v[j].first + dy][v[j].second - dx]) {
                  /*cout<<v[i].first<<' '<<v[i].second<<endl;
                  cout<<v[j].first<<' '<<v[j].second<<endl;
                  cout<<v[i].first-dx+dy<<' '<<v[i].second-dx-dy<<endl;
                  cout<<v[j].first+dy<<' '<<v[j].second-dx<<endl;*/
                  ans = max(ans, ((dx * dx) + (dy * dy)));
                }
              }
            }
          }

          if (v[i].first - dy >= 0 && v[i].second + dx >= 0 &&
              v[i].first - dy <= 5002 && v[i].second + dx <= 5002) {
            if (v[j].first - dy >= 0 && v[j].second + dx >= 0 &&
                v[j].first - dy <= 5002 && v[j].second + dx <= 5002) {
              if (pillar[v[i].first - dy][v[i].second + dx]) {
                if (pillar[v[j].first - dy][v[j].second + dx]) {
                  /*	cout<<v[i].first<<' '<<v[i].second<<endl;
                          cout<<v[j].first<<' '<<v[j].second<<endl;
                          cout<<v[i].first-dx-dy<<' '<<v[i].second-dx+dy<<endl;
                          cout<<v[j].first-dy<<' '<<v[j].second+dx<<endl;*/
                  ans = max(ans, ((dx * dx) + (dy * dy)));
                }
              }
            }
          }
        }
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
bool pillar[5050][5050];

int main() {
  int a;
  while (cin >> a, a) {
    vector<P> v;
    for (int i = 0; i < 5005; i++)
      for (int j = 0; j < 5005; j++)
        pillar[i][j] = false;

    for (int i = 0; i < a; i++) {
      int b, c;
      cin >> b >> c;
      v.push_back(P(b, c));
      pillar[b][c] = true;
    }
    sort(v.begin(), v.end());

    int ans = 0;
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < a; j++) {
        if (i >= j)
          continue;

        int dx = v[i].first - v[j].first;
        int dy = v[i].second - v[j].second;

        if (v[i].first - dx + dy >= 0 && v[i].second - dx - dy >= 0 &&
            v[i].first - dx + dy <= 5002 && v[i].second - dx - dy <= 5002) {
          if (v[j].first + dy >= 0 && v[j].second - dx >= 0 &&
              v[j].first + dy <= 5002 && v[j].second - dx <= 5002) {
            if (pillar[v[i].first - dx + dy][v[i].second - dx - dy]) {
              if (pillar[v[j].first + dy][v[j].second - dx]) {
                ans = max(ans, ((dx * dx) + (dy * dy)));
              }
            }
          }
        }

        if (v[i].first - dx - dy >= 0 && v[i].second - dx + dy >= 0 &&
            v[i].first - dx - dy <= 5002 && v[i].second - dx + dy <= 5002) {
          if (v[j].first - dy >= 0 && v[j].second + dx >= 0 &&
              v[j].first - dy <= 5002 && v[j].second + dx <= 5002) {
            if (pillar[v[i].first - dx + dy][v[i].second - dx + dy]) {
              if (pillar[v[j].first - dy][v[j].second + dx]) {
                ans = max(ans, ((dx * dx) + (dy * dy)));
              }
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}