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

#include <algorithm>
#include <complex>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main() {
  int n;

  cin >> n;

  while (n != 0) {
    int ans = 0;
    vector<pair<int, int>> colp;
    set<pair<int, int>> cols;

    int x, y, temp;

    for (int i = 0; i < n; ++i) {
      cin >> x >> y;
      colp.push_back({x, y});
      cols.insert({x, y});
    }

    sort(colp.begin(), colp.end());

    for (int i = 0; i < n - 1; ++i) {
      for (int j = i + 1; j < n; ++j) {
        temp =
            (colp[i].first - colp[j].first) * (colp[i].first - colp[j].first) +
            (colp[i].second - colp[j].second) *
                (colp[i].second - colp[j].second);
        if (temp < ans)
          continue;

        complex<int> v[4];
        complex<int> rot(0, 1);

        v[0] = complex<int>(colp[i].first, colp[i].second);
        v[1] = complex<int>(colp[j].first, colp[j].second);

        v[2] = v[0] + (v[1] - v[0]) * rot;

        if (v[2].real() < 0 || v[2].imag() < 0)
          continue;

        v[3] = v[1] + v[2] - v[0];
        if (v[3].real() < 0 || v[3].imag() < 0)
          continue;

        if (cols.count({v[2].real(), v[2].imag()}) != 0 &&
            cols.count({v[3].real(), v[3].imag()}) != 0) {
          ans = temp;
          continue;
        }

        rot = complex<int>(0, -1);

        v[2] = v[0] + (v[1] - v[0]) * rot;

        if (v[2].real() < 0 || v[2].imag() < 0)
          continue;

        v[3] = v[1] + v[2] - v[0];
        if (v[3].real() < 0 || v[3].imag() < 0)
          continue;

        if (cols.count({v[2].real(), v[2].imag()}) != 0 &&
            cols.count({v[3].real(), v[3].imag()}) != 0) {
          ans = temp;
        }
      }
    }
    cout << ans << endl;
    cols.clear();
    colp.clear();
    cin >> n;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <set>
#include <vector>

using namespace std;

int dist2(pair<int, int> a, pair<int, int> b) {
  return (a.first - b.first) * (a.first - b.first) +
         (a.second - b.second) * (a.second - b.second);
}

int main() {
  int n;
  int x, y, ans;
  vector<pair<int, int>> colp;
  set<int> cols[5001];

  cin >> n;

  while (n != 0) {
    ans = 0;

    for (int i = 0; i < n; ++i) {
      cin >> x >> y;
      colp.push_back({x, y});
      cols[x].insert(y);
    }

    for (int i = 0; i < n - 1; ++i) {
      for (int j = i + 1; j < n; ++j) {
        int temp = dist2(colp[i], colp[j]);
        if (temp < ans)
          break;

        pair<int, int> v[4];
        v[0] = colp[i];
        v[1] = colp[j];

        v[2].first = v[0].first + (v[0].second - v[1].second);
        v[2].second = v[0].second + (-v[0].first + v[1].first);
        if (v[2].first < 0 || v[2].second < 0)
          break;

        v[3].first = v[1].first + v[2].first - v[0].first;
        v[3].second = v[1].second + v[2].second - v[0].second;
        if (v[3].first < 0 || v[3].second < 0)
          break;

        if (cols[v[2].first].count(v[2].second) != 0 &&
            cols[v[3].first].count(v[3].second) != 0) {
          ans = temp;
        }
      }
    }
    cout << ans << endl;

    for (int i = 0; i < 5001; ++i) {
      cols[i].clear();
    }
    colp.clear();

    cin >> n;
  }
  return 0;
}