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

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ll H, W;
  cin >> H >> W;
  ll M;
  cin >> M;
  vector<pair<ll, ll>> B(H);
  for (ll i = 0; i < H; i++) {
    B[i] = make_pair(0, i);
  }
  vector<pair<ll, ll>> C(W);
  for (ll i = 0; i < W; i++) {
    C[i] = make_pair(0, i);
  }
  set<pair<ll, ll>> A;
  for (ll i = 0; i < M; i++) {
    ll h, w;
    cin >> h >> w;
    h--;
    w--;
    A.insert(make_pair(h, w));
    B[h].first++;
    C[w].first++;
  }
  sort(B.begin(), B.end());
  reverse(B.begin(), B.end());
  sort(C.begin(), C.end());
  reverse(C.begin(), C.end());
  ll ans = 0;
  ans = B[0].first + C[0].first;
  if (!A.count(make_pair(B[0].second, C[0].second))) {
    cout << ans << endl;
  } else {
    ll cnt1 = 1;
    ll cnt2 = 1;
    for (ll i = 1; i < H; i++) {
      if (B[i].first < B[i - 1].first) {
        break;
      } else {
        cnt1++;
      }
    }
    for (ll i = 1; i < W; i++) {
      if (C[i].first < C[i - 1].first) {
        break;
      } else {
        cnt2++;
      }
    }
    bool frag = false;
    for (ll i = 0; i < cnt1; i++) {
      for (ll j = 0; j < cnt2; j++) {
        if (!A.count(make_pair(B[i].second, C[j].second))) {
          frag = true;
        }
      }
    }
    if (frag) {
      cout << ans << endl;
    } else {
      cout << ans - 1 << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ll H, W;
  cin >> H >> W;
  ll M;
  cin >> M;
  vector<pair<ll, ll>> B(H);
  for (ll i = 0; i < H; i++) {
    B[i] = make_pair(0, i);
  }
  vector<pair<ll, ll>> C(W);
  for (ll i = 0; i < W; i++) {
    C[i] = make_pair(0, i);
  }
  vector<vector<ll>> A(H, vector<ll>(W, 0));
  for (ll i = 0; i < M; i++) {
    ll h, w;
    cin >> h >> w;
    h--;
    w--;
    A[h][w] = 1;
    B[h].first++;
    C[w].first++;
  }
  sort(B.begin(), B.end());
  reverse(B.begin(), B.end());
  sort(C.begin(), C.end());
  reverse(C.begin(), C.end());
  ll ans = 0;
  ans = B[0].first + C[0].first;
  if (A[B[0].second][C[0].second] == 0) {
    cout << ans << endl;
  } else {
    ll cnt1 = 1;
    ll cnt2 = 1;
    for (ll i = 1; i < H; i++) {
      if (B[i].first < B[i - 1].first) {
        break;
      } else {
        cnt1++;
      }
    }
    for (ll i = 1; i < W; i++) {
      if (C[i].first < C[i - 1].first) {
        break;
      } else {
        cnt2++;
      }
    }
    bool frag = false;
    for (ll i = 0; i < cnt1; i++) {
      for (ll j = 0; j < cnt2; j++) {
        if (A[B[i].second][C[j].second] == 0) {
          frag = true;
        }
      }
    }
    if (frag) {
      cout << ans << endl;
    } else {
      cout << ans - 1 << endl;
    }
  }
}