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

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)

const int INF = 1 << 24;

typedef unsigned long long ull;

struct P {
  int x, y;
};

P operator+(const P &lp, const P &rp) { return {lp.x + rp.x, lp.y + rp.y}; }

bool operator==(const P &lp, const P &rp) {
  return lp.x == rp.x && lp.y == rp.y;
}

bool operator>(const P &lp, const P &rp) {
  if (lp.x != rp.x) {
    return lp.x > rp.x;
  }
  return lp.y > rp.y;
}

bool operator<(const P &lp, const P &rp) { return !(lp > rp) && !(lp == rp); }

int M, N;
std::vector<P> A, B;

// static int counter = 0;

bool can(const P &find_p) {
  auto it = std::lower_bound(B.begin(), B.end(), find_p);
  auto p = *it;
  // std::cout << "find:" << find_p.x << "," << find_p.y << std::endl;
  // if(counter++ < 10){
  //     std::cout << p.x << "," << p.y << std::endl;
  // }

  return p.x == find_p.x && p.y == find_p.y;
}

bool search(const P &diff) {
  FOR(i, 1, M) {
    P p = A[i];

    if (!can(p + diff)) {
      return false;
    }
  }

  return true;
}

P solve() {
  REP(i, N) {
    P a = A[0], b = B[i], diff = {b.x - a.x, b.y - a.y};

    if (search(diff)) {
      return diff;
    }
  }
}

int main() {
  while (std::cin >> M, M) {
    A.clear();
    B.clear();

    REP(i, M) {
      int x, y;
      std::cin >> x >> y;
      A.push_back({x, y});
    }

    std::cin >> N;
    REP(j, N) {
      int x, y;
      std::cin >> x >> y;
      B.push_back({x, y});
    }

    std::sort(B.begin(), B.end());

    // for(auto p : B){
    //     std::cout << "phoo: " << p.x << "," << p.y << std::endl;
    // }

    P res = solve();
    std::cout << res.x << " " << res.y << std::endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

typedef std::pair<int, int> P;
const int INF = 1 << 24;

void inputStars(int star_n, std::vector<P> &pv) {
  for (int i = 0; i < star_n; i++) {
    int first, second;
    std::cin >> first >> second;
    pv.push_back(std::make_pair(first, second));
  }
}

void toAbsolute(std::vector<P> &pv) {
  for (int i = 1; i < pv.size(); i++) {
    pv[i].first -= pv[0].first;
    pv[i].second -= pv[0].second;
  }
}

int main() {
  int n, m;
  auto compareP = [](const P &lp, const P &rp) -> bool {
    return lp.first < rp.first || lp.second < rp.second;
  };

  while (std::cin >> n, n) {

    std::vector<P> cv, sv;
    inputStars(n, cv);
    std::sort(cv.begin(), cv.end(), compareP);
    toAbsolute(cv);

    std::cin >> m;
    inputStars(m, sv);
    std::sort(sv.begin(), sv.end(), compareP);

    P res = std::make_pair(INF, INF);
    for (int i = 0; i < sv.size() && (res.first == INF); i++) { // iを起点とする
      bool isMatched = true;
      for (int j = 1; j < cv.size(); j++) {
        int x = sv[i].first + cv[j].first, y = sv[i].second + cv[j].second;
        if (std::find(sv.begin(), sv.end(), std::make_pair(x, y)) != sv.end()) {

        } else {
          isMatched = false;
          break;
        }
      }
      if (isMatched) {
        res = std::make_pair(sv[i].first - cv[0].first,
                             sv[i].second - cv[0].second);
      }
    }

    std::cout << res.first << " " << res.second << std::endl;
  }
  return 0;
}