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

#include <bits/stdc++.h>
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<ll, ll>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const ll MOD = 1000000007LL;
ll bpow(ll b, ll p, ll m = 1000000007LL) {
  ll rt = 1;
  for (; p; p >>= 1, b = b * b % m)
    if (p & 1)
      rt = rt * b % m;
  return rt;
}
pii cnth[300005], cntw[300005];
map<pii, int> o;
vector<int> rr[300005];
int main() {
  IOS;
  int h, w, m;
  cin >> h >> w >> m;
  for (int i = 0; i < 300005; i++)
    cnth[i].ss = cntw[i].ss = i;
  for (int i = 0; i < m; i++) {
    int r, c;
    cin >> r >> c;
    cnth[r].ff++, cntw[c].ff++;
    o[{r, c}] = 1;
    rr[r].pb(c);
  }
  sort(cnth, cnth + 300005, greater<pii>());
  sort(cntw, cntw + 300005, greater<pii>());
  set<int> mh, mw;
  int mxh = cnth[0].ff, mxw = cntw[0].ff;
  for (int i = 0; i < 300005; i++) {
    if (cnth[i].ff == mxh)
      mh.insert(cnth[i].ss);
    else
      break;
  }
  for (int i = 0; i < 300005; i++) {
    if (cntw[i].ff == mxw)
      mw.insert(cntw[i].ss);
    else
      break;
  }
  for (int i : mh) {
    int cnt = 0;
    for (int j : rr[i]) {
      if (mw.find(j) != mw.end()) {
        cnt++;
      }
    }
    if (cnt != mw.size()) {
      cout << mxh + mxw << endl;
      return 0;
    }
  }
  cout << mxh + mxw - 1 << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<ll, ll>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
ll bpow(ll b, ll p, ll m = 1000000007LL) {
  ll rt = 1;
  for (; p; p >>= 1, b = b * b % m)
    if (p & 1)
      rt = rt * b % m;
  return rt;
}
pair<int, int> cccnt[300005];
int rrcnt[300005];
int ccccnt[300005];
int rr[300005], cc[300005];
vector<int> ccc[300005];
map<pair<int, int>, int> used;
int main() {
  IOS;
  int r, c, m;
  cin >> r >> c >> m;
  for (int i = 0; i < m; i++) {
    cin >> rr[i] >> cc[i];
    rrcnt[rr[i]]++;
    cccnt[cc[i]].ff++;
    ccccnt[cc[i]]++;
    cccnt[cc[i]].ss = cc[i];
    used[{rr[i], cc[i]}] = 1;
    ccc[rr[i]].pb(cc[i]);
  }
  sort(cccnt, cccnt + 300005, greater<pair<int, int>>());
  int mx = 0;
  for (int i = 1; i <= r; i++) {
    int cur = rrcnt[i];
    for (int j : ccc[i]) {
      mx = max(cur + ccccnt[j] - 1, mx);
    }
    for (int j = 0, cnt = 0; j <= 300000 && cnt < 20; j++) {
      if (cnt >= 2 && cccnt[j].ff == 0)
        break;
      if (!used[{i, cccnt[j].ss}])
        cnt++;
      mx = max(cur + cccnt[j].ff - used[{i, cccnt[j].ss}], mx);
    }
  }
  cout << mx << endl;
}