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

#include <bits/stdc++.h> //---------------------------------------------------------------

#define ll long long
#define mod 1000000007
#define MAX 100005
#define PI 3.141592653589793238 // shortcut keys

#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define fr(i, a, b) for (int i = a; i <= b; i++)

#define decimal(k) fixed << setprecision(k)
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0); //---------------------------------------------------------------
using namespace std;

int main() {
  fast;
  int h, w, m;
  cin >> h >> w >> m;
  unordered_map<int, int> m1;
  unordered_map<int, int> m2;
  map<pair<int, int>, int> m3;
  fr(i, 1, m) {
    int x, y;
    cin >> x >> y;
    m1[x]++;
    m2[y]++;
    m3[{x, y}]++;
  }
  int x = 0, y = 0;
  vector<pair<int, int>> v1, v2;
  for (auto it = m1.begin(); it != m1.end(); it++) {
    if (it->second > x) {
      x = it->second;
    }
  }
  for (auto it = m1.begin(); it != m1.end(); it++) {
    if (it->second == x) {
      v1.pb({x, it->first});
    }
  }

  // cout<<x<<' ';
  for (auto it = m2.begin(); it != m2.end(); it++) {
    if (it->second > y) {
      y = it->second;
    }
  }
  for (auto it = m2.begin(); it != m2.end(); it++) {
    if (it->second == y) {
      v2.pb({y, it->first});
    }
  }
  // cout<<y<<' ';
  int n1 = v1.size(), n2 = v2.size();
  int ans = x + y - 1;
  fr(i, 0, n1 - 1) {
    fr(j, 0, n2 - 1) {
      if (m3[{v1[i].second, v2[j].second}] == 0) {
        ans++;
        break;
      }
    }
    if (ans == x + y)
      break;
  }

  cout << ans << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h> //---------------------------------------------------------------

#define ll long long
#define mod 1000000007
#define MAX 100005
#define PI 3.141592653589793238 // shortcut keys

#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define fr(i, a, b) for (int i = a; i <= b; i++)

#define decimal(k) fixed << setprecision(k)
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0); //---------------------------------------------------------------
using namespace std;

int main() {
  fast;
  int h, w, m;
  cin >> h >> w >> m;
  unordered_map<int, int> m1;
  unordered_map<int, int> m2;
  map<pair<int, int>, int> m3;
  fr(i, 1, m) {
    int x, y;
    cin >> x >> y;
    m1[x]++;
    m2[y]++;
    m3[{x, y}]++;
  }
  int x = 0, y = 0;
  vector<pair<int, int>> v1, v2;
  for (auto it = m1.begin(); it != m1.end(); it++) {
    if (it->second > x) {
      x = it->second;
    }
  }
  for (auto it = m1.begin(); it != m1.end(); it++) {
    if (it->second == x) {
      v1.pb({x, it->first});
    }
  }

  // cout<<x<<' ';
  for (auto it = m2.begin(); it != m2.end(); it++) {
    if (it->second > y) {
      y = it->second;
    }
  }
  for (auto it = m2.begin(); it != m2.end(); it++) {
    if (it->second == y) {
      v2.pb({y, it->first});
    }
  }
  // cout<<y<<' ';
  int n1 = v1.size(), n2 = v2.size();
  int ans = 0;
  fr(i, 0, n1 - 1) {
    fr(j, 0, n2 - 1) {
      ans = max(ans,
                v1[i].first + v2[j].first - m3[{v1[i].second, v2[j].second}]);
    }
  }

  cout << ans << '\\n';

  return 0;
}