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

#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
vector<int> bombs[3300000];
signed main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> tate(h), yoko(w);
  vector<P> res;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    res.emplace_back(a, b);
    tate[a]++;
    yoko[b]++;
  }
  int Tate = 0, Yoko = 0;
  rep(i, h) Tate = max(Tate, tate[i]);
  rep(i, w) Yoko = max(Yoko, yoko[i]);
  vector<int> kouhoTate, kouhoYoko;
  rep(i, h) {
    if (tate[i] == Tate)
      kouhoTate.push_back(i);
  }
  rep(i, w) {
    if (yoko[i] == Yoko)
      kouhoYoko.push_back(i);
  }
  int cnt = 0;
  rep(i, m) {
    int a = res[i].first, b = res[i].second;
    cnt += (tate[a] == Tate && yoko[b] == Yoko);
  }
  if (kouhoTate.size() * kouhoYoko.size() == cnt)
    cout << Tate + Yoko - 1 << endl;
  else
    cout << Tate + Yoko << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
signed main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<vector<int>> f(h, vector<int>(w, 0)),
      left(h + 1, vector<int>(w + 1, 0)), down(h + 1, vector<int>(w + 1, 0)),
      up(h + 1, vector<int>(w + 1, 0)), right(h + 1, vector<int>(w + 1, 0));
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    f[a][b] = 1;
  }
  rep(i, h) rep(j, w) {
    // left,up
    left[i][j + 1] = left[i][j] + (f[i][j] == 1);
    up[i + 1][j] = up[i][j] + (f[i][j] == 1);
  }
  for (int i = h - 1; i >= 0; i--)
    for (int j = w - 1; j >= 0; j--) {
      right[i][j] = right[i][j + 1] + (f[i][j] == 1);
      down[i][j] = down[i + 1][j] + (f[i][j] == 1);
    }
  int ret = 0;
  rep(i, h + 1) rep(j, w + 1) {
    int tmp = up[i][j] + down[i][j] + right[i][j] + left[i][j];
    if (i < h && j < w) {
      if (f[i][j])
        tmp--;
    }
    ret = max(ret, tmp);
  }
  cout << ret << endl;

  return 0;
}