Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#ifndef DEBUG
#pragma GCC optimize("O3,no-stack-protector")
#pragma GCC optimize("unroll-loops")

#if __cplusplus < 201703L
#pragma GCC target("avx")
#else
#pragma GCC target("avx2")
#endif

#endif

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define dump(x) cout << x << endl
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i <= i##_len; ++i)
#define rep3(i, l, r) for (int i = l, i##_len = (r); i < i##_len; ++i)
#define REP3(i, l, r) for (int i = l, i##_len = (r); i <= i##_len; ++i)
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
typedef int64_t Int;
typedef long double Ld;

using namespace std;
using Graph = vector<vector<Int>>;

const Ld pi = M_PI;
const Int MOD = 1000000007;
const Int INF = 1LL << 62;

Int Floor(Int a, Int b) { return (a - (a % b)) / b; }

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  Int H, W, M;
  cin >> H >> W >> M;

  set<pair<Int, Int>> s;
  vector<Int> hcnt(H), wcnt(W);
  for (Int i = 0; i < M; i++) {
    Int h, w;
    cin >> h >> w;
    h--;
    w--;

    s.insert({h, w});
    hcnt[h]++;
    wcnt[w]++;
  }

  Int a = 0, b = 0;
  for (Int i = 0; i < H; i++) {
    chmax(a, hcnt[i]);
  }
  for (Int i = 0; i < W; i++) {
    chmax(b, wcnt[i]);
  }

  vector<Int> hmax, wmax;
  for (Int i = 0; i < H; i++) {
    if (hcnt[i] == a) {
      hmax.push_back(i);
    }
  }
  for (Int i = 0; i < W; i++) {
    if (wcnt[i] == b) {
      wmax.push_back(i);
    }
  }
  Int ans = a + b - 1;
  for (auto h : hmax) {
    for (auto w : wmax) {
      if (s.find({h, w}) == s.end()) {
        ans++;
        cout << ans << endl;
        return 0;
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#ifndef DEBUG
#pragma GCC optimize("O3,no-stack-protector")
#pragma GCC optimize("unroll-loops")

#if __cplusplus < 201703L
#pragma GCC target("avx")
#else
#pragma GCC target("avx2")
#endif

#endif

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define dump(x) cout << x << endl
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i <= i##_len; ++i)
#define rep3(i, l, r) for (int i = l, i##_len = (r); i < i##_len; ++i)
#define REP3(i, l, r) for (int i = l, i##_len = (r); i <= i##_len; ++i)
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
typedef int64_t Int;
typedef long double Ld;

using namespace std;
using Graph = vector<vector<Int>>;

const Ld pi = M_PI;
const Int MOD = 1000000007;
const Int INF = 1LL << 62;

Int Floor(Int a, Int b) { return (a - (a % b)) / b; }

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  Int H, W, M;
  cin >> H >> W >> M;

  set<pair<Int, Int>> s;
  map<Int, Int> hcnt, wcnt;
  // vector<Int> hcnt(H), wcnt(W);
  for (Int i = 0; i < M; i++) {
    Int h, w;
    cin >> h >> w;
    h--;
    w--;

    s.insert({h, w});
    hcnt[h]++;
    wcnt[w]++;
  }

  Int ans = 0;
  for (auto [h, cnt1] : hcnt) {
    for (auto [w, cnt2] : wcnt) {
      Int tem = 0;
      if (s.count({h, w}) == 1) {
        tem = cnt1 + cnt2 - 1;
      } else {
        tem = cnt1 + cnt2;
      }

      chmax(ans, tem);
    }
  }

  cout << ans << endl;

  return 0;
}