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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(n) for (ll i = 0; i < n; ++i)
#define rep2(i, n) for (ll i = 0; i < n; ++i)
#define rep3(i, a, b) for (ll i = a; i < b; ++i)
#define rep4(i, a, b, c) for (ll i = a; i < b; i += c)
#define erep(n) for (ll i = 0; i <= n; ++i)
#define erep2(i, n) for (ll i = 0; i <= n; ++i)
#define erep3(i, a, b) for (ll i = a; i <= b; ++i)
#define erep4(i, a, b, c) for (ll i = a; i <= b; i += c)
#define rrep(i, a, b) for (ll i = a; i > b; --i)
#define rrep2(i, a, b, c) for (ll i = a; i > b; i -= c)
#define errep(i, a, b) for (ll i = a; i >= b; --i)
#define errep2(i, a, b, c) for (ll i = a; i >= b; i -= c)
#define print(a) cout << a << endl;
#define printVec(v)                                                            \\
  printf("{");                                                                 \\
  for (const auto &i : v) {                                                    \\
    std::cout << i << ", ";                                                    \\
  }                                                                            \\
  printf("}\\n");
#define cin(x) cin >> x;
const ll MOD = 1000000007;
const double PI = 3.14159265358979323846264338327950;
inline int newline() {
  putchar('\\n');
  return 0;
}

int main() {
  using P = pair<ll, ll>;

  ll h, w, m;
  cin(h);
  cin(w);
  cin(m);

  vector<ll> hs(h), ws(w);
  set<P> s;

  rep2(i, m) {
    ll _h, _w;
    cin(_h);
    cin(_w);
    --_h;
    --_w;

    ++hs[_h];
    ++ws[_w];
    s.emplace(_h, _w);
  }

  ll mh = 0, mw = 0;
  rep2(i, h) mh = max(mh, hs[i]);
  rep2(i, w) mw = max(mw, ws[i]);

  vector<ll> hi, wi;
  rep2(i, h) if (hs[i] == mh) hi.push_back(i);
  rep2(i, w) if (ws[i] == mw) wi.push_back(i);

  ll result = mh + mw;

  for (auto i : hi)
    for (auto j : wi) {
      if (s.count({i, j}) > 0)
        continue;

      print(result);
      return 0;
    }

  --result;
  print(result);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(n) for (ll i = 0; i < n; ++i)
#define rep2(i, n) for (ll i = 0; i < n; ++i)
#define rep3(i, a, b) for (ll i = a; i < b; ++i)
#define rep4(i, a, b, c) for (ll i = a; i < b; i += c)
#define erep(n) for (ll i = 0; i <= n; ++i)
#define erep2(i, n) for (ll i = 0; i <= n; ++i)
#define erep3(i, a, b) for (ll i = a; i <= b; ++i)
#define erep4(i, a, b, c) for (ll i = a; i <= b; i += c)
#define rrep(i, a, b) for (ll i = a; i > b; --i)
#define rrep2(i, a, b, c) for (ll i = a; i > b; i -= c)
#define errep(i, a, b) for (ll i = a; i >= b; --i)
#define errep2(i, a, b, c) for (ll i = a; i >= b; i -= c)
#define print(a) cout << a << endl;
#define printVec(v)                                                            \\
  printf("{");                                                                 \\
  for (const auto &i : v) {                                                    \\
    std::cout << i << ", ";                                                    \\
  }                                                                            \\
  printf("}\\n");
#define cin(x) cin >> x;
const ll MOD = 1000000007;
const double PI = 3.14159265358979323846264338327950;
inline int newline() {
  putchar('\\n');
  return 0;
}

int main() {
  using P = pair<ll, ll>;

  ll h, w, m;
  cin(h);
  cin(w);
  cin(m);

  vector<P> vc(w), hc(h);
  vector<vector<bool>> board(h, vector<bool>(w, false));

  rep2(i, w) vc[i] = {0, i};
  rep2(i, h) hc[i] = {0, i};

  rep2(i, m) {
    ll _h, _w;
    cin(_h);
    cin(_w);
    --_h;
    --_w;

    board[_h][_w] = true;
    ++vc[_w].first;
    ++hc[_h].first;
  }

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

  ll result = 0;

  rep2(i, w) {
    ll sum = hc[h - 1].first + vc[i].first;

    if (board[hc[h - 1].second][i])
      --sum;

    if (sum > result)
      result = sum;
  }

  print(result);
  return 0;
}