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

#include <bits/stdc++.h>
#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define set_zero(a) memset(a, 0, sizeof(a));
#define ll long long
#define PI 2 * acos(0.0)
#define MAX 1000000000
#define INF 1000000
#define M 1000000007
using namespace std;
int main() {
  fast_io ll h, w, m;
  cin >> h >> w >> m;
  map<ll, ll> row, col;
  map<pair<ll, ll>, ll> mp;
  while (m--) {
    ll x, y;
    cin >> x >> y;
    row[x]++;
    col[y]++;
    mp[{x, y}]++;
  }
  ll maxrow = 0;
  ll rows = 0;
  ll colmn = 0;
  for (ll i = 1; i <= h; i++) {
    maxrow = max(maxrow, row[i]);
  }
  ll maxcol = 0;
  for (ll i = 1; i <= w; i++) {
    maxcol = max(maxcol, col[i]);
  }
  ll ans = maxrow + maxcol;
  vector<ll> ro, co;
  for (ll i = 1; i <= h; i++) {
    if (row[i] == maxrow) {
      ro.push_back(i);
    }
  }
  for (ll i = 1; i <= w; i++) {
    if (col[i] == maxcol) {
      co.push_back(i);
    }
  }
  bool ok = false;
  for (ll i = 0; i < ro.size(); i++) {
    for (ll j = 0; j < co.size(); j++) {
      if (mp[{ro[i], co[j]}] == 0) {
        ok = true;
        break;
      }
    }
  }
  if (ok == true) {
    cout << ans << endl;
  } else {
    cout << ans - 1 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define set_zero(a) memset(a, 0, sizeof(a));
#define ll long long
#define PI 2 * acos(0.0)
#define MAX 1000000000
#define INF 1000000
#define M 1000000007
using namespace std;
int main() {
  fast_io ll h, w, m;
  cin >> h >> w >> m;
  ll a[h][w];
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      a[i][j] = 0;
    }
  }
  ll rig[h][w];
  ll lef[h][w];
  ll top[h][w];
  ll dow[h][w];
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      rig[i][j] = 0;
    }
  }
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      lef[i][j] = 0;
    }
  }
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      top[i][j] = 0;
    }
  }
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      dow[i][j] = 0;
    }
  }
  while (m--) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    a[x][y] = 1;
  }
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      if (j == 0) {
        rig[i][j] = a[i][j];
      } else {
        rig[i][j] = rig[i][j - 1] + a[i][j];
      }
    }
  }
  for (ll i = 0; i < h; i++) {
    for (ll j = w - 1; j >= 0; j--) {
      if (j == w - 1) {
        lef[i][j] = a[i][j];
      } else {
        lef[i][j] = lef[i][j + 1] + a[i][j];
      }
    }
  }
  for (ll i = 0; i < w; i++) {
    for (ll j = 0; j < h; j++) {
      if (j == 0) {
        top[j][i] = a[j][i];
      } else {
        top[j][i] = top[j - 1][i] + a[j][i];
      }
    }
  }
  for (ll i = 0; i < w; i++) {
    for (ll j = h - 1; j >= 0; j--) {
      if (j == h - 1) {
        dow[j][i] = a[j][i];
      } else {
        dow[j][i] = top[j + 1][i] + a[j][i];
      }
    }
  }
  ll main_ans = 0;
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      ll row = max(rig[i][j], lef[i][j]);
      ll column = max(top[i][j], dow[i][j]);
      if (a[i][j] == 1) {
        main_ans = max(main_ans, row + column - 1);
      } else {
        main_ans = max(main_ans, row + column);
      }
    }
  }
  cout << main_ans << endl;
  return 0;
}