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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define RAP(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)

#include <iostream>
#include <vector>
using namespace std;

// 重複順列
vector<ll> buf;
void permutation(ll i, const ll size, const ll range_start,
                 const ll range_end) {
  if (i == size) {
    // ここで所望の作業を行う
    for (ll itr : buf) {
      cout << itr;
    }
  } else {
    for (ll j = range_start; j <= range_end; ++j) {
      buf[i] = j;
      permutation(i + 1, size, range_start, range_end);
    }
  }
}

int main(void) {
  map<ll, ll> H, W;
  vector<ll> MH, MW;
  map<pair<ll, ll>, ll> check;
  ll h, w, m, ans = -1, ansh = -1, answ = -1, a, b, mh = -1, mw = -1;
  cin >> h >> w >> m;
  RAP(i, m) {
    cin >> a >> b;
    H[a]++;
    W[b]++;
    check[make_pair(a, b)]++;
  }
  for (auto x : H) {
    if (x.second > ansh) {
      ansh = x.second;
      MH.clear();
      MH.push_back(x.first);
    } else if (x.second == ansh) {
      MH.push_back(x.first);
    }
  }
  for (auto y : W) {
    if (y.second > answ) {
      answ = y.second;
      MW.clear();
      MW.push_back(y.first);
    } else if (y.second == answ) {
      MW.push_back(y.first);
    }
  }
  bool flag = false;
  for (auto x : MH) {
    for (auto y : MW) {
      if (check[make_pair(x, y)] == 0) {
        flag = true;
        break;
      }
    }
    if (flag) {
      break;
    }
  }
  if (flag) {
    ans = ansh + answ;
  } else {
    ans = ansh + answ - 1;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define RAP(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)

#include <iostream>
#include <vector>
using namespace std;

// 重複順列
vector<ll> buf;
void permutation(ll i, const ll size, const ll range_start,
                 const ll range_end) {
  if (i == size) {
    // ここで所望の作業を行う
    for (ll itr : buf) {
      cout << itr;
    }
  } else {
    for (ll j = range_start; j <= range_end; ++j) {
      buf[i] = j;
      permutation(i + 1, size, range_start, range_end);
    }
  }
}

int main(void) {
  map<ll, ll> H, W;
  ll h, w, m, ans = -1, ansh = -1, answ = -1, a, b, mh = -1, mw = -1;
  cin >> h >> w >> m;
  vector<ll> MH, MW;
  vector<vector<ll>> check(h + 1, vector<ll>(w + 1));
  RAP(i, m) {
    cin >> a >> b;
    H[a]++;
    W[b]++;
    check[a][b]++;
  }
  for (auto x : H) {
    if (x.second > ansh) {
      ansh = x.second;
      MH.clear();
      MH.push_back(x.first);
    } else if (x.second == ansh) {
      MH.push_back(x.first);
    }
  }
  for (auto y : W) {
    if (y.second > answ) {
      answ = y.second;
      MW.clear();
      MW.push_back(y.first);
    } else if (y.second == answ) {
      MW.push_back(y.first);
    }
  }
  bool flag = false;
  for (auto x : MH) {
    for (auto y : MW) {
      if (check[x][y] == 0) {
        flag = true;
        break;
      }
    }
    if (flag) {
      break;
    }
  }
  if (flag) {
    ans = ansh + answ;
  } else {
    ans = ansh + answ - 1;
  }
  cout << ans << endl;
  return 0;
}