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

#include <bits/stdc++.h>

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define dump_1(x1) cerr << #x1 << ": " << x1 << endl
#define dump_2(x1, x2)                                                         \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define dump_3(x1, x2, x3)                                                     \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << endl
#define dump_4(x1, x2, x3, x4)                                                 \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << endl
#define dump_5(x1, x2, x3, x4, x5)                                             \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#define dump(...)                                                              \\
  CHOOSE((__VA_ARGS__, dump_5, dump_4, dump_3, dump_2, dump_1, ~))(__VA_ARGS__)
#define check(s) cerr << s << endl

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

using namespace std;

using ll = long long;

vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

const ll LINF = 2e18;
const int INF = 1e9;

void solve(ll H, ll W, ll M, std::vector<ll> h, std::vector<ll> w) {
  map<ll, ll> xcnt;
  map<ll, ll> ycnt;
  set<pair<ll, ll>> mp; // (y, x)

  rep(i, M) {
    if (ycnt.count(h.at(i))) {
      ycnt.at(h.at(i))++;
    } else {
      ycnt.insert({h.at(i), 1});
    }
    if (xcnt.count(w.at(i))) {
      xcnt.at(w.at(i))++;
    } else {
      xcnt.insert({w.at(i), 1});
    }
    mp.insert({h.at(i), w.at(i)});
  }

  vector<pair<ll, ll>> vpy;
  vector<pair<ll, ll>> vpx;

  for (auto vy : ycnt) {
    vpy.push_back({vy.second, vy.first});
  }
  for (auto vx : xcnt) {
    vpx.push_back({vx.second, vx.first});
  }

  sort(all(vpy));
  sort(all(vpx));

  reverse(all(vpy));
  reverse(all(vpx));

  ll my = vpy.at(0).first;
  ll mx = vpx.at(0).first;

  vector<pair<ll, ll>> vpyl;
  vector<pair<ll, ll>> vpxl;
  for (auto v : vpy) {
    if (v.first != my)
      break;
    vpyl.push_back(v);
  }
  for (auto v : vpx) {
    if (v.first != mx)
      break;
    vpxl.push_back(v);
  }

  ll ans = 0;
  bool b = false;

  for (auto vy : vpyl) {
    for (auto vx : vpxl) {
      ll y = vy.second;
      ll yc = vy.first;
      ll x = vx.second;
      ll xc = vx.first;
      if (mp.count({y, x})) {
        ans = max(ans, yc + xc - 1);
      } else {
        ans = max(ans, yc + xc);
        b = true;
        break;
      }
    }
    if (b)
      break;
  }

  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll H;
  scanf("%lld", &H);
  ll W;
  scanf("%lld", &W);
  ll M;
  scanf("%lld", &M);
  std::vector<ll> h(M);
  std::vector<ll> w(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &h[i]);
    scanf("%lld", &w[i]);
    // 0-indexed
    h[i]--;
    w[i]--;
  }
  solve(H, W, M, std::move(h), std::move(w));
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define dump_1(x1) cerr << #x1 << ": " << x1 << endl
#define dump_2(x1, x2)                                                         \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define dump_3(x1, x2, x3)                                                     \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << endl
#define dump_4(x1, x2, x3, x4)                                                 \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << endl
#define dump_5(x1, x2, x3, x4, x5)                                             \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#define dump(...)                                                              \\
  CHOOSE((__VA_ARGS__, dump_5, dump_4, dump_3, dump_2, dump_1, ~))(__VA_ARGS__)
#define check(s) cerr << s << endl

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

using namespace std;

using ll = long long;

vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

const ll LINF = 2e18;
const int INF = 1e9;

void solve(ll H, ll W, ll M, std::vector<ll> h, std::vector<ll> w) {
  map<ll, ll> xcnt;
  map<ll, ll> ycnt;
  set<pair<ll, ll>> mp; // (y, x)

  rep(i, M) {
    if (ycnt.count(h.at(i))) {
      ycnt.at(h.at(i))++;
    } else {
      ycnt.insert({h.at(i), 1});
    }
    if (xcnt.count(w.at(i))) {
      xcnt.at(w.at(i))++;
    } else {
      xcnt.insert({w.at(i), 1});
    }
    mp.insert({h.at(i), w.at(i)});
  }

  vector<pair<ll, ll>> vpy;
  vector<pair<ll, ll>> vpx;

  for (auto vy : ycnt) {
    vpy.push_back({vy.second, vy.first});
  }
  for (auto vx : xcnt) {
    vpx.push_back({vx.second, vx.first});
  }

  sort(all(vpy));
  sort(all(vpx));
  reverse(all(vpy));
  reverse(all(vpx));

  ll my = vpy.at(0).first;
  ll mx = vpx.at(0).first;

  vector<pair<ll, ll>> vpyl;
  vector<pair<ll, ll>> vpxl;
  for (auto v : vpy) {
    if (v.first != my)
      break;
    vpyl.push_back(v);
  }
  for (auto v : vpx) {
    if (v.first != mx)
      break;
    vpxl.push_back(v);
  }

  ll ans = 0;

  for (auto vy : vpyl) {
    for (auto vx : vpxl) {
      ll y = vy.second;
      ll yc = vy.first;
      ll x = vx.second;
      ll xc = vx.first;
      if (mp.count({y, x})) {
        ans = max(ans, yc + xc - 1);
      } else {
        ans = max(ans, yc + xc);
      }
    }
  }

  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll H;
  scanf("%lld", &H);
  ll W;
  scanf("%lld", &W);
  ll M;
  scanf("%lld", &M);
  std::vector<ll> h(M);
  std::vector<ll> w(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &h[i]);
    scanf("%lld", &w[i]);
    // 0-indexed
    h[i]--;
    w[i]--;
  }
  solve(H, W, M, std::move(h), std::move(w));
  return 0;
}