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

// 2020-08-23 00:17:26
#include <bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
using P = pair<int, int>;
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}

void answer() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<P> t(m);
  vector<int> hc(h), wc(w);
  int hm = 0, wm = 0;
  rep(i, m) {
    int hi, wi;
    cin >> hi >> wi;
    hi--;
    wi--;
    t[i] = P(hi, wi);
    chmax(hm, ++hc[hi]);
    chmax(wm, ++wc[wi]);
  }
  int hm_sz = 0;
  int wm_sz = 0;
  rep(i, h) hm_sz += (hc[i] == hm);
  rep(i, w) wm_sz += (wc[i] == wm);
  int rem = hm_sz * wm_sz;
  for (const auto [hi, wi] : t) {
    if (hc[hi] == hm && wc[wi] == wm)
      rem--;
  }
  int ans = hm + wm;
  if (rem == 0)
    ans--;
  cout << ans << '\\n';
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}

Error: TLE

Fixed submission:

// 2020-08-23 00:17:26
#include <bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
using P = pair<int, int>;
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}

void answer() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<P> t(m), hc(h), wc(w);
  rep(i, h) hc[i].second = i;
  rep(i, w) wc[i].second = i;
  rep(i, m) {
    int hi, wi;
    cin >> hi >> wi;
    hi--;
    wi--;
    t[i] = P(hi, wi);
    hc[hi].first++;
    wc[wi].first++;
  }
  sort(RALL(hc));
  sort(RALL(wc));
  int hm = hc[0].first;
  int wm = wc[0].first;
  set<P> s;
  for (const auto [hn, hi] : hc) {
    if (hn < hm)
      break;
    for (const auto [wn, wi] : wc) {
      if (wn < wm)
        break;
      s.emplace(P(hi, wi));
    }
  }
  for (const auto &a : t) {
    auto itr = s.lower_bound(a);
    if (itr != s.end() && (*itr) == a)
      s.erase(itr);
  }
  int ans = hm + wm;
  if (s.empty())
    ans--;
  cout << ans << '\\n';
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}