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

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x)                                                         \\
  sort(all((x)));                                                              \\
  (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int h = 0, w = 0, m = 0;

  cin >> h >> w >> m;

  vector<pair<int, int>> xt(h), yt(w);

  set<pair<int, int>> have;

  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    xt[--x].fi++;
    yt[--y].fi++;
    have.insert(mp(x, y));
  }

  int ans = 0;

  for (int i = 0; i < h; i++)
    xt[i].se = i;

  for (int i = 0; i < w; i++)
    yt[i].se = i;

  sort(rall(xt));
  sort(rall(yt));

  for (int i = 0; i < min(h, 10); i++)
    for (int j = 0; j < w; j++) {
      int cur = xt[i].fi + yt[j].fi - (have.count(mp(xt[i].se, yt[j].se)));
      ans = max(ans, cur);
    }

  for (int j = 0; j < min(w, 10); j++)
    for (int i = 0; i < h; i++) {
      int cur = xt[i].fi + yt[j].fi - (have.count(mp(xt[i].se, yt[j].se)));
      ans = max(ans, cur);
    }

  cout << ans;

  return 0;
}
/*

*/


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x)                                                         \\
  sort(all((x)));                                                              \\
  (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll h = 0, w = 0, m = 0;

  cin >> h >> w >> m;

  vector<pair<ll, ll>> xt(h), yt(w);

  set<pair<ll, ll>> have;

  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    xt[--x].fi++;
    yt[--y].fi++;
    have.insert(mp(x, y));
  }

  ll ans = 0;

  for (ll i = 0; i < h; i++)
    xt[i].se = i;

  for (ll i = 0; i < w; i++)
    yt[i].se = i;

  sort(rall(xt));
  sort(rall(yt));

  for (ll i = 0; i < min(h, 50ll); i++)
    for (ll j = 0; j < w; j++) {
      ll cur = xt[i].fi + yt[j].fi - (have.count(mp(xt[i].se, yt[j].se)));
      ans = max(ans, cur);
    }

  for (int j = 0; j < min(w, 50ll); j++)
    for (int i = 0; i < h; i++) {
      ll cur = xt[i].fi + yt[j].fi - (have.count(mp(xt[i].se, yt[j].se)));
      ans = max(ans, cur);
    }

  cout << ans;

  return 0;
}
/*

*/