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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <iomanip> //cout << fixed << setprecision(15) << << endl;
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define ll long long int
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define P pair<int, int>

int mx8[] = {0, 0, 1, -1, -1, 1, -1, 1};
int my8[] = {-1, 1, 0, 0, -1, -1, 1, 1};
int mx4[] = {1, -1, 0, 0};
int my4[] = {0, 0, -1, 1};
const int MOD = 1000000007;

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  int m, h, w;
  cin >> h >> w >> m;
  vector<P> tate(h), yoko(w);
  rep(i, h) tate[i].second = i;
  rep(i, w) yoko[i].second = i;
  set<P> bomb;

  while (m--) {
    int hm, wm;
    cin >> hm >> wm;
    hm--;
    wm--;
    bomb.insert(P(hm, wm));
    tate[hm].first++;
    yoko[wm].first++;
  }
  sort(tate.rbegin(), tate.rend());
  sort(yoko.rbegin(), yoko.rend());
  int tatemax = tate[0].first, yokomax = yoko[0].first;

  bool ok = false;
  rep(i, h) {
    rep(j, w) {
      if (tate[i].first != tatemax)
        break;
      if (yoko[j].first != yokomax)
        break;
      auto itr = bomb.find(P(tate[i].second, yoko[j].second));
      if (itr == bomb.end()) {
        // 爆弾がなかった
        ok = true;
        break;
      }
    }
    if (ok)
      break;
  }
  int ans = tate[0].first + yoko[0].first;
  if (!ok)
    ans--;

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <iomanip> //cout << fixed << setprecision(15) << << endl;
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define ll long long int
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define P pair<int, int>

int mx8[] = {0, 0, 1, -1, -1, 1, -1, 1};
int my8[] = {-1, 1, 0, 0, -1, -1, 1, 1};
int mx4[] = {1, -1, 0, 0};
int my4[] = {0, 0, -1, 1};
const int MOD = 1000000007;

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  int h, w, m;
  cin >> h >> w >> m;
  vector<P> tate(h), yoko(w);
  bool bomb[h][w];
  rep(i, h) rep(j, w) bomb[i][j] = false;
  rep(i, h) tate[i].second = i;
  rep(i, w) yoko[i].second = i;

  while (m--) {
    int hm, wm;
    cin >> hm >> wm;
    hm--;
    wm--;
    bomb[hm][wm] = true;
    tate[hm].first++;
    yoko[wm].first++;
  }
  sort(tate.rbegin(), tate.rend());
  sort(yoko.rbegin(), yoko.rend());
  int tatemax = tate[0].first, yokomax = yoko[0].first;

  bool ok = false;
  rep(i, h) {
    rep(j, w) {
      if (tate[i].first != tatemax)
        break;
      if (yoko[j].first != yokomax)
        break;
      if (!bomb[tate[i].second][yoko[j].second]) {
        ok = true;
        break;
      }
    }
    if (ok)
      break;
  }
  int ans = tate[0].first + yoko[0].first;
  if (!ok)
    ans--;

  cout << ans << endl;
}