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

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPr(i, n) for (int i = (n)-1; i >= 0; --i)
#define FORq(i, m, n) for (int i = (m); i <= (n); ++i)
#define FORqr(i, m, n) for (int i = (n); i >= (m); --i)
#define MP make_pair
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x, 0, sizeof(x))
#define FILL(x, y) memset(x, y, sizeof(x))
#define MM(x) memset(x, -1, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define DB(x) cerr << #x << " = " << x << endl
#define DB2(x, y)                                                              \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\\n";
#define DEBUG                                                                  \\
  int x12345;                                                                  \\
  cin >> x12345;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
const long long MOD = 1e9 + 7;
///////////////////////////////////////////////
// for template
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
///////////////////////////////////////////////
/// 🍈( '-' 🍈 |AC|
int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> hcount(H);
  vector<int> wcount(W);
  vector<PII> bomb;
  REP(i, M) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    bomb.push_back(MP(h, w));
    hcount[h]++;
    wcount[w]++;
  }

  int ans = 0;
  int HM, WM;
  HM = 0;
  WM = 0;
  REP(i, H) { chmax(HM, hcount[i]); }
  REP(j, W) { chmax(WM, wcount[j]); }

  ll areabomb = 0;
  ll HMc, WMc;
  HMc = 0;
  WMc = 0;
  REP(i, H) {
    if (HM == hcount[i])
      HMc++;
  }
  REP(j, W) {
    if (WM == wcount[j])
      WMc++;
  }

  REP(i, M) {
    int h = bomb[i].first;
    int w = bomb[i].second;
    if ((hcount[h] == HM) and (wcount[w] == WM))
      areabomb++;
  }

  ans = HM + WM - 1;
  if (areabomb != HMc * WMc)
    ans++;

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPr(i, n) for (int i = (n)-1; i >= 0; --i)
#define FORq(i, m, n) for (int i = (m); i <= (n); ++i)
#define FORqr(i, m, n) for (int i = (n); i >= (m); --i)
#define MP make_pair
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x, 0, sizeof(x))
#define FILL(x, y) memset(x, y, sizeof(x))
#define MM(x) memset(x, -1, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define DB(x) cerr << #x << " = " << x << endl
#define DB2(x, y)                                                              \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\\n";
#define DEBUG                                                                  \\
  int x12345;                                                                  \\
  cin >> x12345;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
const long long MOD = 1e9 + 7;
///////////////////////////////////////////////
// for template
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
///////////////////////////////////////////////
/// 🍈( '-' 🍈 |AC|
int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> hcount(H);
  vector<int> wcount(W);
  map<PII, bool> bomb;
  REP(i, M) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    bomb[MP(h, w)] = true;
    hcount[h]++;
    wcount[w]++;
  }

  int ans = 0;
  REP(i, H) {
    REP(j, W) {
      int nans = hcount[i] + wcount[j];
      if (bomb[MP(i, j)])
        nans--;
      chmax(ans, nans);
    }
  }
  cout << ans << endl;
}