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

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define ins insert
#define sz(x) (long long)x.size()
typedef long long ll;
typedef long double ld;
const int N = 1e5 + 5;
const ld pi = 3.141592653;
const ll MOD = 1e9 + 7;
const ll mod = 998244353;
const ll INF = __LONG_LONG_MAX__;

using namespace std;

int dx[9] = {1, -1, 0, 0, 1, 1, -1, -1, 0};
int dy[9] = {0, 0, 1, -1, 1, -1, 1, -1, 0};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  // cin>>T;
  while (T--) {
    int h, w, i, m, rmax = 0, cmax = 0, ans = 0, j;
    cin >> h >> w >> m;
    vector<pair<int, int>> v;
    map<int, int> r, c;
    set<int> row, col, crow, ccol;
    vector<int> g[3 * N], q[3 * N];
    for (i = 0; i < m; i++) {
      int x, y;
      cin >> x >> y;
      v.pb({x, y});
      if (r.find(x) != r.end())
        r[x]++;
      else
        r.ins({x, 1});
      if (c.find(y) != c.end())
        c[y]++;
      else
        c.ins({y, 1});
      rmax = max(rmax, r[x]);
      cmax = max(cmax, c[y]);
    }
    for (auto &o : r) {
      if (o.se == rmax)
        row.ins(o.fi);
    }
    for (auto &o : c) {
      if (o.se == cmax)
        col.ins(o.fi);
    }
    for (i = 0; i < m; i++) {
      if (row.find(v[i].fi) != row.end())
        g[v[i].fi].pb(v[i].se);
      if (col.find(v[i].se) != col.end())
        q[v[i].se].pb(v[i].fi);
    }
    crow = row;
    ccol = col;
    bool ok = false;
    for (i = 1; i <= h; i++) {
      if (sz(g[i]) > 0) {
        for (j = 0; j < sz(g[i]); j++) {
          if (ccol.find(g[i][j]) != ccol.end())
            ccol.erase(g[i][j]);
        }
        if (sz(ccol) > 0) {
          ok = true;
          break;
        } else {
          for (j = 0; j < sz(g[i]); j++) {
            if (col.find(g[i][j]) != col.end())
              ccol.ins(g[i][j]);
          }
        }
      }
    }
    if (ok)
      ans = rmax + cmax;
    else {
      for (i = 1; i <= w; i++) {
        if (sz(q[i]) > 0) {
          for (j = 0; j < sz(q[i]); j++) {
            if (crow.find(q[i][j]) != crow.end())
              crow.erase(q[i][j]);
          }
          if (sz(crow) > 0) {
            ok = true;
            break;
          } else {
            for (j = 0; j < sz(q[i]); j++) {
              if (row.find(q[i][j]) != row.end())
                crow.ins(q[i][j]);
            }
          }
        }
      }
      if (ok)
        ans = rmax + cmax;
      else
        ans = rmax + cmax - 1;
    }
    cout << ans;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define ins insert
#define sz(x) (long long)x.size()
typedef long long ll;
typedef long double ld;
const int N = 1e5 + 5;
const ld pi = 3.141592653;
const ll MOD = 1e9 + 7;
const ll mod = 998244353;
const ll INF = __LONG_LONG_MAX__;

using namespace std;

int dx[9] = {1, -1, 0, 0, 1, 1, -1, -1, 0};
int dy[9] = {0, 0, 1, -1, 1, -1, 1, -1, 0};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  // cin>>T;
  while (T--) {
    int h, w, i, m, rmax = 0, cmax = 0, ans = 0, j;
    cin >> h >> w >> m;
    vector<pair<int, int>> v;
    map<int, int> r, c;
    set<int> row, col;
    vector<int> g[100], q[100];
    for (i = 0; i < m; i++) {
      int x, y;
      cin >> x >> y;
      v.pb({x, y});
      if (r.find(x) != r.end())
        r[x]++;
      else
        r.ins({x, 1});
      if (c.find(y) != c.end())
        c[y]++;
      else
        c.ins({y, 1});
      rmax = max(rmax, r[x]);
      cmax = max(cmax, c[y]);
    }
    for (auto &o : r) {
      if (o.se == rmax)
        row.ins(o.fi);
    }
    for (auto &o : c) {
      if (o.se == cmax)
        col.ins(o.fi);
    }
    for (i = 0; i < m; i++) {
      if (row.find(v[i].fi) != row.end())
        g[v[i].fi].pb(v[i].se);
      if (col.find(v[i].se) != col.end())
        q[v[i].se].pb(v[i].fi);
    }
    bool ok = false;
    for (i = 1; i <= h; i++) {
      if (sz(g[i]) > 0) {
        for (j = 0; j < sz(g[i]); j++) {
          if (col.find(g[i][j]) != col.end())
            break;
        }
        if (j == sz(g[i])) {
          ok = true;
          break;
        }
      }
    }
    if (ok)
      ans = rmax + cmax;
    else {
      for (i = 1; i <= w; i++) {
        if (sz(q[i]) > 0) {
          for (j = 0; j < sz(q[i]); j++) {
            if (row.find(q[i][j]) != row.end())
              break;
          }
          if (j == sz(q[i])) {
            ok = true;
            break;
          }
        }
      }
      if (ok)
        ans = rmax + cmax;
      else
        ans = rmax + cmax - 1;
    }
    cout << ans;
  }
  return 0;
}