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

#include <bits/stdc++.h>
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define range(i, l, h) for (int i = l; i < h; i++)
#define endl '\\n'
#define I INT_MAX
#define L INT_MIN
#define pb push_back
#define ll long long
#define ull unsigned long long
#define vi vector<int>
using namespace std;
using std::ios;
constexpr int MOD = 1e9 + 7;
constexpr int mod = 998244353;

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  vector<pair<ll, ll>> coord(m);
  vector<ll> r(h + 1), c(w + 1);
  map<pair<int, int>, int> mp;
  range(i, 0, m) {
    int x, y;
    cin >> x >> y;
    coord[i] = {x, y};
    r[x]++, c[y]++;
    mp[make_pair(x, y)]++;
  }
  sort(coord.begin(), coord.end());
  ll mxr = 0, rid = 0, mxc = 0, cid = 0;
  vector<ll> row, col;
  ll ans = 0;
  for (int rr = 1; rr <= h; rr++) {
    if (r[rr] > mxr) {
      mxr = r[rr];
      rid = rr;
    }
  }

  for (int cc = 1; cc <= w; cc++) {
    if (c[cc] > mxc) {
      mxc = c[cc];
      cid = cc;
    }
  }

  for (int rr = 1; rr <= h; rr++) {
    if (r[rr] == mxr) {
      row.pb(rr);
    }
  }

  for (int cc = 1; cc <= w; cc++) {
    if (c[cc] == mxc) {
      col.pb(cc);
    }
  }

  bool ok = false;
  int id = 0;
  for (ll R : row) {
    for (ll C : col) {
      // cout<<R<<','<<C<<endl;
      ll temp = mxc + mxr;
      if (mp[{R, C}])
        temp--;

      if (id > 0 && temp > ans) {
        ans = temp;
        ok = 1;
        break;
      } else if (id > 0 && temp < ans) {
        ok = 1;
        break;
      }

      ans = max(ans, temp);
      id++;
    }
    if (ok)
      break;
  }
  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define range(i, l, h) for (int i = l; i < h; i++)
#define endl '\\n'
#define I INT_MAX
#define L INT_MIN
#define pb push_back
#define ll long long
#define ull unsigned long long
#define vi vector<int>
using namespace std;
using std::ios;
constexpr int MOD = 1e9 + 7;
constexpr int mod = 998244353;

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  vector<pair<ll, ll>> coord(m);
  vector<ll> r(h + 1), c(w + 1);
  map<pair<int, int>, int> mp;
  range(i, 0, m) {
    int x, y;
    cin >> x >> y;
    coord[i] = {x, y};
    r[x]++, c[y]++;
    mp[make_pair(x, y)]++;
  }
  sort(coord.begin(), coord.end());
  ll mxr = 0, rid = 0, mxc = 0, cid = 0;
  vector<ll> row, col;
  ll ans = 0;
  for (int rr = 1; rr <= h; rr++) {
    if (r[rr] > mxr) {
      mxr = r[rr];
      rid = rr;
    }
  }

  for (int cc = 1; cc <= w; cc++) {
    if (c[cc] > mxc) {
      mxc = c[cc];
      cid = cc;
    }
  }

  for (int rr = 1; rr <= h; rr++) {
    if (r[rr] == mxr) {
      row.pb(rr);
    }
  }

  for (int cc = 1; cc <= w; cc++) {
    if (c[cc] == mxc) {
      col.pb(cc);
    }
  }

  for (ll R : row) {
    for (ll C : col) {
      // cout<<R<<','<<C<<endl;
      ll temp = mxc + mxr;
      if (mp[{R, C}])
        temp--;
      ans = max(ans, temp);
    }
  }
  cout << ans;
}