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

#include <bits/stdc++.h>
// Fast io
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
// Class shorthands
#define ll long long
#define db long double
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
// Function Shorthands
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mp make_pair
#define po pop_back
#define eb emplace_back
// Code Shorthands
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repe(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = b; i >= a; i--)
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
// Constants
#define hell 1000000007
#define PI acos(-1.0)
using namespace std;

void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  set<pii> s;
  map<int, int> m1, m2;
  rep(i, 0, m) {
    int t1, t2;
    cin >> t1 >> t2;
    s.insert({t1, t2});
    m1[t1]++;
    m2[t2]++;
  }
  vii v1, v2;
  for (auto i : m1) {
    v1.pb({i.se, i.fi});
  }
  for (auto i : m2)
    v2.pb({i.se, i.fi});
  sort(all(v1));
  reverse(all(v1));
  sort(all(v2));
  reverse(all(v2));
  int ans = -1;
  for (int i = 0; i < v1.size(); i++) {
    for (int j = 0; j < v2.size(); j++) {
      int cur = v1[i].fi + v2[j].fi;
      if (cur <= ans)
        break;
      if (s.find({v1[i].se, v2[j].se}) != s.end())
        cur--;
      ans = max(ans, cur);
    }
  }
  cout << ans << endl;
}

int main() {
  io;
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// Fast io
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
// Class shorthands
#define ll long long
#define db long double
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
// Function Shorthands
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mp make_pair
#define po pop_back
#define eb emplace_back
// Code Shorthands
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repe(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = b; i >= a; i--)
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
// Constants
#define hell 1000000007
#define PI acos(-1.0)
using namespace std;

void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  set<pii> s;
  map<int, int> m1, m2;
  rep(i, 0, m) {
    int t1, t2;
    cin >> t1 >> t2;
    s.insert({t1, t2});
    m1[t1]++;
    m2[t2]++;
  }
  vii v1, v2;
  for (auto i : m1) {
    v1.pb({i.se, i.fi});
  }
  for (auto i : m2)
    v2.pb({i.se, i.fi});
  sort(all(v1));
  reverse(all(v1));
  sort(all(v2));
  reverse(all(v2));
  int ans = -1;
  for (int i = 0; i < v1.size(); i++) {
    for (int j = 0; j < v2.size(); j++) {
      int cur = v1[i].fi + v2[j].fi;
      if (s.find({v1[i].se, v2[j].se}) != s.end())
        cur--;
      if (cur >= ans - 1)
        ans = max(cur, ans);
      else
        break;
    }
  }
  cout << ans << endl;
}

int main() {
  io;
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}