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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #include <boost/multiprecision/cpp_ll.hpp>
// typedef boost::multiprecision::cpp_ll ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E18;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define endl "\\n"
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

int main() {
  fastio ll H, W, M;
  cin >> H >> W >> M;
  ll h[M], w[M];
  ll ch[H], cw[W];
  rep(i, 0, H - 1) ch[i] = 0;
  rep(i, 0, W - 1) cw[i] = 0;
  vector<ll> v1[H], v2[W];
  set<l_l> st;
  rep(i, 0, M - 1) {
    cin >> h[i] >> w[i];
    v1[h[i] - 1].pb(w[i] - 1);
    v2[w[i] - 1].pb(h[i] - 1);
    st.insert(make_pair(h[i] - 1, w[i] - 1));
    ch[h[i] - 1]++;
    cw[w[i] - 1]++;
  }
  ll mh = 0, mw = 0;
  vector<ll> vh, vw;
  rep(i, 0, H - 1) {
    if (mh > ch[i])
      continue;
    if (mh == ch[i]) {
      vh.pb(i);
    } else {
      vh.clear();
      vh.pb(i);
      mh = ch[i];
    }
  }
  rep(i, 0, W - 1) {
    if (mw > cw[i])
      continue;
    if (mw == cw[i]) {
      vw.pb(i);
    } else {
      vw.clear();
      vw.pb(i);
      mw = cw[i];
    }
  }
  bool bol = true;
  rep(i, 0, vh.size() - 1) {
    rep(j, 0, vw.size() - 1) {
      ll h = vh[i];
      ll w = vw[j];
      auto it = st.find(make_pair(h, w));
      if (it == st.end()) {
        bol = false;
        break;
      }
    }
    if (!bol)
      break;
  }
  if (!bol) {
    cout << mh + mw << endl;
  } else {
    cout << mh + mw - 1 << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #include <boost/multiprecision/cpp_ll.hpp>
// typedef boost::multiprecision::cpp_ll ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E18;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define endl "\\n"
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

int main() {
  fastio ll H, W, M;
  cin >> H >> W >> M;
  ll h[M], w[M];
  ll ch[H], cw[W];
  rep(i, 0, H - 1) ch[i] = 0;
  rep(i, 0, W - 1) cw[i] = 0;
  vector<ll> v1[H], v2[W];
  rep(i, 0, M - 1) {
    cin >> h[i] >> w[i];
    v1[h[i] - 1].pb(w[i] - 1);
    v2[w[i] - 1].pb(h[i] - 1);
    ch[h[i] - 1]++;
    cw[w[i] - 1]++;
  }
  ll mh = 0, mw = 0;
  vector<ll> vh, vw;
  rep(i, 0, H - 1) {
    if (mh > ch[i])
      continue;
    if (mh == ch[i]) {
      vh.pb(i);
    } else {
      vh.clear();
      vh.pb(i);
      mh = ch[i];
    }
  }
  rep(i, 0, W - 1) {
    if (mw > cw[i])
      continue;
    if (mw == cw[i]) {
      vw.pb(i);
    } else {
      vw.clear();
      vw.pb(i);
      mw = cw[i];
    }
  }
  bool bol = true;
  rep(i, 0, vh.size() - 1) {
    rep(j, 0, vw.size() - 1) {
      ll h = vh[i];
      ll w = vw[j];
      bool hoge = true;
      rep(k, 0, (ll)v1[h].size() - 1) {
        if (w == v1[h][k]) {
          hoge = false;
          break;
        }
      }
      if (hoge) {
        bol = false;
        break;
      }
    }
    if (!bol)
      break;
  }
  if (!bol) {
    cout << mh + mw << endl;
  } else {
    cout << mh + mw - 1 << endl;
  }
}