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

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define N_TIMES(i, n) for (uint64_t i = 0; i < n; ++i)
#define N_TIMES_REV(i, n) for (int64_t i = n - 1; i >= 0; --i)

template <typename T>
using maximum_heap = std::priority_queue<T, std::vector<T>, std::less<T>>;
template <typename T>
using minimum_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
  for (typename std::vector<T>::size_type i = 0; i < v.size(); ++i) {
    is >> v[i];
  }
  return is;
}

using namespace std;

struct Point {
  uint64_t y, x;
  Point(uint64_t y, uint64_t x) : y(y), x(x) {}
  bool operator==(const Point &rhs) const { return y == rhs.y && x == rhs.x; }
  bool operator<(const Point &rhs) const {
    return y == rhs.y ? x < rhs.x : y < rhs.y;
  }
};

int main() {
  uint64_t H, W, M;
  cin >> H >> W >> M;

  vector<uint64_t> rows(H, 0), cols(W, 0);

  map<uint64_t, set<uint64_t>> hash;
  N_TIMES(m, M) {
    uint64_t h, w;
    cin >> h >> w;
    --h;
    --w;

    ++rows[h];
    ++cols[w];

    hash[h].insert(w);
  }

  uint64_t max_h = 0;
  set<uint64_t> max_h_index;
  N_TIMES(h, H) {
    if (max_h < rows[h]) {
      max_h = rows[h];
      max_h_index = set<uint64_t>();
      max_h_index.insert(h);
    } else if (max_h == rows[h]) {
      max_h_index.insert(h);
    }
  }

  uint64_t max_w = 0;
  set<uint64_t> max_w_index;
  N_TIMES(w, W) {
    if (max_w < cols[w]) {
      max_w = cols[w];
      max_w_index = set<uint64_t>();
      max_w_index.insert(w);
    } else if (max_w == cols[w]) {
      max_w_index.insert(w);
    }
  }

  bool flag = false;
  for (uint64_t y : max_h_index) {
    for (uint64_t x : max_w_index) {
      if (hash[y].find(x) == hash[y].end()) {
        flag = true;
        break;
      }
    }
  }

  if (flag) {
    cout << (max_h + max_w) << endl;
  } else {
    cout << (max_h + max_w - 1) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define N_TIMES(i, n) for (uint64_t i = 0; i < n; ++i)
#define N_TIMES_REV(i, n) for (int64_t i = n - 1; i >= 0; --i)

template <typename T>
using maximum_heap = std::priority_queue<T, std::vector<T>, std::less<T>>;
template <typename T>
using minimum_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
  for (typename std::vector<T>::size_type i = 0; i < v.size(); ++i) {
    is >> v[i];
  }
  return is;
}

using namespace std;

struct Point {
  uint64_t y, x;
  Point(uint64_t y, uint64_t x) : y(y), x(x) {}
  bool operator==(const Point &rhs) const { return y == rhs.y && x == rhs.x; }
  bool operator<(const Point &rhs) const {
    return y == rhs.y ? x < rhs.x : y < rhs.y;
  }
};

typedef bitset<300003> Bitset;

int main() {
  uint64_t H, W, M;
  cin >> H >> W >> M;

  vector<uint64_t> rows(H, 0), cols(W, 0);

  map<uint64_t, Bitset> hash;
  N_TIMES(m, M) {
    uint64_t h, w;
    cin >> h >> w;
    --h;
    --w;

    ++rows[h];
    ++cols[w];

    hash[h][w] = true;
  }

  uint64_t max_h = 0;
  set<uint64_t> max_h_index;
  N_TIMES(h, H) {
    if (max_h < rows[h]) {
      max_h = rows[h];
      max_h_index = set<uint64_t>();
      max_h_index.insert(h);
    } else if (max_h == rows[h]) {
      max_h_index.insert(h);
    }
  }

  uint64_t max_w = 0;
  Bitset max_w_index = 0;
  N_TIMES(w, W) {
    if (max_w < cols[w]) {
      max_w = cols[w];
      max_w_index = 0;
      max_w_index[w] = true;
    } else if (max_w == cols[w]) {
      max_w_index[w] = true;
    }
  }

  bool flag = false;
  Bitset w_full = 0;
  N_TIMES(w, W) { w_full[w] = true; }

  for (uint64_t y : max_h_index) {
    Bitset bs = hash[y] | max_w_index;
    if (bs == w_full) {
      flag = true;
      break;
    }
  }

  if (flag) {
    cout << (max_h + max_w) << endl;
  } else {
    cout << (max_h + max_w - 1) << endl;
  }

  return 0;
}