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

/*****************************************************
 *     created by: nayakashutosh9
 * "Winners Never Quit and Quitters Never Win".
 *****************************************************/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast")
using namespace std;
using namespace __gnu_pbds;
#define int long long int
#define endl '\\n'
#define mod 1000000007
#define inf 1e18
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
const int N = 300005;
/**********************************************************************************************/

void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  unordered_map<int, int> row, col;
  vector<pair<int, int>> a(m);
  set<pair<int, int>> vis;
  for (int i = 0; i < m; i++) {
    cin >> a[i].first >> a[i].second;
    vis.insert(a[i]);
    row[a[i].first]++;
    col[a[i].second]++;
  }
  int mr = row[1], r = 1, mc = col[1], c = 1, ans = 0;
  unordered_set<int> rvis, cvis;
  for (auto x : row) {
    if (mr <= x.second) {
      if (mr == x.second) {
        rvis.insert(x.first);
        continue;
      }
      rvis.clear();
      mr = x.second;
      rvis.insert(x.first);
    }
  }
  for (auto x : col) {
    if (mc <= x.second) {
      if (mc == x.second) {
        cvis.insert(x.first);
        continue;
      }
      mc = x.second;
      cvis.clear();
      cvis.insert(x.first);
    }
  }
  for (int i : rvis) {
    for (int j : cvis) {
      if (vis.find({i, j}) == vis.end()) {
        ans = max(ans, row[i] + col[j]);
        break;
      } else
        ans = max(ans, row[i] + col[j] - 1);
      // cout << i << " " << j << " " << ans << endl;
    }
  }
  cout << ans << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1, z = 1;
  // cin >> t; cin.ignore();
  while (t--) {
    // cout << "Case #" << (z++) << ": ";
    // clock_t start = clock();
    solve();
    // clock_t end = clock();
    // cout << (end-start) << endl;
    // print execution time in ms
  }

  return 0;
}

Error: TLE

Fixed submission:

/*****************************************************
 *     created by: nayakashutosh9
 * "Winners Never Quit and Quitters Never Win".
 *****************************************************/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast")
using namespace std;
using namespace __gnu_pbds;
#define int long long int
#define endl '\\n'
#define mod 1000000007
#define inf 1e18
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
const int N = 300005;
/**********************************************************************************************/

void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  unordered_map<int, int> row, col;
  vector<pair<int, int>> a(m);
  set<pair<int, int>> vis;
  for (int i = 0; i < m; i++) {
    cin >> a[i].first >> a[i].second;
    vis.insert(a[i]);
    row[a[i].first]++;
    col[a[i].second]++;
  }
  int mr = row[1], r = 1, mc = col[1], c = 1, ans = 0;
  unordered_set<int> rvis, cvis;
  for (auto x : row) {
    if (mr <= x.second) {
      if (mr == x.second) {
        rvis.insert(x.first);
        continue;
      }
      rvis.clear();
      mr = x.second;
      rvis.insert(x.first);
    }
  }
  for (auto x : col) {
    if (mc <= x.second) {
      if (mc == x.second) {
        cvis.insert(x.first);
        continue;
      }
      mc = x.second;
      cvis.clear();
      cvis.insert(x.first);
    }
  }
  for (int i : rvis) {
    for (int j : cvis) {
      if (vis.find({i, j}) == vis.end())
        ans = max(ans, row[i] + col[j]);
      else
        ans = max(ans, row[i] + col[j] - 1);
      // cout << i << " " << j << " " << ans << endl;
    }
  }
  cout << ans << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1, z = 1;
  // cin >> t; cin.ignore();
  while (t--) {
    // cout << "Case #" << (z++) << ": ";
    // clock_t start = clock();
    solve();
    // clock_t end = clock();
    // cout << (end-start) << endl;
    // print execution time in ms
  }

  return 0;
}