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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define mp make_pair
#define pb push_back
#define lp(i, s, f) for (ll i = s; i < ll(f); i++)
#define inF freopen("input.in", "r", stdin);
#define outF freopen("output.in", "w", stdout);
#define endl '\\n'
#define MOD 1000000007
#define mm(arr) memset(arr, 0, sizeof(arr))
#define F first
#define S second

int main() {
  FAST int n, m, k;
  cin >> n >> m >> k;
  map<pair<int, int>, bool> has;
  pair<int, int> rows[n];
  pair<int, int> cols[m];
  for (int i = 0; i < n; i++) {
    rows[i] = {0, i};
  }
  for (int j = 0; j < m; j++) {
    cols[j] = {0, j};
  }
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    has[{a, b}] = 1;
    rows[a].first++;
    cols[b].first++;
  }
  sort(rows, rows + n);
  sort(cols, cols + m);
  vector<int> vec1;
  int a = rows[n - 1].first;
  for (int i = n - 1; i >= 0; i--) {
    if (rows[i].first == a) {
      vec1.pb(rows[i].second);
    } else
      break;
  }
  vector<int> vec2;
  a = cols[m - 1].first;
  for (int i = m - 1; i >= 0; i--) {
    if (cols[i].first == a) {
      vec2.pb(cols[i].second);
    } else
      break;
  }

  int mx = rows[n - 1].first + cols[m - 1].first - 1;
  bool yes = 0;
  // cout << mx << endl;
  for (int i = 0; i < vec1.size(); i++) {
    for (int j = 0; j < vec2.size(); j++) {
      if (!has[{vec1[i], vec2[j]}]) {
        mx++;
        i = vec1.size();
        break;
      }
    }
  }
  cout << mx;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define mp make_pair
#define pb push_back
#define lp(i, s, f) for (ll i = s; i < ll(f); i++)
#define inF freopen("input.in", "r", stdin);
#define outF freopen("output.in", "w", stdout);
#define endl '\\n'
#define MOD 1000000007
#define mm(arr) memset(arr, 0, sizeof(arr))
#define F first
#define S second

int main() {
  FAST int n, m, k;
  cin >> n >> m >> k;
  map<pair<int, int>, bool> has;
  pair<int, int> rows[n];
  pair<int, int> cols[m];
  for (int i = 0; i < n; i++) {
    rows[i] = {0, i};
  }
  for (int j = 0; j < m; j++) {
    cols[j] = {0, j};
  }
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    has[{a, b}] = 1;
    rows[a].first++;
    cols[b].first++;
  }
  sort(rows, rows + n);
  sort(cols, cols + m);
  int mx = 0;
  for (int i = n - 1; i >= 0; i--) {
    int curr = rows[i].first + cols[m - 1].first -
               has[{rows[i].second, cols[m - 1].second}];
    if (curr < mx)
      break;
    for (int j = m - 1; j >= 0; j--) {
      curr =
          rows[i].first + cols[j].first - has[{rows[i].second, cols[j].second}];
      if (curr < mx)
        break;
      mx = max(mx, curr);
    }
  }
  cout << mx;
  return 0;
}