Problem p02580 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

#define MAXX 200010

#define PI 3.14159265358979323846264338327950

#define PB push_back
#define F first
#define S second

#define fixx(n) fixed << setprecision(n)

#define ll long long

#define mod 1000000007

ll h, w, t;
ll x, y;
ll a[MAXX];
ll b[MAXX];

int main() {
  FAST;
  cin >> h >> w >> t;
  map<ll, bool> m;
  while (t--) {
    cin >> x >> y;
    a[x]++;
    b[y]++;
    ll k = (x - 1) * w + y;
    m[k] = true;
  }
  vector<pair<ll, ll>> v;
  for (ll i = 1; i <= h; i++) {
    v.push_back({a[i], i});
  }
  vector<pair<ll, ll>> p;
  for (ll i = 1; i <= w; i++) {
    p.push_back({b[i], i});
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  sort(p.begin(), p.end());
  reverse(p.begin(), p.end());
  x = v[0].first;
  y = p[0].first;
  ll k = x + y;
  ll ans = k - 1;
  for (ll i = 0; i < v.size(); i++) {
    bool f = false;
    for (ll j = 0; j < p.size(); j++) {
      ll c = v[i].first;
      ll d = p[j].first;
      if (d < y) {
        break;
      }
      if (c < x) {
        f = true;
        break;
      }
      c = v[i].second;
      d = p[j].second;
      ll sum = (c - 1) * w + d;
      if (m[sum] == false) {
        ans++;
        f = true;
        break;
      }
    }
    if (f == true) {
      break;
    }
  }
  int h2 = 0;
  cout << ans << endl;
  return 0;
}

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

#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

#define MAXX 500005

#define PI 3.14159265358979323846264338327950

#define PB push_back
#define F first
#define S second

#define fixx(n) fixed << setprecision(n)

#define ll long long

#define mod 1000000007

ll h, w, t;
ll x, y;
ll a[MAXX];
ll b[MAXX];

int main() {
  FAST;
  cin >> h >> w >> t;
  map<ll, bool> m;
  while (t--) {
    cin >> x >> y;
    a[x]++;
    b[y]++;
    ll k = (x - 1) * w + y;
    m[k] = true;
  }
  vector<pair<ll, ll>> v;
  for (ll i = 1; i <= h; i++) {
    v.push_back({a[i], i});
  }
  vector<pair<ll, ll>> p;
  for (ll i = 1; i <= w; i++) {
    p.push_back({b[i], i});
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  sort(p.begin(), p.end());
  reverse(p.begin(), p.end());
  x = v[0].first;
  y = p[0].first;
  ll k = x + y;
  ll ans = k - 1;
  for (ll i = 0; i < v.size(); i++) {
    bool f = false;
    for (ll j = 0; j < p.size(); j++) {
      ll c = v[i].first;
      ll d = p[j].first;
      if (d < y) {
        break;
      }
      if (c < x) {
        f = true;
        break;
      }
      c = v[i].second;
      d = p[j].second;
      ll sum = (c - 1) * w + d;
      if (m[sum] == false) {
        ans++;
        f = true;
        break;
      }
    }
    if (f == true) {
      break;
    }
  }
  int h2 = 0;
  cout << ans << endl;
  return 0;
}