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

#include <bits/stdc++.h>

using namespace std;

#define debug(x) cout << (#x) << " is " << x << endl;
#define Q                                                                      \\
  int t;                                                                       \\
  scanf("%d", &t);                                                             \\
  for (int q = 1; q <= t; q++)
typedef long long int lli;
typedef pair<int, int> pi;

#define N 300005
int H[N], W[N];

int main() {
  int h, w, m, hmx = 0, wmx = 0;
  cin >> h >> w >> m;
  vector<pi> p;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    H[x]++;
    W[y]++;
    p.push_back({x, y});
    hmx = max(hmx, H[x]);
    wmx = max(wmx, W[y]);
  }
  lli hc = 0, wc = 0;
  for (int i = 1; i <= h; i++)
    hc += (H[i] == hmx);
  for (int i = 1; i <= w; i++)
    wc += (W[i] == wmx);

  lli cc = 0;
  for (auto el : p) {
    if (H[el.first] == hmx && W[el.second] == wmx)
      cc++;
  }
  if (cc == hc * wc) {
    cout << hmx + wmx - 1 << endl;
  } else
    cout << hmx + wmx << endl;

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define in1(n) cin >> n;
#define in2(n, k) cin >> n >> k;
#define in3(n, k, s) cin >> n >> k >> s;
#define scn(t) scanf("%d", &t)
#define scnld(t) scanf("%ld", &t)
#define scnlld(t) scanf("%lld", &t)
#define scnc(t) scanf("%c", &t)
#define scns(t) scanf("%s", t)
#define scnf(t) scanf("%f", &t)
#define scnlf(t) scanf("%lf", &t)
#define mem(a, b) memset(a, (b), sizeof(a))
#define nloop(i, j, k, in) for (int i = j; i < k; i += in)
#define rloop(i, j, k, in) for (int i = j; i >= k; i -= in)
#define rep(i, j) nloop(i, 0, j, 1)
#define rrep(i, j) rloop(i, j, 0, 1)
#define inar(n, a) rep(i, n) cin >> a[i];
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert(B <= A && A <= C)
#define MP make_pair
#define PB push_back
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
const double pi = acos(-1.0);
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int, int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;

int main() {
  fast int n, m;
  in2(n, m) int row[n], col[m];
  int maze[n][m];
  rep(i, n) rep(j, m) maze[i][j] = 0;
  mem(row, 0);
  mem(col, 0);
  test {
    int a, b;
    in2(a, b) a--, b--;
    row[a]++, col[b]++;
    maze[a][b] = -1;
  }
  int rmax, cmax;
  int ri = distance(row, max_element(row, row + n));
  rep(i, m) if (maze[ri][i] == -1) col[i]--;
  rmax = *max_element(row, row + n);
  cmax = *max_element(col, col + m);
  rep(i, m) if (maze[ri][i] == -1) col[i]++;
  int cnt = rmax + cmax;
  int ci = distance(col, max_element(col, col + m));
  rep(i, n) if (maze[i][ci] == -1) row[i]--;
  rmax = *max_element(row, row + n);
  cmax = *max_element(col, col + m);
  int ans = rmax + cmax;
  cout << max(ans, cnt);
  return 0;
}