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

#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

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;
}

ll gcd(ll n, ll m) {
  ll tmp;
  while (m != 0) {
    tmp = n % m;
    n = m;
    m = tmp;
  }
  return n;
}

ll lcm(ll n, ll m) {
  return abs(n) / gcd(n, m) * abs(m); // gl=xy
}

using namespace std;

using P = pair<int, int>;
void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> x(m), y(m), r(h), c(w);
  map<P, int> mp;
  rep(i, m) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
    r[x[i]]++;
    c[y[i]]++;
  }
  int maxr = *max_element(all(r)), maxc = *max_element(all(c));
  ll a = 0, b = 0;
  rep(i, h) {
    if (maxr == r[i])
      ++a;
  }
  rep(i, w) {
    if (maxc == c[i])
      ++b;
  }
  int ans = maxr + maxc;
  ll cnt = 0;
  rep(i, m) {
    if (r[x[i]] == maxr && c[y[i]] == maxc)
      ++cnt;
  }
  if (cnt >= a * b)
    ans--;
  cout << ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

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;
}

ll gcd(ll n, ll m) {
  ll tmp;
  while (m != 0) {
    tmp = n % m;
    n = m;
    m = tmp;
  }
  return n;
}

ll lcm(ll n, ll m) {
  return abs(n) / gcd(n, m) * abs(m); // gl=xy
}

using namespace std;

using P = pair<int, int>;
void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  int x, y;
  vector<P> r(h), c(w);
  map<P, int> mp;
  rep(i, m) {
    cin >> x >> y;
    x--;
    y--;
    mp[P{x, y}]++;
    r[x].first++;
    c[y].first++;
    r[x].second = x;
    c[y].second = y;
  }
  sort(rall(r));
  sort(rall(c));
  int maxr = r[0].first, maxc = c[0].first;
  vector<int> s, t;
  for (auto x : r) {
    if (x.first == maxr)
      s.pb(x.second);
  }
  for (auto x : c) {
    if (x.first == maxc)
      t.pb(x.second);
  }
  int ans = maxr + maxc;
  bool flag = true;
  for (auto x : s) {
    for (auto y : t) {
      if (!mp.count(P{x, y}))
        flag = false;
    }
  }
  ans -= flag;
  cout << ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}