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

#include <bits/stdc++.h>

#ifdef DEBUG
#define LOG(var, sep) (cerr << #var << " = " << (var) << (sep))

#define LOGM(msg) (cerr << (msg))

#define LOGV(vec, sep)                                                         \\
  {                                                                            \\
    cerr << #vec << " =";                                                      \\
    for (const auto &v : (vec))                                                \\
      cerr << " " << v;                                                        \\
    cerr << (sep);                                                             \\
  }

#define LOGA(arr, N, sep)                                                      \\
  {                                                                            \\
    cerr << #arr << " =";                                                      \\
    for (int i = 0; i < N; ++i)                                                \\
      cerr << " " << (arr)[i];                                                 \\
    cerr << (sep);                                                             \\
  }

#define LOGA2(arr, N, M, sep)                                                  \\
  {                                                                            \\
    cerr << #arr << " =\\n";                                                    \\
    for (int i = 0; i < N; ++i) {                                              \\
      for (int j = 0; j < M; ++j)                                              \\
        cerr << (arr)[i][j] << " ";                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << (sep);                                                             \\
  }
#else
#define LOG(var, sep)
#define LOGM(msg)
#define LOGV(vec, sep)
#define LOGA(arr, N, sep)
#define LOGA2(arr, N, M, sep)
#endif

using namespace std;
using ll = long long;
using ii = pair<int, int>;

const int MAX{300010};
const ll MOD{1000000007}, oo{1LL << 62};

int solve(int H, int W, const set<ii> &ps) {
  map<int, int> hs, vs;

  for (auto p : ps) {
    auto x = p.first, y = p.second;

    ++hs[x];
    ++vs[y];
  }

  vector<ii> xs, ys;

  for (auto p : hs) {
    auto x = p.first, qtd = p.second;

    xs.push_back(ii(qtd, x));
  }

  for (auto p : vs) {
    auto y = p.first, qtd = p.second;

    ys.push_back(ii(qtd, y));
  }

  sort(xs.begin(), xs.end(), greater<ii>());
  sort(ys.begin(), ys.end(), greater<ii>());

  auto A = xs.front().first, B = ys.front().first;

  set<int> bx, by;

  for (auto x : xs) {
    if (x.first != A)
      break;

    bx.insert(x.second);
  }

  for (auto y : ys) {
    if (y.first != B)
      break;

    by.insert(y.second);
  }

  for (auto x : bx)
    for (auto y : by)
      if (ps.count(ii(x, y)) == 0)
        return A + B;

  return A + B - 1;
}

int main() {
  ios::sync_with_stdio(false);

  int H, W, M;
  cin >> H >> W >> M;

  set<ii> ps;

  for (int i = 0; i < M; ++i) {
    ll x, y;
    cin >> x >> y;

    ps.insert(ii(x, y));
  }

  auto ans = solve(H, W, ps);

  cout << ans << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#ifdef DEBUG
#define LOG(var, sep) (cerr << #var << " = " << (var) << (sep))

#define LOGM(msg) (cerr << (msg))

#define LOGV(vec, sep)                                                         \\
  {                                                                            \\
    cerr << #vec << " =";                                                      \\
    for (const auto &v : (vec))                                                \\
      cerr << " " << v;                                                        \\
    cerr << (sep);                                                             \\
  }

#define LOGA(arr, N, sep)                                                      \\
  {                                                                            \\
    cerr << #arr << " =";                                                      \\
    for (int i = 0; i < N; ++i)                                                \\
      cerr << " " << (arr)[i];                                                 \\
    cerr << (sep);                                                             \\
  }

#define LOGA2(arr, N, M, sep)                                                  \\
  {                                                                            \\
    cerr << #arr << " =\\n";                                                    \\
    for (int i = 0; i < N; ++i) {                                              \\
      for (int j = 0; j < M; ++j)                                              \\
        cerr << (arr)[i][j] << " ";                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << (sep);                                                             \\
  }
#else
#define LOG(var, sep)
#define LOGM(msg)
#define LOGV(vec, sep)
#define LOGA(arr, N, sep)
#define LOGA2(arr, N, M, sep)
#endif

using namespace std;
using ll = long long;
using ii = pair<int, int>;

const int MAX{300010};
const ll MOD{1000000007}, oo{1LL << 62};

int hs[MAX], vs[MAX];

int solve(int H, int W, const unordered_set<ll> &ps) {
  int ans = 0;

  for (int x = 1; x <= H; ++x)
    for (int y = 1; y <= W; ++y) {
      auto z = x * MOD + y;
      ans = max(ans, hs[x] + vs[y] - ((int)ps.count(z)));
    }

  return ans;
}

int main() {
  ios::sync_with_stdio(false);

  int H, W, M;
  cin >> H >> W >> M;

  unordered_set<ll> ps;

  for (int i = 0; i < M; ++i) {
    ll x, y;
    cin >> x >> y;

    ps.insert(x * MOD + y);
    ++hs[x];
    ++vs[y];
  }

  auto ans = solve(H, W, ps);

  cout << ans << '\\n';

  return 0;
}