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

#include <bits/stdc++.h>
using namespace std;
// Optimisations
#pragma GCC target("avx2")
#pragma GCC optimization("unroll-loops")
#pragma GCC optimize("O2")
// shortcuts for functions
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define prec(n) fixed << setprecision(n)
#define n_l '\\n'
// make it python
#define gcd __gcd
#define append push_back
#define str to_string
// utility functions shortcuts
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define sswap(a, b)                                                            \\
  {                                                                            \\
    a = a ^ b;                                                                 \\
    b = a ^ b;                                                                 \\
    a = a ^ b;                                                                 \\
  }
#define swap(a, b)                                                             \\
  {                                                                            \\
    auto temp = a;                                                             \\
    a = b;                                                                     \\
    b = temp;                                                                  \\
  }
#define init(dp) memset(dp, -1, sizeof(dp));
#define set0(dp) memset(dp, 0, sizeof(dp));
#define bits(x) __builtin_popcount(x)
#define SORT(v) sort(all(v))
#define endl "\\n"
#define forr(i, n) for (ll i = 0; i < n; i++)
// declaration shortcuts
typedef long long int ll;
#define int ll
// Constants
constexpr int dx[] = {-1, 0, 1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, 1, -1};
constexpr ll INF = 1999999999999999997;
constexpr int inf = INT_MAX;
constexpr int MAXSIZE = int(1e6) + 5;
constexpr auto PI = 3.14159265358979323846L;
constexpr auto oo = numeric_limits<int>::max() / 2 - 2;
constexpr auto eps = 1e-6;
constexpr auto mod = 1000000007;
constexpr auto MOD = 1000000007;
constexpr auto MOD9 = 1000000009;
constexpr auto maxn = 100006;
// void IOfile(){
// freopen(file_name, reade_mode, stdin);
// freopen(file_name, write_mode, stdout);
// }
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
vector<int> row[3 * maxn];
vector<int> col[3 * maxn];

int32_t main() {
  fastio();
  int n, m, w;
  cin >> n >> m >> w;
  map<int, int> r, c;
  set<pair<int, int>> s;
  for (int i = 0; i < w; i++) {
    int a, b;
    cin >> a >> b;
    r[a]++;
    c[b]++;
    s.insert({a, b});
    row[a].push_back(b);
    col[b].push_back(a);
  }
  int ans = 0;
  set<pair<int, int>> cnt;
  for (int i = 1; i <= m; i++) {
    cnt.insert({col[i].size(), i});
  }
  for (int i = 1; i <= n; i++) {
    for (auto j : row[i]) {
      ans = max(ans, (int)row[i].size() + (int)col[j].size() - 1);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (auto j : row[i]) {
      cnt.erase({col[j].size(), j});
    }
    if (cnt.size() > 0) {
      pair<int, int> p = *cnt.rbegin();
      ans = max(ans, (int)row[i].size() + p.ff);
    }
    for (auto j : row[i]) {
      cnt.insert({col[j].size(), j});
    }
  }
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// Optimisations
#pragma GCC target("avx2")
#pragma GCC optimization("unroll-loops")
#pragma GCC optimize("O2")
// shortcuts for functions
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define prec(n) fixed << setprecision(n)
#define n_l '\\n'
// make it python
#define gcd __gcd
#define append push_back
#define str to_string
// utility functions shortcuts
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define sswap(a, b)                                                            \\
  {                                                                            \\
    a = a ^ b;                                                                 \\
    b = a ^ b;                                                                 \\
    a = a ^ b;                                                                 \\
  }
#define swap(a, b)                                                             \\
  {                                                                            \\
    auto temp = a;                                                             \\
    a = b;                                                                     \\
    b = temp;                                                                  \\
  }
#define init(dp) memset(dp, -1, sizeof(dp));
#define set0(dp) memset(dp, 0, sizeof(dp));
#define bits(x) __builtin_popcount(x)
#define SORT(v) sort(all(v))
#define endl "\\n"
#define forr(i, n) for (ll i = 0; i < n; i++)
// declaration shortcuts
typedef long long int ll;
#define int ll
// Constants
constexpr int dx[] = {-1, 0, 1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, 1, -1};
constexpr ll INF = 1999999999999999997;
constexpr int inf = INT_MAX;
constexpr int MAXSIZE = int(1e6) + 5;
constexpr auto PI = 3.14159265358979323846L;
constexpr auto oo = numeric_limits<int>::max() / 2 - 2;
constexpr auto eps = 1e-6;
constexpr auto mod = 1000000007;
constexpr auto MOD = 1000000007;
constexpr auto MOD9 = 1000000009;
constexpr auto maxn = 100006;
// void IOfile(){
// freopen(file_name, reade_mode, stdin);
// freopen(file_name, write_mode, stdout);
// }
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

int32_t main() {
  fastio();
  int n, m, w;
  cin >> n >> m >> w;
  int a[n + 1][m + 1];
  memset(a, 0, sizeof(a));
  vector<int> row(n + 1, 0);
  vector<int> col(m + 1, 0);
  for (int i = 1; i <= w; i++) {
    int x, y;
    cin >> x >> y;
    a[x][y] = 1;
  }
  for (int i = 1; i <= n; i++) {
    int s = 0;
    for (int j = 1; j <= m; j++) {
      s += a[i][j];
    }
    row[i] = s;
  }
  for (int i = 1; i <= m; i++) {
    int s = 0;
    for (int j = 1; j <= n; j++) {
      s += a[j][i];
    }
    col[i] = s;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      ans = max(ans, col[j] + row[i] - a[i][j]);
    }
  }
  cout << ans;
}