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

#include <bits/stdc++.h>
using namespace std;

int main() {
  using coord = pair<int, int>;
  int H, W, M;
  cin >> H >> W >> M;

  set<coord> targets;
  int x, y;
  for (int i = 0; i < M; i++) {
    cin >> x >> y;
    x--;
    y--; // 1-indexed -> 0-indexed
    targets.insert(make_pair(x, y));
  }

  vector<int> Nrow(H);
  vector<int> Ncol(W);
  for (auto [x, y] : targets) {
    Nrow.at(x)++;
    Ncol.at(y)++;
  }

  vector<int> argmax_row;
  int cummax_row = -1;
  for (int h = 0; h < H; h++) {
    if (cummax_row > Nrow.at(h)) {
      continue;
    } else if (cummax_row < Nrow.at(h)) {
      cummax_row = Nrow.at(h);
      argmax_row.clear();
      argmax_row.emplace_back(h);
    } else {
      argmax_row.emplace_back(h);
    }
  }

  vector<int> argmax_col;
  int cummax_col = -1;
  for (int w = 0; w < W; w++) {
    if (cummax_col > Ncol.at(w)) {
      continue;
    } else if (cummax_col < Ncol.at(w)) {
      cummax_col = Ncol.at(w);
      argmax_col.clear();
      argmax_col.emplace_back(w);
    } else {
      argmax_col.emplace_back(w);
    }
  }

  int64_t Ncross = (int64_t)argmax_row.size() * argmax_col.size();
  if (Ncross > M) {
    int ans = cummax_row + cummax_col;
    cout << ans << endl;
  } else {
    vector<bool> filled(Ncross);
    for (size_t i = 0; i < argmax_row.size(); i++) {
      for (size_t j = 0; j < argmax_col.size(); j++) {
        size_t id = i * argmax_col.size() + j;
        filled.at(id) =
            targets.count(make_pair(argmax_row.at(i), argmax_col.at(j)));
      }
    }
    bool vacant_exist = false;
    for (auto b : filled) {
      vacant_exist |= !b;
    }

    int ans =
        vacant_exist ? cummax_row + cummax_col : cummax_row + cummax_col - 1;
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  using coord = pair<int, int>;
  int H, W, M;
  cin >> H >> W >> M;

  set<coord> targets;
  int x, y;
  for (int i = 0; i < M; i++) {
    cin >> x >> y;
    x--;
    y--; // 1-indexed -> 0-indexed
    targets.insert(make_pair(x, y));
  }

  vector<int> Nrow(H);
  vector<int> Ncol(W);
  for (auto [x, y] : targets) {
    Nrow.at(x)++;
    Ncol.at(y)++;
  }

  vector<int> argmax_row;
  int cummax_row = -1;
  for (int h = 0; h < H; h++) {
    if (cummax_row > Nrow.at(h)) {
      continue;
    } else if (cummax_row < Nrow.at(h)) {
      cummax_row = Nrow.at(h);
      argmax_row.clear();
      argmax_row.emplace_back(h);
    } else {
      argmax_row.emplace_back(h);
    }
  }

  vector<int> argmax_col;
  int cummax_col = -1;
  for (int w = 0; w < W; w++) {
    if (cummax_col > Ncol.at(w)) {
      continue;
    } else if (cummax_col < Ncol.at(w)) {
      cummax_col = Ncol.at(w);
      argmax_col.clear();
      argmax_col.emplace_back(w);
    } else {
      argmax_col.emplace_back(w);
    }
  }

  int Ncross = (int)argmax_row.size() * argmax_col.size();
  if (Ncross > M) {
    int ans = cummax_row + cummax_col;
    cout << ans << endl;
  } else {
    vector<bool> filled(Ncross);
    for (size_t i = 0; i < argmax_row.size(); i++) {
      for (size_t j = 0; j < argmax_col.size(); j++) {
        size_t id = i * argmax_col.size() + j;
        filled.at(id) =
            targets.count(make_pair(argmax_row.at(i), argmax_col.at(j)));
      }
    }
    bool vacant_exist = false;
    for (auto b : filled) {
      vacant_exist |= !b;
    }

    int ans =
        vacant_exist ? cummax_row + cummax_col : cummax_row + cummax_col - 1;
    cout << ans << endl;
  }
}