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

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void solve() {
  ll H, W, M;
  cin >> H >> W >> M;
  map<ll, ll> row, col;
  vector<P> items(M);
  set<P> used;
  rep(i, M) cin >> items[i].first >> items[i].second;
  rep(i, M) {
    P p = items[i];
    row[p.first]++;
    col[p.second]++;
    used.insert(p);
  }
  ll m = 0;
  rep2(i, 1, W + 1) { m = max(m, col[i]); }
  ll m2 = 0;
  rep2(i, 1, H + 1) { m2 = max(m2, row[i]); }
  vector<ll> tar1, tar2;
  rep2(i, 1, W + 1) {
    if (col[i] == m)
      tar1.push_back(i);
  }
  rep2(i, 1, H + 1) {
    if (row[i] == m2)
      tar2.push_back(i);
  }
  ll ans = 0;
  for (ll r : tar2) {
    for (ll c : tar1) {
      if (used.count({r, c})) {
        ans = max(ans, m + m2 - 1);
      } else {
        ans = max(ans, m + m2);
      }
      if (ans == m + m2)
        break;
    }
  }
  cout << ans << '\\n';
}

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void solve() {
  ll H, W, M;
  cin >> H >> W >> M;
  map<ll, ll> row, col;
  vector<P> items(M);
  set<P> used;
  rep(i, M) cin >> items[i].first >> items[i].second;
  rep(i, M) {
    P p = items[i];
    row[p.first]++;
    col[p.second]++;
    used.insert(p);
  }
  ll m = 0;
  rep2(i, 1, W + 1) { m = max(m, col[i]); }
  ll m2 = 0;
  rep2(i, 1, H + 1) { m2 = max(m2, row[i]); }
  vector<ll> tar1, tar2;
  rep2(i, 1, W + 1) {
    if (col[i] == m)
      tar1.push_back(i);
  }
  rep2(i, 1, H + 1) {
    if (row[i] == m2)
      tar2.push_back(i);
  }
  ll ans = 0;
  for (ll r : tar2) {
    for (ll c : tar1) {
      if (used.count({r, c})) {
        ans = max(ans, m + m2 - 1);
      } else {
        ans = max(ans, m + m2);
      }
    }
  }
  cout << ans << '\\n';
}

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