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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define mem0(a) memset(a, 0, sizeof(a))
#define mem1(a) memset(a, -1, sizeof(a))
#define memf(a) memset(a, false, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define endl "\\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define DEC(x) fixed << setprecision(x)
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / __gcd((a), (b))
const int M = 1000000007;
const int MM = 998244353;
const long double PI = acos(-1);
const long long INF = 2e18;

template <typename T, typename T1> T amax(T &a, T1 b) {
  if (b > a)
    a = b;
}
template <typename T, typename T1> T amin(T &a, T1 b) {
  if (b < a)
    a = b;
}

ll power(ll b, ll e) {
  if (e == 0)
    return 1;
  if (e & 1)
    return b * power(b * b, e / 2);
  return power(b * b, e / 2);
}
ll power(ll b, ll e, ll m) {
  if (e == 0)
    return 1;
  if (e & 1)
    return b * power(b * b % m, e / 2, m) % m;
  return power(b * b % m, e / 2, m);
}
ll modinv(ll a, ll m) { return power(a, m - 2, m); }

// #define _NCR_
const ll int N = 300005;
ll cr[N], cc[N];
set<pll> s;
set<ll> sr, sc;
// vector<vector<ll>>vr,vc;
int TLE_TERROR() {
  ll int h, w, m, l, r, mr = 1, mc = 1, ans, z, y;
  cin >> h >> w >> m;
  for (ll int i = 0; i < m; i++) {
    cin >> l >> r;
    cr[l]++;
    cc[r]++;
    s.insert({l, r});
    mr = max(mr, cr[l]);
    mc = max(mc, cc[r]);
  }
  ans = mr + mc;
  // cout<<ans<<endl;
  for (ll int i = 1; i <= h; i++) {
    if (cr[i] == mr) {
      sr.insert(i);
    }
  }
  for (ll int i = 1; i <= w; i++) {
    if (cc[i] == mc) {
      sc.insert(i);
    }
  }
  for (auto it = sr.begin(); it != sr.end(); it++) {
    for (auto itr = sc.begin(); itr != sc.end(); itr++) {
      z = (*it);
      y = (*itr);
      if (!s.count({z, y})) {
        cout << ans;
        return 0;
      }
    }
  }
  ans -= 1;
  cout << ans;

  return 0;
}

int main() {
  FAST
      // freopen("input.txt","r",stdin);
      // freopen("output.txt","w",stdout);
      ll int TESTS = 1;
// cin>>TESTS;
#ifdef _NCR_
  initialvalues();
#endif
  while (TESTS--)
    TLE_TERROR();
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define mem0(a) memset(a, 0, sizeof(a))
#define mem1(a) memset(a, -1, sizeof(a))
#define memf(a) memset(a, false, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define endl "\\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define DEC(x) fixed << setprecision(x)
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / __gcd((a), (b))
const int M = 1000000007;
const int MM = 998244353;
const long double PI = acos(-1);
const long long INF = 2e18;

template <typename T, typename T1> T amax(T &a, T1 b) {
  if (b > a)
    a = b;
}
template <typename T, typename T1> T amin(T &a, T1 b) {
  if (b < a)
    a = b;
}

ll power(ll b, ll e) {
  if (e == 0)
    return 1;
  if (e & 1)
    return b * power(b * b, e / 2);
  return power(b * b, e / 2);
}
ll power(ll b, ll e, ll m) {
  if (e == 0)
    return 1;
  if (e & 1)
    return b * power(b * b % m, e / 2, m) % m;
  return power(b * b % m, e / 2, m);
}
ll modinv(ll a, ll m) { return power(a, m - 2, m); }

// #define _NCR_
const int N = 300005;
ll cr[N], cc[N];
set<pll> s;
set<ll> sr, sc;
// vector<vector<ll>>vr,vc;
int TLE_TERROR() {
  ll int h, w, m, l, r, mr = 1, mc = 1, ans, z, y;
  cin >> h >> w >> m;
  for (ll int i = 0; i < m; i++) {
    cin >> l >> r;
    cr[l]++;
    cc[r]++;
    s.insert({l, r});
    amax(mr, cr[l]);
    amax(mc, cc[r]);
  }
  ans = mr + mc;
  // cout<<ans<<endl;
  for (ll int i = 1; i <= h; i++) {
    if (cr[i] == mr) {
      sr.insert(i);
    }
  }
  for (ll int i = 1; i <= w; i++) {
    if (cc[i] == mc) {
      sc.insert(i);
    }
  }
  for (auto it = sr.begin(); it != sr.end(); it++) {
    for (auto itr = sc.begin(); itr != sc.end(); itr++) {
      z = (*it);
      y = (*itr);
      if (!s.count({z, y})) {
        cout << ans;
        return 0;
      }
    }
  }
  ans -= 1;
  cout << ans;

  return 0;
}

int main() {
  FAST
      // freopen("input.txt","r",stdin);
      // freopen("output.txt","w",stdout);
      ll int TESTS = 1;
// cin>>TESTS;
#ifdef _NCR_
  initialvalues();
#endif
  while (TESTS--)
    TLE_TERROR();
  return 0;
}