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

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int64_t min(int64_t a, int64_t b) {
  if (a > b) {
    return b;
  } else {
    return a;
  }
}

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<pair<int, int>> htable(h, pair<int, int>());
  vector<pair<int, int>> wtable(w, pair<int, int>());
  for (size_t i = 0; i < h; i++) {
    htable.at(i).first = 0;
    htable.at(i).second = i;
  }
  for (size_t i = 0; i < w; i++) {
    wtable.at(i).first = 0;
    wtable.at(i).second = i;
  }
  vector<pair<int, int>> b;
  for (int i = 0; i < m; i++) {
    int hi, wi;
    cin >> hi >> wi;
    htable.at(hi - 1).first++;
    wtable.at(wi - 1).first++;
    b.push_back(make_pair(hi - 1, wi - 1));
  }
  sort(b.begin(), b.end());
  sort(htable.begin(), htable.end(), greater<pair<int, int>>());
  sort(wtable.begin(), wtable.end(), greater<pair<int, int>>());
  int limit = 1000000;
  int lookh = 1;
  int lookw = 1;
  while (lookh < limit && h > 1) {
    if (htable.at(lookh).first == htable.at(lookh - 1).first) {
      lookh++;
    } else {
      break;
    }
    if (lookh >= h) {
      break;
    }
  }
  int ans = 0;
  int temp = 0;
  for (int i = 0; i < min(h, lookh); i++) {
    for (int j = 0; j < min(w, limit / lookh + 1); j++) {
      temp = htable.at(i).first + wtable.at(j).first;
      temp -=
          binary_search(b.begin(), b.end(),
                        make_pair(htable.at(i).second, wtable.at(j).second));
      ans = max(temp, ans);
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int64_t min(int64_t a, int64_t b) {
  if (a > b) {
    return b;
  } else {
    return a;
  }
}

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<pair<int, int>> htable(h, pair<int, int>());
  vector<pair<int, int>> wtable(w, pair<int, int>());
  for (size_t i = 0; i < h; i++) {
    htable.at(i).first = 0;
    htable.at(i).second = i;
  }
  for (size_t i = 0; i < w; i++) {
    wtable.at(i).first = 0;
    wtable.at(i).second = i;
  }
  vector<pair<int, int>> b;
  for (int i = 0; i < m; i++) {
    int hi, wi;
    cin >> hi >> wi;
    htable.at(hi - 1).first++;
    wtable.at(wi - 1).first++;
    b.push_back(make_pair(hi - 1, wi - 1));
  }
  sort(b.begin(), b.end());
  sort(htable.begin(), htable.end(), greater<pair<int, int>>());
  sort(wtable.begin(), wtable.end(), greater<pair<int, int>>());
  int limit = 1000000;
  int lookh = 1;
  int lookw = 1;
  while (lookh < limit) {
    if (htable.at(lookh).first == htable.at(lookh - 1).first) {
      lookh++;
    } else {
      break;
    }
    if (lookh == h) {
      break;
    }
  }
  while (lookh * lookw < limit) {
    if (wtable.at(lookw).first == wtable.at(lookw - 1).first) {
      lookw++;
    } else {
      break;
    }
    if (lookw == w) {
      break;
    }
  }
  int ans = 0;
  int temp = 0;
  for (int i = 0; i < min(h, lookh + 1); i++) {
    for (int j = 0; j < min(w, lookw + 1); j++) {
      temp = htable.at(i).first + wtable.at(j).first;
      temp -=
          binary_search(b.begin(), b.end(),
                        make_pair(htable.at(i).second, wtable.at(j).second));
      ans = max(temp, ans);
    }
  }
  cout << ans << endl;
}