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

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define fo(i, n) for (ll i = 0; i < n; i++)
#define fo1(i, n) for (ll i = 1; i <= n; i++)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define loopr(i, a, b) for (ll i = b; i >= a; i--)
#define vll vector<ll>
#define vvl vector<vll>
#define pii pair<ll, ll>
#define F first
#define S second
#define endl "\\n"
#define M 1000000007
ll max(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}

/*ll po(ll x, ll y){
    if(y==0)
    return 1;
    if(y%2){
        return ((x)(po(x,y/2)%M)(po(x,y/2)%M) + M)%M;
    }
   return ((po(x,y/2)%M)*(po(x,y/2)%M) + M)%M;
}*/

/*void seive(bool prime[]){
    //bool prime[100002];


    for (ll p=2; p*p<=1000001; p++)
    {
        if (prime[p])
        {
            for (ll i=p*p; i<=1000001; i+=p)
                prime[i] = false;
        }
    }
}*/

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll t = 1;
  // cin>>t;
  while (t--) {
    ll h, w, m;
    cin >> h >> w >> m;
    unordered_map<ll, ll> row;
    unordered_map<ll, ll> col;
    ll mc = 0, mr = 0;
    // unordered_map<ll,ll> mp;
    unordered_map<ll, unordered_map<ll, ll>> mp;
    ll p = 0, q = 0;
    vector<pair<ll, ll>> pi(m);
    fo(i, m) {
      ll r, c;
      cin >> r >> c;
      pi[i].first = r;
      pi[i].second = c;
      mp[r][c] = 1;
      row[r]++;
      col[c]++;
    }
    fo(i, m) {
      ll r = pi[i].first;
      ll c = pi[i].second;
      // row[r];
      // col[c];
      if (row[r] > mr) {
        mr = row[r];
        p = r;
      } else if (row[r] == mr && mp[r][q] == 0)
        p = r;
      if (col[c] > mc) {
        mc = col[c];
        q = c;
      } else if (c != q && col[c] == mc && mp[p][c] == 0)
        q = c;
    }
    //  cout<<p<<" "<<q<<endl;
    // cout<<mr<<" "<<mc<<endl;
    if (mp[p][q] == 1)
      cout << mr + mc - 1 << endl;
    else
      cout << mr + mc << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define fo(i, n) for (ll i = 0; i < n; i++)
#define fo1(i, n) for (ll i = 1; i <= n; i++)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define loopr(i, a, b) for (ll i = b; i >= a; i--)
#define vll vector<ll>
#define vvl vector<vll>
#define pii pair<ll, ll>
#define F first
#define S second
#define endl "\\n"
#define M 1000000007
ll max(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}

/*ll po(ll x, ll y){
    if(y==0)
    return 1;
    if(y%2){
        return ((x)(po(x,y/2)%M)(po(x,y/2)%M) + M)%M;
    }
   return ((po(x,y/2)%M)*(po(x,y/2)%M) + M)%M;
}*/

void seive(bool prime[]) {
  // bool prime[100002];

  for (ll p = 2; p * p <= 1000001; p++) {
    if (prime[p]) {
      for (ll i = p * p; i <= 1000001; i += p)
        prime[i] = false;
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll t = 1;
  // cin>>t;
  while (t--) {
    ll h, w, m;
    cin >> h >> w >> m;
    vll row(h + 1, 0);
    vll col(w + 1, 0);
    ll mc = 0, mr = 0;
    vvl v(h + 1, vll(w + 1, 0));
    ll p = 0, q = 0;
    fo(i, m) {
      ll r, c;
      cin >> r >> c;
      v[r][c] = 1;
      row[r]++;
      col[c]++;
      if (row[r] > mr) {
        mr = row[r];
        p = r;
      }
      if (col[c] > mc) {
        mc = col[c];
        q = c;
      }
    }
    cout << mr + mc - v[p][q] << endl;
  }
  return 0;
}