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

// ヘッダー
#include <bits/stdc++.h>
using namespace std;

// 型定義
typedef long long ll;

// 定数
const ll INF = 1e+18;
const int MOD = 1e+9 + 7;

// REPマクロ
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define REP2(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define REPD2(i, a, b) for (ll i = a; i > (ll)(b); i--)

// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

// vectorの扱い
#define ALL(x) (x).begin(), (x).end() // sortなどの引数省略
#define SIZE(x) ((ll)(x).size())      // size
#define MAX(x) *max_element(ALL(x))   // 最大値
#define MIN(x) *min_element(ALL(x))   // 最小値

// 省略
using vi = vector<int>;
using vii = vector<vector<int>>;
using vl = vector<ll>;
using vll = vector<vector<ll>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int main() {
  ll H, W, M;
  cin >> H >> W >> M;
  vl h(M), w(M);
  REP(i, M) cin >> h[i] >> w[i];

  map<ll, ll> x, y;
  map<pair<ll, ll>, ll> mp;
  map<ll, vector<ll>> mpx, mpy;
  REP(i, M) {
    x[h[i]]++;
    y[w[i]]++;
    mp[make_pair(h[i], w[i])]++;
    mpx[h[i]].push_back(w[i]);
    mpy[w[i]].push_back(h[i]);
  }

  vector<ll> xlist;
  set<ll> yset;
  ll xmax = 0;
  ll ymax = 0;
  for (auto itr = x.begin(); itr != x.end(); itr++) {
    xmax = max(xmax, itr->second);
  }

  for (auto itr = y.begin(); itr != y.end(); itr++) {
    ymax = max(ymax, itr->second);
  }

  for (auto itr = y.begin(); itr != y.end(); itr++) {
    if (itr->second == ymax)
      yset.insert(itr->first);
  }

  ll ans = xmax + ymax - 1;

  for (auto itr = mpx.begin(); itr != mpx.end(); itr++) {
    vl vec = itr->second;
    ll cnt = 0;
    if (vec.size() != xmax)
      continue;
    REP(i, vec.size()) {
      ll ykouho = vec[i];
      if (yset.count(ykouho))
        cnt++;
    }
    if (cnt == yset.size())
      continue;
    else {
      ans = xmax + ymax;
    }
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

// ヘッダー
#include <bits/stdc++.h>
using namespace std;

// 型定義
typedef long long ll;

// 定数
const ll INF = 1e+18;
const int MOD = 1e+9 + 7;

// REPマクロ
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define REP2(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define REPD2(i, a, b) for (ll i = a; i > (ll)(b); i--)

// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

// vectorの扱い
#define ALL(x) (x).begin(), (x).end() // sortなどの引数省略
#define SIZE(x) ((ll)(x).size())      // size
#define MAX(x) *max_element(ALL(x))   // 最大値
#define MIN(x) *min_element(ALL(x))   // 最小値

// 省略
using vi = vector<int>;
using vii = vector<vector<int>>;
using vl = vector<ll>;
using vll = vector<vector<ll>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int main() {
  ll H, W, M;
  cin >> H >> W >> M;
  vl h(M), w(M);
  REP(i, M) cin >> h[i] >> w[i];

  map<ll, ll> x, y;
  map<pair<ll, ll>, ll> mp;
  REP(i, M) {
    x[h[i]]++;
    y[w[i]]++;
    mp[make_pair(h[i], w[i])]++;
  }

  ll ans = 0;

  for (auto itr = x.begin(); itr != x.end(); itr++) {
    for (auto itr2 = y.begin(); itr2 != y.end(); itr2++) {
      ll X = itr->second;
      ll Y = itr2->second;
      ll tmp = X + Y;
      if (mp.count(make_pair(itr->first, itr2->first))) {
        tmp -= mp[make_pair(itr->first, itr2->first)];
      }
      ans = max(ans, tmp);
    }
  }
  cout << ans << endl;
}