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

#include "bits/stdc++.h"
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;

void Main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<P> exist;
  map<P, int> bomb;
  vi cx(h, 0);
  vi cy(w, 0);

  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cx[x]++;
    cy[y]++;
    bomb[{x, y}]++;
    exist.pb({x, y});
  }

  vector<P> cntx(h), cnty(w);
  rep(i, h) cntx[i] = {cx[i], i};
  rep(i, w) cnty[i] = {cy[i], i};

  sort(rall(cntx));
  sort(rall(cnty));

  /*
  for(auto [c, i] : cntx) cout << c << " " << i << endl;
  cout << endl;
  for(auto [c, i] : cnty) cout << c << " " << i << endl;
  */

  int ans = 0;
  for (auto [x, y] : exist) {
    ans = max(ans, cx[x] + cy[y] - 1);
  }

  int mx = cntx[0].fi + cnty[0].fi;
  // cout << mx << endl << endl;
  if (ans >= mx) {
    cout << ans << endl;
    return;
  }

  vi candx, candy;
  rep(i, h) {
    if (cntx[i].fi == cntx[0].fi)
      candx.pb(cntx[i].se);
    else
      break;
  }
  rep(i, w) {
    if (cnty[i].fi == cnty[0].fi)
      candy.pb(cnty[i].se);
    else
      break;
  }

  for (auto x : candx) {
    for (auto y : candy) {
      // cout << x << " " << y << endl;
      //*
      if (bomb[{x, y}] == 0) {
        cout << mx << endl;
        return;
      }
      //*/
    }
  }
  cout << mx - 1 << endl;
  return;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;

void Main() {
  int h, w, m;
  cin >> h >> w >> m;
  vvi bomb(h, vi(w, 0));
  vector<P> exist;
  vi cx(h, 0);
  vi cy(w, 0);

  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cx[x]++;
    cy[y]++;
    bomb[x][y] = 1;
    exist.pb({x, y});
  }

  vector<P> cntx(h), cnty(w);
  rep(i, h) cntx[i] = {cx[i], i};
  rep(i, w) cnty[i] = {cy[i], i};

  sort(rall(cntx));
  sort(rall(cnty));

  /*
  for(auto [c, i] : cntx) cout << c << " " << i << endl;
  cout << endl;
  for(auto [c, i] : cnty) cout << c << " " << i << endl;
  */

  int ans = 0;
  for (auto [x, y] : exist) {
    ans = max(ans, cx[x] + cy[y] - 1);
  }

  int mx = cntx[0].fi + cnty[0].fi;
  // cout << mx << endl << endl;
  if (ans >= mx) {
    cout << ans << endl;
    return;
  }

  vi candx, candy;
  rep(i, h) {
    if (cntx[i].fi == cntx[0].fi)
      candx.pb(cntx[i].se);
    else
      break;
  }
  rep(i, w) {
    if (cnty[i].fi == cnty[0].fi)
      candy.pb(cnty[i].se);
    else
      break;
  }

  for (auto x : candx) {
    for (auto y : candy) {
      // cout << x << " " << y << endl;
      //*
      if (bomb[x][y] == 0) {
        cout << mx << endl;
        return;
      }
      //*/
    }
  }
  cout << mx - 1 << endl;
  return;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}