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

#include <bits/stdc++.h>
#define bug1(x) cout << #x << " = " << x << endl
#define bug2(x, y) cout << #x << " = " << x << " " << #y << " = " << y << endl
#define bug3(x, y, z)                                                          \\
  cout << #x << " = " << x << " " << #y << " = " << y << " " << #z << " = "    \\
       << z << endl
#define bug4(x, y, z, m)                                                       \\
  cout << #x << " = " << x << " " << #y << " = " << y << " " << #z << " = "    \\
       << z << " " << #m << " = " << m << endl

using namespace std;

class Debugger {
public:
  template <typename T>
  void printVector(vector<T> const &vec, bool printSize = true) {
    if (printSize) {
      cout << vec.size() << endl;
    }
    for (auto &elem : vec) {
      cout << elem << " ";
    }
    cout << endl;
  }

  template <typename T> void printMatrix(vector<vector<T>> const &matrix) {
    cout << matrix.size() << " ";
    if (!matrix.empty()) {
      cout << matrix[0].size();
    }
    cout << endl;
    for (auto &vec : matrix) {
      printVector(vec, false);
    }
    cout << endl;
  }
};

#define ll long long
const int MAXN = 3005;
const int MOD = 1000000007;

class TaskE {
private:
  Debugger debugger;

public:
  void solveOne(int it) {
    int rows, cols, bombs;
    cin >> rows >> cols >> bombs;
    unordered_map<int, int> rowToCols, colToRows;
    unordered_map<int, vector<int>> rowToValues;
    vector<int> x(bombs), y(bombs);
    set<pair<int, int>> all;
    for (int i = 0; i < bombs; i++) {
      cin >> x[i] >> y[i];
      rowToCols[x[i]]++;
      colToRows[y[i]]++;
      rowToValues[x[i]].push_back(y[i]);
      all.insert({x[i], y[i]});
    }
    assert(all.size() == bombs);
    int ans = 0;
    for (int i = 0; i < bombs; i++) {
      ans = max(ans, rowToCols[x[i]] + colToRows[y[i]] - 1);
    }
    multiset<int> s;
    for (auto item : colToRows) {
      s.insert(item.second);
    }
    for (auto item : rowToCols) {
      int row = item.first;
      for (int col : rowToValues[row]) {
        s.erase(s.find(colToRows[col]));
      }
      ans = max(ans, (!s.empty() ? *s.rbegin() : 0) + item.second);
      for (int col : rowToValues[row]) {
        s.insert(colToRows[col]);
      }
    }
    cout << ans << endl;
  }
  void solve() {
    int tc = 1;
    for (int it = 1; it <= tc; it++) {
      solveOne(it);
    }
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  TaskE solver;
  solver.solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define bug1(x) cout << #x << " = " << x << endl
#define bug2(x, y) cout << #x << " = " << x << " " << #y << " = " << y << endl
#define bug3(x, y, z)                                                          \\
  cout << #x << " = " << x << " " << #y << " = " << y << " " << #z << " = "    \\
       << z << endl
#define bug4(x, y, z, m)                                                       \\
  cout << #x << " = " << x << " " << #y << " = " << y << " " << #z << " = "    \\
       << z << " " << #m << " = " << m << endl

using namespace std;

class Debugger {
public:
  template <typename T>
  void printVector(vector<T> const &vec, bool printSize = true) {
    if (printSize) {
      cout << vec.size() << endl;
    }
    for (auto &elem : vec) {
      cout << elem << " ";
    }
    cout << endl;
  }

  template <typename T> void printMatrix(vector<vector<T>> const &matrix) {
    cout << matrix.size() << " ";
    if (!matrix.empty()) {
      cout << matrix[0].size();
    }
    cout << endl;
    for (auto &vec : matrix) {
      printVector(vec, false);
    }
    cout << endl;
  }
};

#define ll long long
const int MAXN = 3005;
const int MOD = 1000000007;

class TaskE {
private:
  Debugger debugger;

public:
  void solveOne(int it) {
    int rows, cols, bombs;
    cin >> rows >> cols >> bombs;
    unordered_map<int, int> rowToCols, colToRows;
    unordered_map<int, vector<int>> rowToValues;
    vector<int> x(bombs), y(bombs);
    for (int i = 0; i < bombs; i++) {
      cin >> x[i] >> y[i];
      rowToCols[x[i]]++;
      colToRows[y[i]]++;
      rowToValues[x[i]].push_back(y[i]);
    }
    int ans = 0;
    for (int i = 0; i < bombs; i++) {
      ans = max(ans, rowToCols[x[i]] + colToRows[y[i]] - 1);
    }
    multiset<int> s;
    for (auto item : colToRows) {
      s.insert(item.second);
    }
    for (auto item : rowToCols) {
      int row = item.first;
      for (int col : rowToValues[row]) {
        assert(s.count(colToRows[col]));
        s.erase(s.find(colToRows[col]));
      }
      ans = max(ans, *s.rbegin() + item.second);
      for (int col : rowToValues[row]) {
        s.insert(colToRows[col]);
      }
    }
    cout << ans << endl;
  }
  void solve() {
    int tc = 1;
    for (int it = 1; it <= tc; it++) {
      solveOne(it);
    }
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  TaskE solver;
  solver.solve();
  return 0;
}