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

/* Simplicity and Goodness */

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> indexed_set;

void my_dbg() { cout << endl; }
template <typename Arg, typename... Args> void my_dbg(Arg A, Args... B) {
  cout << ' ' << A;
  my_dbg(B...);
}
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", my_dbg(__VA_ARGS__)

#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define pri(n) printf("%d ", (int)(n))
#define prin(n) printf("%d\\n", (int)(n))
#define lpri(n) printf("%lld ", n)
#define lprin(n) printf("%lld\\n", n)
#define rep(i, a, b) for (int i = a; i < (int)b; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;

const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 1e9 + 7;
const int maxN = 2e5 + 2;

void solve() {
  int h, w, m;
  cin >> h >> w >> m;

  map<int, int> col, row;
  set<pair<int, int>> s;

  vi maxr, maxc;

  rep(i, 0, m) {
    int x, y;
    scn(x);
    scn(y);

    row[x]++;
    col[y]++;

    s.emplace(x, y);
  }

  int cm = 0, cr = 0;

  for (auto it : row) {
    if (it.S > cr) {
      maxr.clear();
      maxr.pb(it.F);
      cr = it.S;
    }

    if (it.S == cr) {
      maxr.pb(it.F);
    }
  }

  for (auto it : col) {
    if (it.S > cm) {
      maxc.clear();
      maxc.pb(it.F);
      cm = it.S;
    }

    if (it.S == cm) {
      maxc.pb(it.F);
    }
  }

  int ans = cr + cm - 1;

  rep(i, 0, maxr.size()) {
    rep(j, 0, maxc.size()) {
      if (!s.count({maxr[i], maxc[j]})) {
        ans = cr + cm;
        break;
      }
    }
  }

  prin(ans);
}

int main() {
  int t = 1;
  // scn(t);

  while (t--) {
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

/* Simplicity and Goodness */

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> indexed_set;

void my_dbg() { cout << endl; }
template <typename Arg, typename... Args> void my_dbg(Arg A, Args... B) {
  cout << ' ' << A;
  my_dbg(B...);
}
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", my_dbg(__VA_ARGS__)

#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define pri(n) printf("%d ", (int)(n))
#define prin(n) printf("%d\\n", (int)(n))
#define lpri(n) printf("%lld ", n)
#define lprin(n) printf("%lld\\n", n)
#define rep(i, a, b) for (int i = a; i < (int)b; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;

const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 1e9 + 7;
const int maxN = 2e5 + 2;

void solve() {
  int h, w, m;
  scanf("%d%d%d", &h, &w, &m);

  vi col(h + 2, 0), row(w + 2, 0);
  set<pair<int, int>> s;

  vi maxr, maxc;

  rep(i, 0, m) {
    int x, y;
    scn(x);
    scn(y);

    row[x]++;
    col[y]++;

    s.emplace(x, y);
  }

  int cm = 0, cr = 0;

  rep(i, 1, h + 1) {
    pair<int, int> it = {i, row[i]};

    if (it.S > cr) {
      maxr.clear();
      maxr.pb(it.F);
      cr = it.S;
    }

    if (it.S == cr) {
      maxr.pb(it.F);
    }
  }

  rep(i, 1, w + 1) {
    pair<int, int> it = {i, col[i]};

    if (it.S > cm) {
      maxc.clear();
      maxc.pb(it.F);
      cm = it.S;
    }

    if (it.S == cm) {
      maxc.pb(it.F);
    }
  }

  int ans = cr + cm - 1;

  rep(i, 0, maxr.size()) {
    rep(j, 0, maxc.size()) {
      if (!s.count({maxr[i], maxc[j]})) {
        ans = cr + cm;
        break;
      }
    }
  }

  prin(ans);
}

int main() {
  int t = 1;
  // scn(t);

  while (t--) {
    solve();
  }
  return 0;
}