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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define debug(var)                                                             \\
  do {                                                                         \\
    cout << #var << " : ";                                                     \\
    view(var);                                                                 \\
  } while (0)
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
template <class T> void view(T e) { cout << e << endl; }
template <class T> void view(const vector<T> &v) {
  for (const auto &e : v) {
    cout << e << " ";
  }
  cout << endl;
}
template <class T> void view(const vector<vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

int main() {
  int H, W, m;
  cin >> H >> W >> m;
  vint sum_h(H, 0), sum_w(W, 0);
  vector<P> data(m);
  rep(i, m) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    data[i] = P(h, w);
    sum_h[h]++;
    sum_w[w]++;
  }
  int mx_h = *max_element(all(sum_h));
  int mx_w = *max_element(all(sum_w));
  int ct_h = 0, ct_w = 0;
  rep(i, H) if (sum_h[i] == mx_h) ct_h++;
  rep(i, W) if (sum_w[i] == mx_w) ct_w++;
  int sum = ct_h * ct_w;
  rep(i, m) {
    if (sum_h[data[i].first] == mx_h && sum_w[data[i].second] == mx_w) {
      sum--;
    }
  }
  int ans = mx_h + mx_w;
  if (sum != 0)
    cout << ans << endl;
  else
    cout << ans - 1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define debug(var)                                                             \\
  do {                                                                         \\
    cout << #var << " : ";                                                     \\
    view(var);                                                                 \\
  } while (0)
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
template <class T> void view(T e) { cout << e << endl; }
template <class T> void view(const vector<T> &v) {
  for (const auto &e : v) {
    cout << e << " ";
  }
  cout << endl;
}
template <class T> void view(const vector<vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

int main() {
  int H, W, m;
  cin >> H >> W >> m;
  vint sum_h(H, 0), sum_w(W, 0);
  set<P> st;
  rep(i, m) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    st.insert(P(h, w));
    sum_h[h]++;
    sum_w[w]++;
  }
  int mx_h = *max_element(all(sum_h));
  int mx_w = *max_element(all(sum_w));
  bool ok = false;
  rep(i, H) rep(j, W) {
    if (sum_h[i] == mx_h && sum_w[j] == mx_w) {
      if (!st.count(P(i, j)))
        ok = true;
    }
  }
  int ans = mx_h + mx_w;
  if (ok)
    cout << ans << endl;
  else
    cout << ans - 1 << endl;
}