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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < int(N); ++i)
#define rep1(i, N) for (int i = 1; i < int(N); ++i)
#define all(a) (a).begin(), (a).end()
#define print(v)                                                               \\
  {                                                                            \\
    cerr << #v << ": [ ";                                                      \\
    for (auto _ : v)                                                           \\
      cerr << _ << ", ";                                                       \\
    cerr << "]" << endl;                                                       \\
  }
#define printpair(v)                                                           \\
  {                                                                            \\
    cerr << #v << ": [ ";                                                      \\
    for (auto _ : v)                                                           \\
      cerr << "{" << _.first << "," << _.second << "}"                         \\
           << ", ";                                                            \\
    cerr << "]" << endl;                                                       \\
  }
#define dump(x) cerr << #x << ": " << x << endl;
#define bit(k) (1LL << (k))
typedef long long ll;
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "{" << p.first << ", " << p.second << "}";
  return os;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int INF = (ll)1e9;
const ll INFLL = (ll)1e18 + 1;
const ll MOD = 1'000'000'007;
// const ll MOD = 998244353;
const double PI = acos(-1.0);
/*
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const string dir = "DRUL";
*/

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll H, W, M;
  cin >> H >> W >> M;
  vvec<int> vx(H);
  map<l_l, int> mp;
  vvec<int> vy(W);
  vector<l_l> cnty(1, {-1, -1});
  rep(i, M) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    vx[h].emplace_back(w);
    vy[w].emplace_back(h);
    mp[{h, w}]++;
  }
  // rep(i,H)sort(all(vx[i]));
  // rep(j,W)sort(all(vy[j]));
  // rep(i,H)print(vx[i]);
  // rep(j,W)print(vy[j]);

  rep(j, W) { cnty.emplace_back(vy[j].size(), j); }
  sort(all(cnty));
  // print(cnty);
  ll maxxy = cnty.back().first;
  vector<int> kouho_y;
  for (int i = W; i >= 0; i--) {
    kouho_y.emplace_back(cnty[i].second);
    if (cnty[i].first != cnty[i - 1].first)
      break;
  }
  // sort(all(kouho_y));
  // print(kouho_y);
  // dump(maxxy);
  ll ans = 0;
  rep(i, H) {
    ll tmp = vx[i].size();
    if (ans >= tmp + maxxy)
      continue;
    for (auto y : kouho_y) {
      if (mp[{i, y}] > 0) {
        chmax(ans, tmp + maxxy - 1);
      } else {
        chmax(ans, tmp + maxxy);
      }
      if (ans >= tmp + maxxy)
        break;
    }
  }
  cout << ans << endl;
}
/*

行を固定する
列は、最大の列を試す、一番大きいのだけで良い
*/

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < int(N); ++i)
#define rep1(i, N) for (int i = 1; i < int(N); ++i)
#define all(a) (a).begin(), (a).end()
#define print(v)                                                               \\
  {                                                                            \\
    cerr << #v << ": [ ";                                                      \\
    for (auto _ : v)                                                           \\
      cerr << _ << ", ";                                                       \\
    cerr << "]" << endl;                                                       \\
  }
#define printpair(v)                                                           \\
  {                                                                            \\
    cerr << #v << ": [ ";                                                      \\
    for (auto _ : v)                                                           \\
      cerr << "{" << _.first << "," << _.second << "}"                         \\
           << ", ";                                                            \\
    cerr << "]" << endl;                                                       \\
  }
#define dump(x) cerr << #x << ": " << x << endl;
#define bit(k) (1LL << (k))
typedef long long ll;
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "{" << p.first << ", " << p.second << "}";
  return os;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int INF = (ll)1e9;
const ll INFLL = (ll)1e18 + 1;
const ll MOD = 1'000'000'007;
// const ll MOD = 998244353;
const double PI = acos(-1.0);
/*
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const string dir = "DRUL";
*/

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll H, W, M;
  cin >> H >> W >> M;
  vvec<int> vx(H);
  map<l_l, int> mp;
  vvec<int> vy(W);
  vector<l_l> cnty(1, {-1, -1});
  rep(i, M) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    vx[h].emplace_back(w);
    vy[w].emplace_back(h);
    mp[{h, w}]++;
  }
  rep(i, H) sort(all(vx[i]));
  rep(j, W) sort(all(vy[j]));
  // rep(i,H)print(vx[i]);
  // rep(j,W)print(vy[j]);

  rep(j, W) { cnty.emplace_back(vy[j].size(), j); }
  sort(all(cnty));
  // print(cnty);
  ll maxxy = cnty.back().first;
  vector<int> kouho_y;
  for (int i = W; i >= 0; i--) {
    kouho_y.emplace_back(cnty[i].second);
    if (cnty[i].first != cnty[i - 1].first)
      break;
  }
  sort(all(kouho_y));
  // print(kouho_y);
  // dump(maxxy);
  ll ans = 0;
  rep(i, H) {
    ll tmp = vx[i].size();
    for (auto y : kouho_y) {
      if (mp[{i, y}] > 0) {
        chmax(ans, tmp + maxxy - 1);
      } else {
        chmax(ans, tmp + maxxy);
      }
    }
  }
  cout << ans << endl;
}
/*

行を固定する
列は、最大の列を試す、一番大きいのだけで良い
*/