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

#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)

#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define SORT(x) sort(ALL(x))
#define REV(x) reverse(ALL(x))

using namespace std;
using ll = long long;
using vel = vector<ll>;
using ves = vector<string>;
using veb = vector<bool>;
using qul = queue<ll>;

void Yes(bool a) { cout << (a ? "Yes" : "No") << endl; }
void YES(bool a) { cout << (a ? "YES" : "NO") << endl; }

int binary_search(ll x, vel a) {
  auto ite = lower_bound(ALL(a), x);
  if (ite != a.end() && *ite == x) {
    return ite - a.begin();
  } else {
    return -1;
  }
}

ll modpow(ll a, ll n, ll mod) {
  if (n == 0)
    return 1;
  if (n == 1)
    return a % mod;
  if (n % 2 == 1)
    return (a * modpow(a, n - 1, mod)) % mod;
  ll t = modpow(a, n / 2, mod);
  return (t * t) % mod;
}

int main() {
  ll H, W, M, h, w, count = 0, h_max, w_max, c_h = 0, c_w = 0, ans;
  cin >> H >> W >> M;
  vel max_h(H, 0), max_w(W), ar_h(M), ar_w(M);

  REP(i, M) {
    cin >> h >> w;
    ar_h[i] = h;
    ar_w[i] = w;
    max_h[h - 1]++;
    max_w[w - 1]++;
  }
  h_max = MAX(max_h);
  w_max = MAX(max_w);
  ans = h_max + w_max - 1;

  REP(i, M) {
    h = ar_h[i];
    w = ar_w[i];
    if (ans == max_h[h - 1] + max_w[w - 1] - 1)
      count++;
  }

  SORT(max_h);
  SORT(max_w);

  REV(max_h);
  REV(max_w);

  REP(i, H) {
    if (max_h[i] != h_max)
      break;
    c_h++;
  }

  REP(i, W) {
    if (max_w[i] != w_max)
      break;
    c_w++;
  }

  if (count == c_h * c_w) {
    cout << ans << endl;
  } else {
    cout << ans + 1 << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)

#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define SORT(x) sort(ALL(x))
#define REV(x) reverse(ALL(x))

using namespace std;
using ll = long long;
using vel = vector<ll>;
using ves = vector<string>;
using veb = vector<bool>;
using qul = queue<ll>;

void Yes(bool a) { cout << (a ? "Yes" : "No") << endl; }
void YES(bool a) { cout << (a ? "YES" : "NO") << endl; }

int binary_search(ll x, vel a) {
  auto ite = lower_bound(ALL(a), x);
  if (ite != a.end() && *ite == x) {
    return ite - a.begin();
  } else {
    return -1;
  }
}

ll modpow(ll a, ll n, ll mod) {
  if (n == 0)
    return 1;
  if (n == 1)
    return a % mod;
  if (n % 2 == 1)
    return (a * modpow(a, n - 1, mod)) % mod;
  ll t = modpow(a, n / 2, mod);
  return (t * t) % mod;
}

int main() {
  ll H, W, M, h, w, count = 0, h_max, w_max, c_h = 0, c_w = 0, ans;
  cin >> H >> W >> M;
  vector<int> max_h(H), max_w(W), ar_h(M), ar_w(M);

  REP(i, M) {
    cin >> h >> w;
    ar_h[i] = h;
    ar_w[i] = w;
    max_h[h]++;
    max_w[w]++;
  }

  h_max = MAX(max_h);
  w_max = MAX(max_w);

  ans = h_max + w_max - 1;

  REP(i, M) {
    h = ar_h[i];
    w = ar_w[i];
    if (ans == max_h[h] + max_w[w])
      count++;
  }

  SORT(max_h);
  SORT(max_w);

  REV(max_h);
  REV(max_w);

  REP(i, H) {
    if (max_h[i] != h_max)
      break;
    c_h++;
  }

  REP(i, W) {
    if (max_w[i] != w_max)
      break;
    c_w++;
  }

  if (count == c_h * c_w) {
    cout << ans << endl;
  } else {
    cout << ans + 1 << endl;
  }

  return 0;
}