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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, s, n) for (int i = (s); i < (n); i++)
#define all(v) v.begin(), v.end()
#define outve(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl
#define outmat(v)                                                              \\
  for (auto i : v) {                                                           \\
    for (auto j : i)                                                           \\
      cout << j << " ";                                                        \\
    cout << endl;                                                              \\
  }
#define in(n, v)                                                               \\
  for (int i = 0; i < (n); i++) {                                              \\
    cin >> v[i];                                                               \\
  }
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define si(v) int(v.size())
#define len(v) int(v.length())
#define lob(v, n) lower_bound(all(v), n)
#define lobi(v, n) lower_bound(all(v), n) - v.begin()
#define upb(v, n) upper_bound(all(v), n)
#define upbi(v, n) upper_bound(all(v), n) - v.begin()
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vc = vector<char>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vvp = vector<vector<pair<int, int>>>;
template <typename T> using ve = vector<T>;
template <typename T> using pq2 = priority_queue<T>;
template <typename T> using pq1 = priority_queue<T, vector<T>, greater<T>>;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int H, W, M;
  cin >> H >> W >> M;
  vi A(H, 0), B(W, 0);
  ve<pii> C;
  rep(i, M) {
    int h, w;
    cin >> h >> w;
    h--, w--;
    C.push_back({h, w});
    A[h]++;
  }

  int a = 0;
  int aa = -1;
  rep(i, H) if (chmax(a, A[i])) aa = i;
  rep(i, M) if (C[i].fi != aa) B[C[i].se]++;
  int b = 0;
  rep(i, W) if (chmax(b, B[i])) aa = i;

  int ans = a + b;

  rep(i, H) A[i] = 0;
  rep(i, W) B[i] = 0;
  rep(i, M) B[C[i].se]++;
  a = 0;
  aa = -1;

  rep(i, W) if (chmax(a, B[i])) aa = i;
  rep(i, M) if (C[i].se != aa) A[C[i].fi]++;
  b = 0;
  rep(i, H) if (chmax(b, A[i])) aa = i;

  chmax(ans, a + b);

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, s, n) for (int i = (s); i < (n); i++)
#define all(v) v.begin(), v.end()
#define outve(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl
#define outmat(v)                                                              \\
  for (auto i : v) {                                                           \\
    for (auto j : i)                                                           \\
      cout << j << " ";                                                        \\
    cout << endl;                                                              \\
  }
#define in(n, v)                                                               \\
  for (int i = 0; i < (n); i++) {                                              \\
    cin >> v[i];                                                               \\
  }
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define si(v) int(v.size())
#define len(v) int(v.length())
#define lob(v, n) lower_bound(all(v), n)
#define lobi(v, n) lower_bound(all(v), n) - v.begin()
#define upb(v, n) upper_bound(all(v), n)
#define upbi(v, n) upper_bound(all(v), n) - v.begin()
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vc = vector<char>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vvp = vector<vector<pair<int, int>>>;
template <typename T> using ve = vector<T>;
template <typename T> using pq2 = priority_queue<T>;
template <typename T> using pq1 = priority_queue<T, vector<T>, greater<T>>;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int H, W, M;
  cin >> H >> W >> M;
  vi A(H, 0), B(W, 0);
  ve<pii> C;
  rep(i, M) {
    int h, w;
    cin >> h >> w;
    h--, w--;
    C.push_back({h, w});
    A[h]++;
  }

  int a = 0;
  int aa = -1;
  rep(i, H) if (chmax(a, A[i])) aa = i;

  rep(i, M) if (C[i].fi != aa) B[C[i].se]++;
  int b = 0;
  rep(i, W) if (chmax(b, B[i])) aa = i;

  int ans = a + b;

  rep(i, H) A[i] = 0;
  rep(i, W) B[i] = 0;
  rep(i, M) B[C[i].se]++;
  a = 0;
  aa = -1;

  rep(i, W) if (chmax(a, B[i])) aa = i;
  rep(i, M) if (C[i].se != aa) A[C[i].se]++;
  b = 0;
  rep(i, H) if (chmax(b, A[i])) aa = i;

  chmax(ans, a + b);

  cout << ans << endl;

  return 0;
}