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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
pii point[3010];
bool ex[5010][5010];
int main() {
  int n;
  while (cin >> n, n) {
    if (n == 0)
      break;
    rep(i, 5010) {
      rep(j, 5010) { ex[i][j] = false; }
    }
    memset(point, 0, sizeof(point));
    for (int i = 0; i < n; i++) {
      cin >> point[i].first >> point[i].second;
      ex[point[i].second][point[i].first] = true;
    }
    sort(point, point + n);
    /*rep(i,n){
            printf("(x,y) =(%d,%d)\\n",point[i].first,point[i].second);
    }*/
    int ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          continue;
        int dx = abs(point[j].first - point[i].first);
        int dy = abs(point[j].second - point[i].second);
        int tmp = dx * dx + dy * dy;
        if (tmp <= ans)
          continue;
        /*printf("(x1,y1) =(%d,%d)\\n",point[i].first,point[i].second);
        printf("(x2,y2) =(%d,%d)\\n",point[j].first,point[j].second);
        printf("(dx,dy) = (%d,%d)\\n",dx,dy);*/
        int nx1 = point[j].first + dy;
        int ny1 = point[j].second + dx;

        int nx2 = point[i].first + dy;
        int ny2 = point[i].second + dx;
        //
        int mnx1 = (point[j].first - dy);
        int mny1 = (point[j].second - dx);

        int mnx2 = (point[i].first - dy);
        int mny2 = (point[i].second - dx);
        /*printf("(nx1,ny1) =(%d,%d)\\n",nx1,ny1);
        printf("(nx2,ny2) =(%d,%d)\\n",nx2,ny2);
        printf("(mnx1,mny2) =(%d,%d)\\n",mnx1,mny1);
        printf("(mnx1,mny2) =(%d,%d)\\n",mnx2,mny2);
        cout <<endl;*/
        if (nx1 <= 5000 && ny1 <= 5000 && nx2 <= 5000 && ny2 <= 5000 &&
            ex[ny1][nx1] && ex[ny2][nx2]) {
          ans = max(ans, tmp);
          // cout <<ans<<endl;
          continue;
        }
        if (mnx1 >= 0 && mny1 >= 0 && mnx2 >= 0 && mny2 >= 0 &&
            ex[mny1][mnx1] && ex[mny2][mnx2]) {
          ans = max(ans, tmp);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
pii point[3010];
bool ex[5010][5010];
int main() {
  int n;
  while (cin >> n, n) {
    if (n == 0)
      return 0;
    rep(i, 5010) {
      point[i].first = 0;
      point[i].second = 0;
      rep(j, 5010) { ex[i][j] = false; }
    }
    for (int i = 0; i < n; i++) {
      cin >> point[i].first >> point[i].second;
      ex[point[i].second][point[i].first] = true;
    }
    sort(point, point + n);
    /*rep(i,n){
            printf("(x,y) =(%d,%d)\\n",point[i].first,point[i].second);
    }*/
    int ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          continue;
        int dx = abs(point[j].first - point[i].first);
        int dy = abs(point[j].second - point[i].second);
        int tmp = dx * dx + dy * dy;
        if (tmp <= ans)
          continue;
        /*printf("(x1,y1) =(%d,%d)\\n",point[i].first,point[i].second);
        printf("(x2,y2) =(%d,%d)\\n",point[j].first,point[j].second);
        printf("(dx,dy) = (%d,%d)\\n",dx,dy);*/
        int nx1 = point[j].first + dy;
        int ny1 = point[j].second + dx;

        int nx2 = point[i].first + dy;
        int ny2 = point[i].second + dx;
        //
        int mnx1 = (point[j].first - dy);
        int mny1 = (point[j].second - dx);

        int mnx2 = (point[i].first - dy);
        int mny2 = (point[i].second - dx);
        /*printf("(nx1,ny1) =(%d,%d)\\n",nx1,ny1);
        printf("(nx2,ny2) =(%d,%d)\\n",nx2,ny2);
        printf("(mnx1,mny2) =(%d,%d)\\n",mnx1,mny1);
        printf("(mnx1,mny2) =(%d,%d)\\n",mnx2,mny2);
        cout <<endl;*/
        if (nx1 <= 5000 && ny1 <= 5000 && nx2 <= 5000 && ny2 <= 5000 &&
            ex[ny1][nx1] && ex[ny2][nx2]) {
          ans = max(ans, tmp);
          // cout <<ans<<endl;
          continue;
        }
        if (mnx1 >= 0 && mny1 >= 0 && mnx2 >= 0 && mny2 >= 0 &&
            ex[mny1][mnx1] && ex[mny2][mnx2]) {
          ans = max(ans, tmp);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}