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

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll h, w, m2;
  cin >> h >> w >> m2;
  vector<vector<ll>> m;
  vector<vector<ll>> hh(h, vector<ll>(2, 0));
  vector<vector<ll>> ww(w, vector<ll>(2, 0));
  vector<ll> hh2;
  vector<ll> ww2;

  rep(i, h) { hh[i][1] = i; }
  rep(i, w) { ww[i][1] = i; }

  ll x, y;
  rep(i, m2) {
    cin >> x >> y;
    --x;
    --y;
    ++hh[x][0];
    ++ww[y][0];
    //++m[x][y];
    m.push_back({x, y});
  }

  sort(hh.rbegin(), hh.rend());
  sort(ww.rbegin(), ww.rend());
  sort(m.rbegin(), m.rend());
  sort(hh2.rbegin(), hh2.rend());
  sort(ww2.rbegin(), ww2.rend());

  /*rep(i,h){
      cout << hh[i][0] << hh[i][1] <<  endl;
  }*/

  ll ans = 0;

  /*rep(i,h){
      rep(j,w){
          //cout << i << " " << j << " " << hh[i][0] << " " << ww[j][0] << " "
  << m[hh[i][1]][ww[j][1]] << endl;

          //cout << i << " " << j << " " <<
  hh[i][0]+ww[j][0]-m[hh[i][1]][ww[j][1]] << endl;
          ans=max(ans,hh[i][0]+ww[j][0]-m[hh[i][1]][ww[j][1]]);
          if(ans>(hh[i][0]+ww[j][0]-m[hh[i][1]][ww[j][1]]+1)){
              break;
          }
      }
  }*/

  ll hma, wma;
  hma = hh[0][0];
  wma = ww[0][0];

  rep(i, h) {
    if (hh[i][0] == hma) {
      hh2.push_back(hh[i][1]);
    } else {
      break;
    }
  }
  rep(i, w) {
    if (ww[i][0] == wma) {
      ww2.push_back(ww[i][1]);
    } else {
      break;
    }
  }

  ll hl, wl;
  hl = hh2.size();
  wl = ww2.size();

  int rr = 1;
  ans = hma + wma;
  int k = 0;

  rep(i, hl) {
    rep(j, wl) {
      while (1) {
        if (m[k][0] == hh2[i] and m[k][1] == ww2[j]) {
          break;
        } else {
          ++k;
          if (k == m2) {
            rr = 0;
            break;
          }
        }
      }
      if (rr == 0) {
        break;
      }
    }
    if (rr == 0) {
      break;
    }
  }

  if (rr == 1) {
    --ans;
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll h, w, m2;
  cin >> h >> w >> m2;
  vector<vector<int>> m(h, vector<int>(w, 0));
  vector<vector<ll>> hh(h, vector<ll>(2, 0));
  vector<vector<ll>> ww(w, vector<ll>(2, 0));
  vector<vector<ll>> hh2;
  vector<vector<ll>> ww2;

  rep(i, h) { hh[i][1] = i; }
  rep(i, w) { ww[i][1] = i; }

  ll x, y;
  rep(i, m2) {
    cin >> x >> y;
    --x;
    --y;
    ++hh[x][0];
    ++ww[y][0];
    ++m[x][y];
  }

  sort(hh.rbegin(), hh.rend());
  sort(ww.rbegin(), ww.rend());

  ll ans = 0;

  /*rep(i,h){
      rep(j,w){
          //cout << i << " " << j << " " << hh[i][0] << " " << ww[j][0] << " "
  << m[hh[i][1]][ww[j][1]] << endl;

          //cout << i << " " << j << " " <<
  hh[i][0]+ww[j][0]-m[hh[i][1]][ww[j][1]] << endl;
          ans=max(ans,hh[i][0]+ww[j][0]-m[hh[i][1]][ww[j][1]]);
          if(ans>(hh[i][0]+ww[j][0]-m[hh[i][1]][ww[j][1]]+1)){
              break;
          }
      }
  }*/

  ll hma, wma;
  hma = hh[0][0];
  wma = ww[0][0];

  rep(i, h) {
    if (hh[i][0] == hma) {
      hh2.push_back(hh[i]);
    } else {
      break;
    }
  }
  rep(i, w) {
    if (ww[i][0] == wma) {
      ww2.push_back(ww[i]);
    } else {
      break;
    }
  }

  ll hl, wl;
  hl = hh2.size();
  wl = ww2.size();

  int rr = 1;
  ans = hma + wma;

  rep(i, hl) {
    rep(j, wl) {
      if (m[hh2[i][1]][ww2[j][1]] == 0) {
        ans = hma + wma;
        rr = 0;
        if (rr == 0) {
          break;
        }
      }
    }
    if (rr == 0) {
      break;
    }
  }

  if (rr == 1) {
    --ans;
  }

  cout << ans << endl;

  return 0;
}