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

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using P = pair<ll, ll>;
using um = unordered_map<ll, ll>;
#define fl cout << flush;
#define endl '\\n'
template <typename T> inline void prt(T v) { cout << v << '\\n'; }
template <typename T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = (ll)1e9 + 7;
const ll MOD2 = 998244353;
const ld pi = 3.14159265358979323846;
const ld eps = 1e-10;

signed main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  ll H;
  cin >> H;
  ll W;
  cin >> W;
  ll M;
  cin >> M;
  vector<ll> a(M, 0);
  vector<ll> b(M, 0);
  for (ll i = 0; i < M; ++i) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  um M2;
  for (auto p : a)
    M2[p]++;
  um D;
  for (auto p : b)
    D[p]++;
  ll MM = 0;
  ll DD = 0;
  for (auto p : M2)
    chmax(MM, p.second);
  for (auto p : D)
    chmax(DD, p.second);
  set<ll> MMM;
  set<ll> DDD;
  for (auto p : M2) {
    if (p.second == MM)
      MMM.emplace(p.first);
  }
  for (auto p : D) {
    if (p.second == DD)
      DDD.emplace(p.first);
  }
  int minus = 0;
  ll count = 0;
  for (ll i = 0; i < M; ++i) {
    if ((MMM.find(a[i]) != MMM.end()) && (DDD.find(b[i]) != DDD.end()))
      count++;
  }
  if (count == MMM.size() * DDD.size())
    minus = 1;
  if (minus)
    prt(MM + DD - 1);
  else
    prt(MM + DD);

  return 0;
}


Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using P = pair<ll, ll>;
using um = unordered_map<ll, ll>;
#define fl cout << flush;
#define endl '\\n'
template <typename T> inline void prt(T v) { cout << v << '\\n'; }
template <typename T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = (ll)1e9 + 7;
const ll MOD2 = 998244353;
const ld pi = 3.14159265358979323846;
const ld eps = 1e-10;
#define debug(v) cout << #v << ": ", prt(v);
template <typename A, typename B> inline void prt(pair<A, B> p) {
  cout << "(" << p.first << ", " << p.second << ")\\n";
}
template <typename A, typename B, typename C>
inline void prt(tuple<A, B, C> p) {
  cout << "(" << get<0>(p) << ", " << get<1>(p) << ", " << get<2>(p) << ")\\n";
}
inline void prt(bool p) {
  if (p)
    cout << "True" << '\\n';
  else
    cout << "False" << '\\n';
}
template <typename T> inline void prt(vector<T> v) {
  cout << '{';
  for (ll i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i < v.size() - 1)
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename T> inline void prt(deque<T> v) {
  cout << '{';
  for (ll i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i < v.size() - 1)
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename A, typename B> inline void prt(map<A, B> v) {
  cout << '{';
  ll c = 0;
  for (auto &p : v) {
    cout << p.first << ":" << p.second;
    c++;
    if (c != v.size())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename A, typename B> inline void prt(unordered_map<A, B> v) {
  cout << '{';
  ll c = 0;
  for (auto &p : v) {
    cout << p.first << ":" << p.second;
    c++;
    if (c != v.size())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename T> inline void prt(set<T> v) {
  cout << '{';
  for (auto i = v.begin(); i != v.end(); i++) {
    cout << *i;
    if (i != --v.end())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename T> inline void prt(multiset<T> v) {
  cout << '{';
  for (auto i = v.begin(); i != v.end(); i++) {
    cout << *i;
    if (i != --v.end())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
signed main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  ll H;
  cin >> H;
  ll W;
  cin >> W;
  ll M;
  cin >> M;
  vector<ll> a(M, 0);
  vector<ll> b(M, 0);
  vector<vector<bool>> check(H, vector<bool>(W, 0));
  for (ll i = 0; i < M; ++i) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    check[a[i]][b[i]] = 1;
  }
  um M2;
  for (auto p : a)
    M2[p]++;
  um D;
  for (auto p : b)
    D[p]++;
  ll MM = 0;
  ll DD = 0;
  for (auto p : M2)
    chmax(MM, p.second);
  for (auto p : D)
    chmax(DD, p.second);
  set<ll> MMM = {};
  set<ll> DDD = {};
  for (auto p : M2) {
    if (p.second == MM)
      MMM.emplace(p.first);
  }
  for (auto p : D) {
    if (p.second == DD)
      DDD.emplace(p.first);
  }
  bool minus = 0;
  ll count = 0;
  for (ll i = 0; i < M; ++i) {
    if (MMM.find(a[i]) != MMM.end() && DDD.find(b[i]) != DDD.end())
      count++;
  }
  if (count == MMM.size() * DDD.size())
    minus = 1;
  if (minus)
    prt(MM + DD - 1);
  else
    prt(MM + DD);
  /*debug(minus)
  debug(MM)
  debug(DD)
  debug(count)
  debug(MMM)
  debug(DDD)*/

  return 0;
}