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

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <chrono>
using namespace std::chrono;
#define fl(i, z, n) for (int i = z; i <= (n); i++)
#define flr(i, z, n) for (int i = z; i >= (n); i--)
using namespace std;
#define ll long long
#define pa(a, s)                                                               \\
  fl(i, 0, s - 1) cout << *(a + i) << " ";                                     \\
  cout << nl;
#define pv(v)                                                                  \\
  fl(i, 0, v.size() - 1) cout << v[i] << " ";                                  \\
  cout << nl;
#define read(v) fl(i, 0, v.size() - 1) cin >> v[i];
#define all(v) v.begin(), v.end()
#define pll pair<ll, ll>
#define vpll vector<pll>
#define vll vector<ll>
#define sll set<ll>
#define spll set<pll>
#define debnl(x) cout << #x << "=" << x << nl;
#define deb(x) cout << #x << "=" << x << " ";
#define mkp(x, y) make_pair(x, y)
#define ump unordered_map
#define ff first
#define ss second
#define in insert
#define sz size
#define len length
#define nl '\\n'
#define pb push_back
#define in insert
#define ub upper_bound
#define lb lower_bound
#define ld long double
const int MXN = 3e5 + 10;
vll x(MXN, 0), y(MXN, 0);
ll mod = 1e9 + 7;
void solve() {
  ll h, w, m;
  cin >> h >> w >> m;
  set<pll> ss;
  fl(i, 0, m - 1) {
    ll a, b;
    cin >> a >> b;
    x[a]++;
    y[b]++;
    ss.insert({a, b});
  }
  int mx = 0, my = 0;
  set<ll> xx, yy;
  mx = *max_element(x.begin(), x.end());
  my = *max_element(y.begin(), y.end());
  fl(i, 1, h) if (x[i] == mx) xx.insert(i);
  fl(i, 1, w) if (y[i] == my) yy.insert(i);
  ll ans = mx + my;
  int ok = 0;
  for (auto a : xx) {
    for (auto b : yy) {
      if (ss.find({a, b}) == ss.end()) {
        ok = 1;
        break;
      }
    }
  }
  if (!ok)
    ans--;
  cout << ans << nl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t; //comment for single test
  while (t--)
    solve();
}

Error: WA

Fixed submission:

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <chrono>
using namespace std::chrono;
#define fl(i, z, n) for (int i = z; i <= (n); i++)
#define flr(i, z, n) for (int i = z; i >= (n); i--)
using namespace std;
#define ll long long
#define pa(a, s)                                                               \\
  fl(i, 0, s - 1) cout << *(a + i) << " ";                                     \\
  cout << nl;
#define pv(v)                                                                  \\
  fl(i, 0, v.size() - 1) cout << v[i] << " ";                                  \\
  cout << nl;
#define read(v) fl(i, 0, v.size() - 1) cin >> v[i];
#define all(v) v.begin(), v.end()
#define pll pair<ll, ll>
#define vpll vector<pll>
#define vll vector<ll>
#define sll set<ll>
#define spll set<pll>
#define debnl(x) cout << #x << "=" << x << nl;
#define deb(x) cout << #x << "=" << x << " ";
#define mkp(x, y) make_pair(x, y)
#define ump unordered_map
#define ff first
#define ss second
#define in insert
#define sz size
#define len length
#define nl '\\n'
#define pb push_back
#define in insert
#define ub upper_bound
#define lb lower_bound
#define ld long double
const int MXN = 3e5 + 10;
vll x(MXN, 0), y(MXN, 0);
ll mod = 1e9 + 7;
void solve() {
  ll h, w, m;
  cin >> h >> w >> m;
  set<pll> ss;
  fl(i, 0, m - 1) {
    ll a, b;
    cin >> a >> b;
    x[a]++;
    y[b]++;
    ss.insert({a, b});
  }
  int mx = 0, my = 0;
  set<ll> xx, yy;
  mx = *max_element(x.begin(), x.end());
  my = *max_element(y.begin(), y.end());
  fl(i, 0, h) if (x[i] == mx) xx.insert(i);
  fl(i, 0, w) if (y[i] == my) yy.insert(i);
  ll ans = mx + my - 1;
  for (auto a : xx) {
    for (auto b : yy) {
      if (ss.find({a, b}) != ss.end()) {
        ans++;
        break;
      }
    }
  }
  cout << ans << nl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  cin >> t; // comment for single test
  while (t--)
    solve();
}