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

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define ll long long int
#define ar array
#define pb push_back
#define fi(a, b) for (int i = a; i < (b); i++)
#define fj(a, b) for (int j = a; j < (b); j++)
#define fk(a, b) for (int k = a; k < (b); k++)
#define lb lower_bound
#define ub upper_bound
// hash base 177013

int main() {
  FAST;
  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt","r",stdin);
  //     freopen("output.txt","w",stdout);
  // #endif
  // ll testcase;
  // cin>>testcase;
  // while(testcase--)
  {
    ll n, m, q;
    cin >> n >> m >> q;
    map<int, int> r, c;
    set<pair<int, int>> s;
    int max_row = 0, max_col = 0;
    fi(0, q) {
      int x, y;
      cin >> x >> y;
      --x, --y;
      s.insert({x, y});
      r[x]++;
      c[y]++;
      max_row = max(max_row, r[x]);
      max_col = max(max_col, c[y]);
    }
    vector<int> rows, cols;
    for (int i = 0; i < n; i++) {
      if (r[i] == max_row) {
        rows.pb(i);
      }
    }
    int ans = max_row + max_col;
    for (int j = 0; j < m; j++) {
      if (c[j] == max_col) {
        cols.pb(j);
      }
    }
    if (rows.size() * cols.size() > 300000) {
      cout << ans;
      return 0;
    }
    // cout<<"here\\n";
    bool common = false;
    for (auto i : rows) {
      for (auto j : cols) {
        if (s.find({i, j}) == s.end()) {
          common = true;
        }
      }
    }
    if (!common)
      ans--;
    cout << ans;
  }
}


Error: WA

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define ll long long int
#define ar array
#define pb push_back
#define fi(a, b) for (int i = a; i < (b); i++)
#define fj(a, b) for (int j = a; j < (b); j++)
#define fk(a, b) for (int k = a; k < (b); k++)
#define lb lower_bound
#define ub upper_bound
// hash base 177013

int main() {
  FAST;
  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt","r",stdin);
  //     freopen("output.txt","w",stdout);
  // #endif
  // ll testcase;
  // cin>>testcase;
  // while(testcase--)
  {
    ll n, m, q;
    cin >> n >> m >> q;
    ll mat[n][m];
    fi(0, n) {
      fj(0, m) { mat[i][j] = 0; }
    }
    fi(0, q) {
      ll x, y;
      cin >> x >> y;
      --x, --y;
      mat[x][y] = 1;
    }
    ll max_row = 0;
    vector<ll> max_row_index, max_col_index;
    fi(0, n) {
      ll sum = 0;
      fj(0, m) { sum += mat[i][j]; }
      max_row = max(max_row, sum);
    }
    ll max_col = 0;
    fj(0, m) {
      ll sum = 0;
      fi(0, n) { sum += mat[i][j]; }
      max_col = max(max_col, sum);
    }
    ll ans = max_col + max_row;
    fi(0, n) {
      ll sum = 0;
      fj(0, m) { sum += mat[i][j]; }
      if (sum == max_row) {
        max_row_index.pb(i);
      }
      if (max_row_index.size() > 300000) {
        cout << ans;
        return 0;
      }
    }

    fj(0, m) {
      ll sum = 0;
      fi(0, n) { sum += mat[i][j]; }
      if (sum == max_col) {
        max_col_index.pb(j);
      }
      if (max_row_index.size() * max_col_index.size() > 300000) {
        cout << ans;
        return 0;
      }
    }

    bool no_common = false;
    if (max_row_index.size() == 0 || max_col_index.size() == 0) {
      cout << 0;
      return 0;
    }
    for (auto i : max_row_index) {
      for (auto j : max_col_index) {
        if (mat[i][j] != 1) {
          no_common = true;
        }
      }
    }
    if (no_common == false) {
      ans--;
    }
    cout << ans;
  }
}