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

#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define lli long long int
#define tc                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define pb push_back
#define vi vector<int>
#define vll vector<lli>
#define vpi vector<pair<int, int>>
#define vpll vector<pair<lli, lli>>
#define vvpi vector<vector<pair<int, int>>>
#define vvpll vector<vector<pair<lli, lli>>>
#define vvi vector<vector<int>>
#define vvll vector<vector<lli>>
#define maxn 1000001
#define mod 1000000007
#define inf 10000000000000000

int main() {
  fastio;
  int n, m;
  cin >> n >> m;
  int n1;
  cin >> n1;
  map<int, int> rsum, csum;
  map<pair<int, int>, int> mp;
  int maxr = 0, maxc = 0;
  for (int i = 0; i < n1; i++) {
    int x, y;
    cin >> x >> y;
    mp[{x - 1, y - 1}]++;
    rsum[x - 1]++;
    csum[y - 1]++;
    maxr = max(maxr, rsum[x - 1]);
    maxc = max(maxc, csum[y - 1]);
  }
  vi vr, vc;
  for (auto x : rsum) {
    if (x.s == maxr) {
      vr.pb(x.f);
    }
  }
  for (auto x : csum) {
    if (x.s == maxc) {
      vc.pb(x.f);
    }
  }
  lli n2 = vr.size(), n3 = vc.size();
  int ans = 0;
  if (n2 * n3 > n1) {
    cout << maxr + maxc << "\\n";
  } else {
    for (int x : vr) {
      for (int y : vc) {
        int tot = 0;
        if (mp[{x, y}])
          tot--;
        tot += rsum[x] + csum[y];
        ans = max(ans, tot);
      }
    }
    cout << ans << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define lli long long int
#define tc                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define pb push_back
#define vi vector<int>
#define vll vector<lli>
#define vpi vector<pair<int, int>>
#define vpll vector<pair<lli, lli>>
#define vvpi vector<vector<pair<int, int>>>
#define vvpll vector<vector<pair<lli, lli>>>
#define vvi vector<vector<int>>
#define vvll vector<vector<lli>>
#define maxn 1000001
#define mod 1000000007
#define inf 10000000000000000

int main() {
  fastio;
  int n, m;
  cin >> n >> m;
  int n1;
  cin >> n1;
  map<int, int> rsum, csum;
  map<pair<int, int>, int> mp;
  for (int i = 0; i < n1; i++) {
    int x, y;
    cin >> x >> y;
    mp[{x - 1, y - 1}]++;
    rsum[x - 1]++;
    csum[y - 1]++;
  }
  int ans = 0;
  for (auto x : rsum) {
    for (auto y : csum) {
      int tot = x.s + y.s;
      if (mp[{x.f, y.f}])
        tot--;
      ans = max(ans, tot);
    }
  }
  cout << ans << "\\n";
  return 0;
}