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

#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define all(v) (v).begin(), (v).end()
#define UNIQUE(v) (v).erase(unique(all(v)), (v).end());
#define MOD 1000000007

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  set<pair<int, int>> target;
  int sumH[h] = {0};
  int sumW[w] = {0};
  vector<int> cH;
  vector<int> cW;
  int mh, mw;
  REP(i, m) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    sumH[x]++;
    sumW[y]++;
    target.insert(make_pair(x, y));
  }

  mh = *max_element(sumH, sumH + h);
  mw = *max_element(sumW, sumW + w);
  REP(i, h) {
    if (sumH[i] == mh)
      cH.push_back(i);
  }
  REP(i, w) {
    if (sumW[i] == mw)
      cW.push_back(i);
  }

  REP(i, cH.size()) REP(j, cW.size()) {
    if (target.find(make_pair(cH[i], cW[j])) == target.end()) {
      cout << mh + mw << endl;
      return 0;
    }
  }

  cout << mh + mw - 1 << endl;

  return 0;
}


Error: 0

Fixed submission:


// BELIEVE IN YOURSELF
// next_permutation

#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define pob pop_back
#define vi vector<ll>
#define mp make_pair
#define sz size()
#define rep1(i, n) for (ll i = 1; i <= n; i++)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define maxx 100005
#define se second
#define fi first
#define be begin()
#define en end()
#define sortv(v) sort(v.begin(), v.end());
#define w()                                                                    \\
  ll _;                                                                        \\
  cin >> _;                                                                    \\
  while (_--)
#define edge pair<ll, ll>
#define pq priority_queue<ll>
#define mod 1000000007
#define Endl "\\n"
#define cy cout << "YES" << endl;
#define cn cout << "NO" << endl;
#define lb(v, x) std::lower_bound(v.begin(), v.end(), x)
#define ub(v, x) std::upper_bound(v.begin(), v.end(), x)

using namespace std;

//------BEAST MODE ON------------
//-----fuck rating

int main() {
  fast;

  ll n, m;
  ll k;

  cin >> n >> m >> k;

  ll a[n + 5][m + 5];

  ll i, j;

  memset(a, 0, sizeof(a));

  for (ll x = 0; x < k; x++) {
    cin >> i >> j;
    a[i][j]++;
  }

  ll mr = 0, mc = 0;

  ll r = 0, c = 0;

  for (i = 1; i <= n; i++) {
    ll x = 0;
    for (j = 1; j <= m; j++) {
      if (a[i][j] > 0) {
        x += a[i][j];
      }
    }
    if (x > mr) {
      mr = x;
      r = i;
    }
  }

  for (i = 1; i <= m; i++) {
    ll x = 0;
    for (j = 1; j <= n; j++) {
      if (a[j][i] > 0) {
        x += a[j][i];
      }
    }
    if (x > mc) {
      mc = x;
      c = i;
    } else if (x == mc) {
      if (a[r][i] < a[r][c]) {
        c = i;
      }
    }
  }
  ll ans = 0;
  if (a[r][c] > 0) {
    ans = mr + mc - a[r][c];
  } else {
    ans = mr + mc;
  }
  cout << ans << endl;

  return 0;
}