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

#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;

using ll = long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;

template <class T> bool ChangeMax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <class T> bool ChangeMin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  static vector<P<int, int>> b(m);
  int cntr[300001] = {};
  int cntc[300001] = {};
  int maxr = 0, maxc = 0;
  for (int i = 0; i < m; ++i) {
    cin >> b[i].first >> b[i].second;
    ChangeMax(maxr, ++cntr[b[i].first]);
    ChangeMax(maxc, ++cntc[b[i].second]);
  }
  vector<int> r, c;
  for (int i = 1; i <= h; ++i)
    if (maxr == cntr[i])
      r.push_back(i);
  for (int i = 1; i <= w; ++i)
    if (maxc == cntc[i])
      c.push_back(i);
  int cntd = 0;
  for (auto e : b)
    cntd += binary_search(r.begin(), r.end(), e.first) &&
            binary_search(c.begin(), c.end(), e.second);
  cout << maxr + maxc - (cntd == (ll)r.size() * (int)c.size()) << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;

using ll = long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;

template <class T> bool ChangeMax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <class T> bool ChangeMin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  static vector<P<int, int>> b(m);
  int cntr[300001] = {};
  int cntc[300001] = {};
  int maxr = 0, maxc = 0;
  for (int i = 0; i < m; ++i) {
    cin >> b[i].first >> b[i].second;
    ChangeMax(maxr, ++cntr[b[i].first]);
    ChangeMax(maxc, ++cntc[b[i].second]);
  }
  static vector<P<int, int>> pairs;
  pairs.reserve(h * w);
  static vector<int> r, c;
  r.reserve(h);
  c.reserve(w);
  for (int i = 1; i <= h; ++i)
    if (cntr[i] == maxr)
      r.push_back(i);
  for (int j = 1; j <= w; ++j)
    if (cntc[j] == maxc)
      c.push_back(j);
  for (auto e1 : r)
    for (auto e2 : c)
      pairs.emplace_back(e1, e2);
  int dcnt = 0;
  for (auto e : b)
    dcnt += (int)binary_search(pairs.begin(), pairs.end(), e);
  cout << maxr + maxc - (dcnt == (int)pairs.size()) << endl;
}