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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long int mod = 1e9 + 7;
template <class T> void inputV(vector<T> &x, int n) {
  for (int i = 0; i < n; i++)
    cin >> x[i];
}
bool mp[5001][5001];

int func(pair<int, int> a, pair<int, int> b) {
  pair<int, int> c = {0, 0};
  c.first = a.first + abs(b.second - a.second);
  c.second = a.second + abs(b.first - a.first);

  pair<int, int> d = {0, 0};
  d.first = b.first + abs(b.second - a.second);
  d.second = b.second + abs(b.first - a.first);
  if (c.first < 0 || c.first > 5000 || c.second < 0 || c.second > 5000)
    return 0;
  if (d.first < 0 || d.first > 5000 || d.second < 0 || d.second > 5000)
    return 0;
  if (mp[c.first][c.second] && mp[d.first][d.second]) {
    // cerr << (a.first-b.first) << " " << (a.second-b.second) << endl;
    return (a.first - b.first) * (a.first - b.first) +
           (a.second - b.second) * (a.second - b.second);
  }
  return 0;
}

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    for (int i = 0; i <= 5000; i++)
      for (int j = 0; j <= 5000; j++)
        mp[i][j] = false;
    vector<pair<int, int>> p(n);
    for (int i = 0; i < n; i++) {
      cin >> p[i].first >> p[i].second;
      mp[p[i].first][p[i].second] = true;
    }

    int ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          continue;
        ans = max(ans, func(p[i], p[j]));
      }
    }
    cout << ans << endl;
  }

  return 0;
}


Error: 0
0 1
-3 1
0 -1
-1 0
3 -1
1 0
0 1
-3 1
0 -1
-1 0
3 -1
1 0


Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long int mod = 1e9 + 7;
template <class T> void inputV(vector<T> &x, int n) {
  for (int i = 0; i < n; i++)
    cin >> x[i];
}
bool mp[5001][5001];

int func(pair<int, int> a, pair<int, int> b) {
  pair<int, int> c = {0, 0};
  c.first = a.first + abs(b.second - a.second);
  c.second = a.second + abs(b.first - a.first);

  pair<int, int> d = {0, 0};
  d.first = b.first + abs(b.second - a.second);
  d.second = b.second + abs(b.first - a.first);
  if (c.first < 0 || c.first > 5000 || c.second < 0 || c.second > 5000)
    return 0;
  if (d.first < 0 || d.first > 5000 || d.second < 0 || d.second > 5000)
    return 0;
  if (mp[c.first][c.second] && mp[d.first][d.second]) {
    cerr << (a.first - b.first) << " " << (a.second - b.second) << endl;
    return (a.first - b.first) * (a.first - b.first) +
           (a.second - b.second) * (a.second - b.second);
  }
  return 0;
}

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    for (int i = 0; i <= 5000; i++)
      for (int j = 0; j <= 5000; j++)
        mp[i][j] = false;
    vector<pair<int, int>> p(n);
    for (int i = 0; i < n; i++) {
      cin >> p[i].first >> p[i].second;
      mp[p[i].first][p[i].second] = true;
    }

    int ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          continue;
        ans = max(ans, func(p[i], p[j]));
      }
    }
    cout << ans << endl;
  }

  return 0;
}