Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define rev2(i, a, b) for (int(i) = (a)-1; (i) >= (b); (i)--)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define yn                                                                     \
  { puts("Yes"); }                                                             \
  else {                                                                       \
    puts("No");                                                                \
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pll = pair<long long, long long>;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
long long TEN(int i) { return !i ? 1 : TEN(i - 1) * 10LL; }
template <class T, class U> bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} iosetup;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

int H, W, M;
int h[300005], w[300005];

int main() {
  cin >> H >> W >> M;
  rep(i, M) cin >> h[i] >> w[i];

  map<P, bool> mp;
  rep(i, M) mp[{h[i], w[i]}] = true;

  V<int> cntH(300005, 0), cntW(300005, 0);
  rep(i, M) cntH[h[i]]++, cntW[w[i]]++;

  int h_id = max_element(rng(cntH)) - cntH.begin();
  int w_id = max_element(rng(cntW)) - cntW.begin();

  vector<int> max_h, max_w;
  rep(i, 300005) {
    if (cntH[i] == cntH[h_id])
      max_h.pb(i);
    if (cntW[i] == cntW[w_id])
      max_w.pb(i);
  }

  int x = 0;
  bool ok = false;
  rep(i, max_h.size()) rep(j, max_w.size()) {
    x++;
    if (x >= TEN(7)) {
      cout << cntH[h_id] + cntW[w_id] - !ok << endl;

      return 0;
    }
    if (!mp[{max_h[i], max_w[j]}]) {
      ok = true;
    }
  }

  cout << cntH[h_id] + cntW[w_id] - !ok << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define rev2(i, a, b) for (int(i) = (a)-1; (i) >= (b); (i)--)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define yn                                                                     \
  { puts("Yes"); }                                                             \
  else {                                                                       \
    puts("No");                                                                \
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pll = pair<long long, long long>;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
long long TEN(int i) { return !i ? 1 : TEN(i - 1) * 10LL; }
template <class T, class U> bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} iosetup;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

int H, W, M;
int h[300005], w[300005];

int main() {
  cin >> H >> W >> M;
  rep(i, M) cin >> h[i] >> w[i];

  map<P, bool> mp;
  rep(i, M) mp[{h[i], w[i]}] = true;

  V<int> cntH(300005, 0), cntW(300005, 0);
  rep(i, M) cntH[h[i]]++, cntW[w[i]]++;

  int h_id = max_element(rng(cntH)) - cntH.begin();
  int w_id = max_element(rng(cntW)) - cntW.begin();

  vector<int> max_h, max_w;
  rep(i, 300005) {
    if (cntH[i] == cntH[h_id])
      max_h.pb(i);
    if (cntW[i] == cntW[w_id])
      max_w.pb(i);
  }

  int x = 0;
  bool ok = false;
  rep(i, max_h.size()) rep(j, max_w.size()) {
    x++;
    if (x >= 3 * TEN(5) + 2) {
      cout << cntH[h_id] + cntW[w_id] - !ok << endl;

      return 0;
    }
    if (!mp[{max_h[i], max_w[j]}]) {
      ok = true;
    }
  }

  cout << cntH[h_id] + cntW[w_id] - !ok << endl;

  return 0;
}