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

#include <bits/stdc++.h>
using namespace std;

#define REP(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ALL(c) c.begin(), c.end()
#define SZ(c) (int)c.size()
#define DBG1(x) cout << #x << " = " << x << "\\n"
#if USE_CP_REDIR
#define CP                                                                     \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#endif

using ll = long long;
const ll MOD = 1'000'000'007;
const int MN = 1'000'000;

int main() {
  ios::sync_with_stdio(false);
#ifdef CP
  CP;
#endif
  int H, W, M;
  cin >> H >> W >> M;
  map<int, map<int, int>> G;
  int h, w;
  unordered_map<int, int> Hc, Wc;
  int Hmax = 0, Wmax = 0;
  REP(i, 0, M) {
    cin >> h >> w;
    assert(h > 0 && h <= H);
    assert(w > 0 && w <= W);
    G[h - 1][w - 1] = 1;
    Hc[h - 1]++;
    Hmax = max(Hmax, Hc[h - 1]);
    Wc[w - 1]++;
    Wmax = max(Wmax, Wc[w - 1]);
  }
  int ans = Hmax + Wmax - 1;
  vector<int> Hcand, Wcand;
  for (auto &item : Hc) {
    if (item.second == Hmax)
      Hcand.push_back(item.first);
  }
  for (auto &item : Wc) {
    if (item.second == Wmax)
      Wcand.push_back(item.first);
  }
  for (auto &h : Hcand) {
    for (auto &w : Wcand) {
      if (G[h][w] == 0) {
        ans++;
        goto fim;
      }
    }
  }
fim:
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define REP(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ALL(c) c.begin(), c.end()
#define SZ(c) (int)c.size()
#define DBG1(x) cout << #x << " = " << x << "\\n"
#if USE_CP_REDIR
#define CP                                                                     \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#endif

using ll = long long;
const ll MOD = 1'000'000'007;
const int MN = 1'000'000;

int main() {
  ios::sync_with_stdio(false);
#ifdef CP
  CP;
#endif
  int H, W, M;
  cin >> H >> W >> M;
  map<int, map<int, int>> G;
  int h, w;
  unordered_map<int, int> Hc, Wc;
  REP(i, 0, M) {
    cin >> h >> w;
    assert(h > 0 && h <= H);
    assert(w > 0 && w <= W);
    G[h - 1][w - 1] = 1;
    Hc[h - 1]++;
    Wc[w - 1]++;
  }

  set<pair<int, int>> Hs, Ws; // <targets, h/w>
  for (auto &item : Hc)
    Hs.insert(pair<int, int>(item.second, item.first));
  for (auto &item : Wc)
    Ws.insert(pair<int, int>(item.second, item.first));
  int targets = 0, ans = 0;
  int Hmax = (*Hs.rbegin()).first;
  for (auto Hiter = Hs.rbegin(); Hiter != Hs.rend(); Hiter++) {
    if ((*Hiter).first < Hmax)
      break;
    for (auto Witer = Ws.rbegin(); Witer != Ws.rend(); Witer++) {
      targets = (*Hiter).first + (*Witer).first;
      targets -= G[(*Hiter).second][(*Witer).second];
      ans = max(ans, targets);
      if (targets < ans) {
        break;
      }
    }
  }
  cout << ans << endl;
}