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

#include <bits/stdc++.h>
#define vc vector<int>
#define um unordered_map<int, int>
#define ll long long
#define rep(i, a, b) for (__typeof(a) i = a; i < b; i++)
#define si(a) a.size()
#define all(a) a.begin(), a.end()
#define ss(a) a.second
#define ff(a) a.first
#define M 1000000
#define pb push_back
#define repit(i, a) for (auto i = a.begin(); i != a.end(); i++)
#define take(arr, n)                                                           \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> arr[i]
#define out(arr, n)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  cout << arr[i] << ' '
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define reprev(i, b, a) for (__typeof(b) i = b; i >= a; i--)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define doublearray vector<vector<int>>
#define mod 998244353
using namespace std;
ll max(ll a, ll b) { return a > b ? a : b; }
ll min(ll a, ll b) { return a < b ? a : b; }
// ll gcd(ll a, ll b){
//     if(b==0) return a;
//     return gcd(b,a%b);
// }
// ll lcm(ll a, ll b){
//     return (a*b)/gcd(a,b);
// }
int binaryS(ll x, ll sb[], int m) {
  int l = 0, r = m;
  int mid = (l + r) / 2;
  while (l < r) {
    mid = (l + r) / 2;
    if (sb[mid] < x)
      l = mid + 1;
    else if (sb[mid] > x)
      r = mid - 1;
    else
      return mid;
  }
  return l;
}
int main() {
  fastio;
  int n, m, k;
  cin >> n >> m >> k;
  um r, c;
  vector<pair<int, int>> tar;
  ll maxr = 0, maxc = 0;
  rep(i, 0, k) {
    int x, y;
    cin >> x >> y;
    r[x]++;
    c[y]++;
    maxr = max(maxr, r[x]);
    maxc = max(maxc, c[y]);
    tar.emplace_back(x, y);
  }
  ll co = 0;
  rep(i, 0, k) {
    if ((r[ff(tar[i])] == maxr) && (c[ss(tar[i])] == maxc)) {
      co++;
    }
  }
  ll crmax = 0;
  repit(it, r) {
    if (it->second == maxr)
      crmax++;
  }
  ll ccmax = 0;
  repit(it, c) {
    if (it->second == maxc)
      ccmax++;
  }
  if (co < (crmax * ccmax)) {
    cout << (maxr + maxc) << endl;
  } else
    cout << (maxc + maxr - 1) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define vc vector<int>
#define um unordered_map<int, int>
#define ll long long
#define rep(i, a, b) for (__typeof(a) i = a; i < b; i++)
#define si(a) a.size()
#define all(a) a.begin(), a.end()
#define ss(a) a.second
#define ff(a) a.first
#define M 1000000
#define pb push_back
#define repit(i, a) for (auto i = a.begin(); i != a.end(); i++)
#define take(arr, n)                                                           \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> arr[i]
#define out(arr, n)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  cout << arr[i] << ' '
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define reprev(i, b, a) for (__typeof(b) i = b; i >= a; i--)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define doublearray vector<vector<int>>
#define mod 998244353
using namespace std;
ll max(ll a, ll b) { return a > b ? a : b; }
ll min(ll a, ll b) { return a < b ? a : b; }
// ll gcd(ll a, ll b){
//     if(b==0) return a;
//     return gcd(b,a%b);
// }
// ll lcm(ll a, ll b){
//     return (a*b)/gcd(a,b);
// }
int binaryS(ll x, ll sb[], int m) {
  int l = 0, r = m;
  int mid = (l + r) / 2;
  while (l < r) {
    mid = (l + r) / 2;
    if (sb[mid] < x)
      l = mid + 1;
    else if (sb[mid] > x)
      r = mid - 1;
    else
      return mid;
  }
  return l;
}
int main() {
  fastio;
  int n, m, k;
  cin >> n >> m >> k;
  um r, c;
  unordered_map<int, um> mp;
  rep(i, 0, k) {
    int x, y;
    cin >> x >> y;
    r[x]++;
    c[y]++;
    mp[x][y]++;
  }
  ll ans = 0;
  repit(it1, r) {
    repit(it2, c) {

      if (mp[it1->first][it2->first] > 0) {
        ans = max(ans, it1->second + it2->second - 1);
      } else {
        ans = max(ans, it1->second + it2->second);
      }
    }
  }
  cout << ans << endl;
  return 0;
}