Problem p02580 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define endl '\n'
#define ll long long
#define in insert
#define pb push_back
#define vt vector
#define P_Q(x) priority_queue<x>
#define p_q(x) priority_queue<x, vector<x>, greater<x>>
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define Rev(i, a, b) for (ll i = a; i >= b; i--)
#define FOR(m) Rep(i, 1, m)
#define For(m) Rep(i, 0, m - 1)
#define Rbl(x, a) for (auto &x : a)
#define FIO                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define F first
#define S second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define vpii vector<pii>
#define vpll vector<pll>
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
const ll INF = 0x3f3f3f3f3f3f3f3fll;
#define mod 1000000007

const int mxN = 3e5 + 1;
// ll a[mxN];
set<ll> row[mxN], col[mxN];
set<array<ll, 2>> R, C;
ll n, m, q;

int main() {
  FIO

          cin >>
      n >> m >> q;
  For(q) {
    ll u, v;
    cin >> u >> v;
    row[u].in(v);
    col[v].in(u);
  }
  FOR(n) if (sz(row[i])) R.in({sz(row[i]), i});
  FOR(m) if (sz(col[i])) C.in({sz(col[i]), i});

  auto z = *R.rbegin();
  ll maxi1 = z[0];
  z = *C.rbegin();
  ll maxi2 = z[0];
  ll ans = maxi1 + maxi2 - 1;
  for (auto i = R.rbegin(); i != R.rend() && (*i)[0] == maxi1; i++) {
    vt<array<ll, 2>> vec;
    for (ll x : row[(*i)[1]]) {
      array<ll, 2> temp = {sz(col[x]), x};
      C.erase(temp);
      vec.pb(temp);
    }

    auto z = *C.rbegin();
    if (z[0] == maxi2) {
      ans = maxi1 + maxi2;
      break;
    }
    for (auto x : vec)
      C.in(x);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define endl '\n'
#define ll long long
#define in insert
#define pb push_back
#define vt vector
#define P_Q(x) priority_queue<x>
#define p_q(x) priority_queue<x, vector<x>, greater<x>>
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define Rev(i, a, b) for (ll i = a; i >= b; i--)
#define FOR(m) Rep(i, 1, m)
#define For(m) Rep(i, 0, m - 1)
#define Rbl(x, a) for (auto &x : a)
#define FIO                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define F first
#define S second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define vpii vector<pii>
#define vpll vector<pll>
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
const ll INF = 0x3f3f3f3f3f3f3f3fll;
#define mod 1000000007

const int mxN = 3e5 + 1;
// ll a[mxN];
set<ll> row[mxN], col[mxN];
set<array<ll, 2>> R, C;
ll n, m, q;

int main() {
  FIO

          cin >>
      n >> m >> q;
  For(q) {
    ll u, v;
    cin >> u >> v;
    row[u].in(v);
    col[v].in(u);
  }
  FOR(n) if (sz(row[i])) R.in({sz(row[i]), i});
  FOR(m) if (sz(col[i])) C.in({sz(col[i]), i});

  auto z = *R.rbegin();
  ll maxi1 = z[0];
  z = *C.rbegin();
  ll maxi2 = z[0];
  ll ans = maxi1 + maxi2 - 1;
  for (auto i = R.rbegin(); i != R.rend() && (*i)[0] == maxi1; i++) {
    vt<array<ll, 2>> vec;
    for (ll x : row[(*i)[1]]) {
      array<ll, 2> temp = {sz(col[x]), x};
      C.erase(temp);
      vec.pb(temp);
    }
    if (!C.empty()) {
      auto z = *C.rbegin();
      if (z[0] == maxi2) {
        ans = maxi1 + maxi2;
        break;
      }
    }
    for (auto x : vec)
      C.in(x);
  }
  cout << ans << endl;
}