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

#include <bits/stdc++.h>
#ifdef __DEBUG__
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);

inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
  cout << "[" << p.first << ";" << p.second << "]";
  return os;
}
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const map<T, S> p) {
  for (auto el : p)
    cout << "[" << el.first << ";" << el.second << "]";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const deque<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
  vector<T> ret(sz);
  for (auto &elem : ret)
    cin >> elem;
  return ret;
}

int H, W, M;
// vector<pii> B;
vector<int> cntX, cntY;
map<int, map<int, int>> Bomb;
void input() {
  fast_io();
  cin >> H >> W >> M;
  cntX.assign(H, 0);
  cntY.assign(W, 0);
  // Bomb.assign(H,vector<int>(W,0));
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    // B.emplace_back(x,y);
    cntX[x]++;
    cntY[y]++;
    Bomb[x][y] = 1;
  }
}

void chmax(int &a, int b) {
  if (a < b)
    a = b;
}

int solve() {
  // vector<pii> cx,cy;
  // for(auto [x,c]:cntX) cx.emplace_back(c,x);
  // for(auto [y,c]:cntY) cy.emplace_back(c,y);
  // auto cmp = [](pii a, pii b){return a.first > b.first;};
  // sort(cx.begin(),cx.end(),cmp);
  // sort(cy.begin(),cy.end(),cmp);
  // DBG(cx)
  // DBG(cy)
  int mx = *max_element(cntX.begin(), cntX.end());
  int my = *max_element(cntY.begin(), cntY.end());
  vector<int> idx, idy;
  for (int i = 0; i < H; i++) {
    if (cntX[i] == mx)
      idx.push_back(i);
  }
  for (int i = 0; i < W; i++) {
    if (cntY[i] == my)
      idy.push_back(i);
  }

  int ans = mx + my - 1;
  for (int i : idx) {
    for (int j : idy) {
      if (!Bomb[i][j]) {
        ans++;
        cout << ans << endl;
        return 0;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

int main() {
  input();
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#ifdef __DEBUG__
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);

inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
  cout << "[" << p.first << ";" << p.second << "]";
  return os;
}
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const map<T, S> p) {
  for (auto el : p)
    cout << "[" << el.first << ";" << el.second << "]";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const deque<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
  vector<T> ret(sz);
  for (auto &elem : ret)
    cin >> elem;
  return ret;
}

int H, W, M;
// vector<pii> B;
vector<int> cntX, cntY;
vector<vector<int>> Bomb;
void input() {
  fast_io();
  cin >> H >> W >> M;
  cntX.assign(H, 0);
  cntY.assign(W, 0);
  Bomb.assign(H, vector<int>(W, 0));
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    // B.emplace_back(x,y);
    cntX[x]++;
    cntY[y]++;
    Bomb[x][y] = 1;
  }
}

void chmax(int &a, int b) {
  if (a < b)
    a = b;
}

int solve() {
  // vector<pii> cx,cy;
  // for(auto [x,c]:cntX) cx.emplace_back(c,x);
  // for(auto [y,c]:cntY) cy.emplace_back(c,y);
  // auto cmp = [](pii a, pii b){return a.first > b.first;};
  // sort(cx.begin(),cx.end(),cmp);
  // sort(cy.begin(),cy.end(),cmp);
  // DBG(cx)
  // DBG(cy)
  int mx = *max_element(cntX.begin(), cntX.end());
  int my = *max_element(cntY.begin(), cntY.end());
  vector<int> idx, idy;
  for (int i = 0; i < H; i++) {
    if (cntX[i] == mx)
      idx.push_back(i);
  }
  for (int i = 0; i < W; i++) {
    if (cntY[i] == my)
      idy.push_back(i);
  }

  int ans = mx + my - 1;
  for (auto i : idx) {
    for (auto j : idy) {
      if (!Bomb[i][j]) {
        ans++;
        cout << ans << endl;
        return 0;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

int main() {
  input();
  solve();
  return 0;
}