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

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vec;
typedef unordered_map<int, int> hash;
#define long long long
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define F first
#define S second
#define pb push_back
// #define mp make_pair
#define fill(a, v) memset(a, v, sizeof(a))
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define INF 0x3f3f3f3f // infinity for integers
#define JJ                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0) // input fast
#define MOD 1000000007

int main() {
  JJ;
  long h, w, m;
  cin >> h >> w >> m;
  long x, y;
  map<long, long> row;
  map<long, long> col;
  map<pair<long, long>, long> mp;
  // memset(arr,0,sizeof(arr));
  FOR(i, 0, m) {
    cin >> x >> y;
    row[x]++;
    col[y]++;
    mp[{x, y}] = 1;
  }
  long mxr = 0, mxr_freq = 0, mxc = 0, mxc_freq = 0;
  for (auto i = row.begin(); i != row.end(); i++) {
    if (i->second >= mxr_freq) {
      mxr_freq = i->second;
      mxr = i->first;
    }
  }
  for (auto i = col.begin(); i != col.end(); i++) {
    if (i->second >= mxc_freq) {
      mxc_freq = i->second;
      mxc = i->first;
    }
  }
  // checking all columns in row with max freq
  long col_bombs = 0, row_bombs = 0;
  // int c=0,r=0;
  FOR(j, 1, w + 1) {
    if (col[j] + mxr_freq - mp[{mxr, j}] >= col_bombs) {
      // c=j;
      col_bombs = col[j] + mxr_freq - mp[{mxr, j}];
    }
  }
  // checking all rows in col with max freq
  FOR(i, 1, h + 1) {
    if (row[i] + mxc_freq - mp[{i, mxc}] >= row_bombs) {
      // r=i;
      row_bombs = row[i] + mxc_freq - mp[{i, mxc}];
    }
  }
  long ans = max(row_bombs, col_bombs);
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vec;
typedef unordered_map<int, int> hash;
#define long long long
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define fill(a, v) memset(a, v, sizeof(a))
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define INF 0x3f3f3f3f // infinity for integers
#define JJ                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0) // input fast
#define MOD 1000000007

int main() {
  JJ;
  long h, w, m;
  cin >> h >> w >> m;
  long x, y;
  map<long, long> row;
  map<long, long> col;
  long arr[h + 1][w + 1];
  memset(arr, 0, sizeof(arr));
  FOR(i, 0, m) {
    cin >> x >> y;
    row[x]++;
    col[y]++;
    arr[x][y] = 1;
  }
  long mxr = 0, mxr_freq = 0, mxc = 0, mxc_freq = 0;
  for (auto i = row.begin(); i != row.end(); i++) {
    if (i->second >= mxr_freq) {
      mxr_freq = i->second;
      mxr = i->first;
    }
  }
  for (auto i = col.begin(); i != col.end(); i++) {
    if (i->second >= mxc_freq) {
      mxc_freq = i->second;
      mxc = i->first;
    }
  }
  // checking all columns in row with max freq
  long col_bombs = 0, row_bombs = 0;
  // int c=0,r=0;
  FOR(j, 1, w + 1) {
    if (col[j] + mxr_freq - arr[mxr][j] >= col_bombs) {
      // c=j;
      col_bombs = col[j] + mxr_freq - arr[mxr][j];
    }
  }
  // checking all rows in col with max freq
  FOR(i, 1, h + 1) {
    if (row[i] + mxc_freq - arr[i][mxc] >= row_bombs) {
      // r=i;
      row_bombs = row[i] + mxc_freq - arr[i][mxc];
    }
  }
  long ans = max(row_bombs, col_bombs);
  cout << ans << endl;

  return 0;
}