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

// By : BebekJK
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
// Declare -> ordered_set NAMA;
// Nth smallest integer -> find_by_order(N-1)
// Number of elements less than N -> order_of_key(N)
// Delete N from the set -> erase(N)

#define endl '\\n'
#define MOD 1000000007
#define PI 3.141592653
#define fi first
#define sc second
#define pb push_back
#define pbb pop_back

// #pragma GCC optimize("Ofast")
// #pragma GCC target ("string"...)
// #pragma GCC push_options
// #pragma GCC pop_options
// #pragma GCC reset_options
// #pragma GCC optimize ("O3")

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

void BOOST() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
void fcin(int &x) {
  bool neg = false;
  register int c;
  x = 0;
  c = getchar();
  if (c == '-') {
    neg = true;
    c = getchar();
  }
  for (; (c > 47 && c < 58); c = getchar())
    x = (x << 1) + (x << 3) + c - 48;
  if (neg)
    x *= -1;
}
ll pmod(ll a, ll b) {
  if (b == 0)
    return 1;
  ll temp = pmod(a, b / 2);
  if (b % 2 == 0)
    return (temp * temp) % MOD;
  return (((temp * temp) % MOD) * a) % MOD;
}
int R, C, M, X, Y;
map<pii, bool> Grid;
int Row[300005];
int Col[300005];
vector<int> RM;
vector<int> CM;
int Rmax, Cmax;
int main() {
  //	BOOST();
  cin >> R >> C >> M;
  for (int i = 1; i <= M; i++) {
    cin >> X >> Y;
    Grid[{X, Y}] = true;
    Row[X]++;
    Col[Y]++;
  }

  for (int i = 1; i <= R; i++)
    Rmax = max(Rmax, Row[i]);
  for (int i = 1; i <= C; i++)
    Cmax = max(Cmax, Col[i]);

  for (int i = 1; i <= R; i++) {
    if (Row[i] == Rmax)
      RM.pb(i);
  }
  for (int i = 1; i <= C; i++) {
    if (Col[i] == Cmax)
      CM.pb(i);
  }

  int Ans = Rmax + Cmax;
  for (int i = 0; i < RM.size(); i++) {
    for (int j = 0; j < CM.size(); j++) {
      if (!Grid[{RM[i], CM[j]}]) {
        cout << Ans << endl;
        return 0;
      }
    }
  }
  cout << Ans - 1 << endl;
}


Error: TLE

Fixed submission:

// By : BebekJK
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
// Declare -> ordered_set NAMA;
// Nth smallest integer -> find_by_order(N-1)
// Number of elements less than N -> order_of_key(N)
// Delete N from the set -> erase(N)

#define endl '\\n'
#define MOD 1000000007
#define PI 3.141592653
#define fi first
#define sc second
#define pb push_back
#define pbb pop_back

// #pragma GCC optimize("Ofast")
// #pragma GCC target ("string"...)
// #pragma GCC push_options
// #pragma GCC pop_options
// #pragma GCC reset_options
// #pragma GCC optimize ("O3")

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

void BOOST() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
void fcin(int &x) {
  bool neg = false;
  register int c;
  x = 0;
  c = getchar();
  if (c == '-') {
    neg = true;
    c = getchar();
  }
  for (; (c > 47 && c < 58); c = getchar())
    x = (x << 1) + (x << 3) + c - 48;
  if (neg)
    x *= -1;
}
ll pmod(ll a, ll b) {
  if (b == 0)
    return 1;
  ll temp = pmod(a, b / 2);
  if (b % 2 == 0)
    return (temp * temp) % MOD;
  return (((temp * temp) % MOD) * a) % MOD;
}
int R, C, M, X, Y;
map<pii, bool> Grid;
int Row[300005];
int Col[300005];
vector<int> RM;
vector<int> CM;
int Rmax, Cmax;
int main() {
  //	BOOST();
  cin >> R >> C >> M;
  for (int i = 1; i <= M; i++) {
    cin >> X >> Y;
    Grid[{X, Y}] = true;
  }
  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      if (Grid[{i, j}])
        Row[i]++, Col[j]++;
    }
  }
  for (int i = 1; i <= R; i++)
    Rmax = max(Rmax, Row[i]);
  for (int i = 1; i <= C; i++)
    Cmax = max(Cmax, Col[i]);

  for (int i = 1; i <= R; i++) {
    if (Row[i] == Rmax)
      RM.pb(i);
  }
  for (int i = 1; i <= C; i++) {
    if (Col[i] == Cmax)
      CM.pb(i);
  }

  int Ans = Rmax + Cmax;
  for (int i = 0; i < RM.size(); i++) {
    for (int j = 0; j < CM.size(); j++) {
      if (!Grid[{RM[i], CM[j]}]) {
        cout << Ans << endl;
        return 0;
      }
    }
  }
  cout << Ans - 1 << endl;
}