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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

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

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

void solve() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<pair<int, int>> bomb(M);
  for (int i = 0; i < M; ++i)
    cin >> bomb[i].first >> bomb[i].second;

  for (int i = 0; i < M; ++i) {
    --bomb[i].first;
    --bomb[i].second;
  }
  vector<int> cnth(H), cntw(W);
  for (auto p : bomb) {
    ++cnth[p.first];
    ++cntw[p.second];
  }
  int maxi = 0;
  for (int x : cntw)
    chmax(maxi, x);
  set<int> st;
  for (int i = 0; i < W; ++i)
    if (cntw[i] == maxi)
      st.insert(i);
  int kind = (int)st.size();
  vector<int> cnt(H);
  for (auto p : bomb)
    if (st.count(p.second))
      ++cnt[p.first];
  int res = 0;
  for (int i = 0; i < H; ++i) {
    int tmp = cnth[i] + maxi;
    if (cnt[i] == kind)
      chmax(res, tmp - 1);
    else
      chmax(res, tmp);
  }
  cout << res << '\\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

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

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

void solve() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> h(M), w(M);
  for (int i = 0; i < M; ++i)
    cin >> h[i] >> w[i];

  vector<int> ch = h, cw = w;
  sort(ch.begin(), ch.end());
  sort(cw.begin(), cw.end());
  ch.erase(unique(ch.begin(), ch.end()), ch.end());
  cw.erase(unique(cw.begin(), cw.end()), cw.end());
  int hh = (int)ch.size(), ww = (int)cw.size();
  for (int i = 0; i < M; ++i) {
    h[i] = lower_bound(ch.begin(), ch.end(), h[i]) - ch.begin();
    w[i] = lower_bound(cw.begin(), cw.end(), w[i]) - cw.begin();
  }
  vector<vector<bool>> bomb(hh, vector<bool>(ww, false));
  for (int i = 0; i < M; ++i)
    bomb[h[i]][w[i]] = true;
  vector<int> cnth(hh), cntw(ww);
  for (int i = 0; i < M; ++i) {
    ++cnth[h[i]];
    ++cntw[w[i]];
  }
  ll res = 0;
  for (int i = 0; i < hh; ++i) {
    for (int j = 0; j < ww; ++j) {
      ll tmp = cnth[i] + cntw[j];
      if (bomb[i][j])
        --tmp;
      chmax(res, tmp);
    }
  }
  cout << res << '\\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}