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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;
using ll = long long;
const ll INF = 1e9;

bool M[5001][5001];
int x[3001], y[3001];

int main() {
  int N;
  while (cin >> N, N) {
    for (int i = 0; i < N; i++)
      for (int k = 0; k < N; k++)
        M[k][i] = false;
    for (int i = 0; i < N; i++) {
      cin >> x[i] >> y[i];
      M[x[i]][y[i]] = true;
    }

    ll ans = 0;
    for (int i = 0; i < N; i++) {
      for (int j = i; j < N; j++) {
        if (i == j)
          continue;
        if (x[i] > x[j])
          continue;

        int dx = x[j] - x[i], dy = y[j] - y[i];
        int kx = x[j] - dy;
        int ky = y[j] + dx;
        //                cout << x[i] << " " << y[i] << " ";
        //                cout << x[j] << " " << y[j] << " ";
        //                cout << kx << " " << ky << endl;
        if (kx < 0 || ky < 0 || kx > 5000 || ky > 5000)
          continue;
        if (M[kx][ky]) { // k
          int lx = kx - dx;
          int ly = ky - dy;
          if (lx < 0 || ly < 0 || lx > 5000 || ly > 5000)
            continue;
          if (M[lx][ly]) { // l
            //                        cout << x[i] << " " << y[i] << endl;
            //                        cout << x[j] << " " << y[j] << endl;
            //                        cout << kx << " " << ky << endl;
            //                        cout << lx << " " << ly << endl;
            ll w = max(kx - x[i], ky - y[i]);
            //                        cout << w << endl;
            ll area = w * w - 2 * abs(dx) * abs(dy);
            ans = max(ans, area);
          }
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;
using ll = long long;
const ll INF = 1e9;

bool M[5001][5001];

int main() {
  int N;
  while (cin >> N, N) {
    int x[N], y[N];
    for (int i = 0; i < N; i++)
      for (int k = 0; k < N; k++)
        M[k][i] = false;
    for (int i = 0; i < N; i++) {
      cin >> x[i] >> y[i];
      M[x[i]][y[i]] = true;
    }

    ll ans = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (i == j)
          continue;
        if (x[i] > x[j])
          continue;

        int dx = x[j] - x[i], dy = y[j] - y[i];
        int kx = x[j] - dy;
        int ky = y[j] + dx;
        //                cout << x[i] << " " << y[i] << " ";
        //                cout << x[j] << " " << y[j] << " ";
        //                cout << kx << " " << ky << endl;
        if (kx < 0 || ky < 0)
          continue;
        if (M[kx][ky]) { // k
          int lx = kx - dx;
          int ly = ky - dy;
          if (lx < 0 || ly < 0)
            continue;
          if (M[lx][ly]) { // l
            //                        cout << x[i] << " " << y[i] << endl;
            //                        cout << x[j] << " " << y[j] << endl;
            //                        cout << kx << " " << ky << endl;
            //                        cout << lx << " " << ly << endl;
            ll w = kx - x[i];
            //                        cout << w << endl;
            ll area = w * w - 2 * abs(dx) * abs(dy);
            ans = max(ans, area);
          }
        }
      }
    }
    cout << ans << endl;
  }

  //                for (int k = 0; k < N; k++) {
  //                    if (x[k] == x[j] - dy && y[k] == y[j] + dx) {
  //                        for (int l = 0; l < N; l++) {
  //                            if (k == l || j == l || i == l) continue;
  //                            if (x[l] == x[k] - dy && y[l] == y[k] - dx) {
  //                                ll area = (x[k] - x[i]) * (x[k] - x[i]) - 4
  //                                * abs(dx) * abs(dy); ans = max(ans, area);
  //                            }
  //                        }
  //                    }
  //                }
  //            }
  //        }

  return 0;
}