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

// Fri Aug 21 19:35:37 IST 2020
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define forn(ii, a, b) for (int ii = (int)a; ii < (int)b; ii++)
#define scan(any)                                                              \\
  for (auto &i : any)                                                          \\
    cin >> i;
#define debug(x) cerr << #x << " " << x << " -- \\n"
#define I__O ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define int64 long long
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define pppp pair<int, int>
#define flushOff fflush(stdout)
#define precise(x) cout << fixed << setprecision(12) << x << '\\n';
using namespace std;
// policy based
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

const int MOD1 = 1e9 + 7;
const int MOD2 = 1e9 + 9;
const double pi = 3.141592653589793238;
const int N = 1e5 + 7;
const int INF = 2e9;
const int64 reallyBig = 1e16 + 7;

struct compare {
  bool operator()(pair<int, int> A, pair<int, int> B) {
    if (A.first == B.first)
      return A.second > B.second;
    return A.first > B.first;
  }
};

int tests() {
  int H, W;
  cin >> H >> W;
  int M;
  cin >> M;
  vector<int> row(H + 1), col(W + 1);
  set<pair<int, int>> s;
  forn(k, 0, M) {
    int a, b;
    cin >> a >> b;
    row[a]++;
    col[b]++;
    s.insert({a, b});
  }
  int row_mex = *max_element(all(row));
  int col_mex = *max_element(all(col));
  int row_len = 0, col_len = 0;
  vector<int> v1, v2;
  forn(i, 1, H + 1) if (row_mex == row[i]) row_len++, v1.push_back(i);
  forn(i, 1, W + 1) if (col_mex == col[i]) col_len++, v2.push_back(i);
  int64 check = (int64)row_len * col_len;
  if (check > M)
    return cout << row_mex + col_mex << '\\n', 0;
  int ans = 0;
  for (int &t : v1)
    for (int &p : v2)
      ans = max(ans, row_mex + col_mex - (int)(s.find({t, p}) != s.end()));
  cout << ans << '\\n';
  return 0;
}
int main() {
  I__O;
  int cases = 1;
  // cin >> cases ;
  while (cases--)
    tests();
  return 0;
}


Error: TLE

Fixed submission:

// Fri Aug 21 19:35:37 IST 2020
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define forn(ii, a, b) for (int ii = (int)a; ii < (int)b; ii++)
#define scan(any)                                                              \\
  for (auto &i : any)                                                          \\
    cin >> i;
#define debug(x) cerr << #x << " " << x << " -- \\n"
#define I__O ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define int64 long long
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define pppp pair<int, int>
#define flushOff fflush(stdout)
#define precise(x) cout << fixed << setprecision(12) << x << '\\n';
using namespace std;
// policy based
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

const int MOD1 = 1e9 + 7;
const int MOD2 = 1e9 + 9;
const double pi = 3.141592653589793238;
const int N = 1e5 + 7;
const int INF = 2e9;
const int64 reallyBig = 1e16 + 7;

struct compare {
  bool operator()(pair<int, int> A, pair<int, int> B) {
    if (A.first == B.first)
      return A.second > B.second;
    return A.first > B.first;
  }
};

int tests() {
  int H, W;
  cin >> H >> W;
  int M;
  cin >> M;
  vector<int> row(H + 1), col(W + 1);
  set<pair<int, int>> s;
  forn(k, 0, M) {
    int a, b;
    cin >> a >> b;
    row[a]++;
    col[b]++;
    s.insert({a, b});
  }
  int row_mex = *max_element(all(row));
  int col_mex = *max_element(all(col));
  int row_len = 0, col_len = 0;
  vector<int> v1, v2;
  forn(i, 1, H + 1) if (row_mex == row[i]) row_len++, v1.push_back(i);
  forn(i, 1, W + 1) if (col_mex == col[i]) col_len++, v2.push_back(i);
  int ans = 0;
  for (int &t : v1)
    for (int &p : v2)
      ans = max(ans, row_mex + col_mex - (int)(s.find({t, p}) != s.end()));
  cout << ans << '\\n';
  return 0;
}
int main() {
  I__O;
  int cases = 1;
  // cin >> cases ;
  while (cases--)
    tests();
  return 0;
}