Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <string>
#include <vector>

#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long int
#define mod 1000000007
#define inf 1e18 + 42
#define endl "\n"
#define pi 3.1415926535897932384626433832795028841971693993751058
#define maxn 100005

#define out1(a) cout << #a << " " << a << endl
#define out2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl
#define out3(a, b, c)                                                          \
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \
       << endl

#define rep(i, a, b) for (int i = a; i < b; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define fori(it, A) for (auto it = A.begin(); it != A.end(); it++)

#define ft first
#define sd second
#define pb push_back
#define mp make_pair
#define pq priority_queue
#define all(x) (x).begin(), (x).end()
#define zero(x) memset(x, 0, sizeof(x));
#define ceil(a, b) (a + b - 1) / b

using namespace std;

int binpow(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}

// START OF CODE ->->->->->->->

void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  int mx_r = 0;
  int mx_c = 0;
  vector<pair<int, int>> p;
  map<pair<int, int>, int> chk;
  map<int, int> f, s;
  rep(i, 0, m) {
    int x, y;
    cin >> x >> y;
    s[x]++;
    f[y]++;
    chk[{x, y}]++;
    p.pb({x, y});
  }
  vector<int> r, c;
  fori(it, s) {
    if (it->sd > mx_r) {
      mx_r = it->sd;
    }
  }
  fori(it, s) {
    if (it->sd == mx_r) {
      r.pb(it->ft);
    }
  }
  fori(it, f) {
    if (it->sd > mx_c) {
      mx_c = it->sd;
    }
  }
  fori(it, f) {
    if (it->sd == mx_c) {
      c.pb(it->ft);
    }
  }
  // rep(i,0,r.size()){
  //     out1(r[i]);
  // }
  // rep(i,0,c.size()){
  //     out1(c[i]);
  // }
  int ans = 0;
  rep(i, 0, r.size()) {
    rep(j, 0, c.size()) {
      if (chk[{r[i], c[j]}] > 0) {
        ans = max(ans, mx_r + mx_c - 1);
      } else {
        ans = max(ans, mx_r + mx_c);
      }
    }
  }

  cout << ans << endl;
}

// END OF CODE ->->->->->->->->

signed main() {
  fast;
  int t = 1;
  // cin>>t;

  while (t--) {
    solve();
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <string>
#include <vector>

#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long int
#define mod 1000000007
#define inf 1e18 + 42
#define endl "\n"
#define pi 3.1415926535897932384626433832795028841971693993751058
#define maxn 100005

#define out1(a) cout << #a << " " << a << endl
#define out2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl
#define out3(a, b, c)                                                          \
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \
       << endl

#define rep(i, a, b) for (int i = a; i < b; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define fori(it, A) for (auto it = A.begin(); it != A.end(); it++)

#define ft first
#define sd second
#define pb push_back
#define mp make_pair
#define pq priority_queue
#define all(x) (x).begin(), (x).end()
#define zero(x) memset(x, 0, sizeof(x));
#define ceil(a, b) (a + b - 1) / b

using namespace std;

int binpow(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}

// START OF CODE ->->->->->->->

void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  int mx_r = 0;
  int mx_c = 0;
  vector<pair<int, int>> p;
  map<pair<int, int>, int> chk;
  map<int, int> f, s;
  rep(i, 0, m) {
    int x, y;
    cin >> x >> y;
    s[x]++;
    f[y]++;
    chk[{x, y}]++;
    p.pb({x, y});
  }
  vector<int> r, c;
  fori(it, s) {
    if (it->sd > mx_r) {
      mx_r = it->sd;
    }
  }
  fori(it, s) {
    if (it->sd == mx_r) {
      r.pb(it->ft);
    }
  }
  fori(it, f) {
    if (it->sd > mx_c) {
      mx_c = it->sd;
    }
  }
  fori(it, f) {
    if (it->sd == mx_c) {
      c.pb(it->ft);
    }
  }
  // rep(i,0,r.size()){
  //     out1(r[i]);
  // }
  // rep(i,0,c.size()){
  //     out1(c[i]);
  // }
  int ans = 0;
  rep(i, 0, r.size()) {
    rep(j, 0, c.size()) {
      if (chk[{r[i], c[j]}] > 0) {
        ans = max(ans, mx_r + mx_c - 1);
      } else {
        ans = max(ans, mx_r + mx_c);
        cout << ans << endl;
        return;
      }
    }
  }

  cout << ans << endl;
}

// END OF CODE ->->->->->->->->

signed main() {
  fast;
  int t = 1;
  // cin>>t;

  while (t--) {
    solve();
  }
  return 0;
}