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

// بسم الله الرحمن الرحيم
///   ***   ---   |||		 In the name of ALLAH		|||   ---   ***
///   ///
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define el << endl
#define PI 3.141592653589793238
#define _test_                                                                 \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define optimize()                                                             \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define file()                                                                 \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout)
ll gcd(ll a, ll b) { return __gcd(a, b); }
ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
int dx[] = {0, 0, +1, -1, +1, -1, -1, +1};
int dy[] = {+1, -1, 0, 0, +1, -1, +1, -1};
///*......................*//* LET'S BEGIN (-_^)
///*//*............................///
const int mx = 3e5 + 123;
int h, w, m, u, v, row[mx], col[mx], mxcol, mxrow;
map<pair<int, int>, int> mp;
vector<int> rowv, colv;

void _case_() {
  cin >> h >> w >> m;
  while (m--) {
    cin >> u >> v;
    row[u]++;
    col[v]++;
    mp[{u, v}]++;
  }
  for (int i = 1; i <= h; i++)
    mxrow = max(row[i], mxrow);
  for (int i = 1; i <= h; i++)
    if (row[i] == mxrow)
      rowv.push_back(i);
  for (int i = 1; i <= w; i++)
    mxcol = max(col[i], mxcol);
  for (int i = 1; i <= w; i++)
    if (col[i] == mxcol)
      colv.push_back(i);
  for (auto i : rowv)
    for (auto j : colv) {
      if (!mp[{i, j}]) {
        cout << mxrow + mxcol el;
        return;
      }
    }
  cout << mxrow + mxcol - 1 el;
}

int main() {

  optimize();
  //_test_
  _case_();
  return 0;
}

///////////*  N A Y E M  *///////////

Error: TLE

Fixed submission:

// بسم الله الرحمن الرحيم
///   ***   ---   |||		 In the name of ALLAH		|||   ---   ***
///   ///
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define el << endl
#define PI 3.141592653589793238
#define _test_                                                                 \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define optimize()                                                             \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define file()                                                                 \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout)
ll gcd(ll a, ll b) { return __gcd(a, b); }
ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
int dx[] = {0, 0, +1, -1, +1, -1, -1, +1};
int dy[] = {+1, -1, 0, 0, +1, -1, +1, -1};
///*......................*//* LET'S BEGIN (-_^)
///*//*............................///
const int mx = 3e5 + 123;
int h, w, m, u, v, row[mx], col[mx], ans;
map<int, int> mp[mx];
void _case_() {
  cin >> h >> w >> m;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    row[u]++;
    col[v]++;
    mp[u][v] = 1;
  }
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      int k = row[i] + col[j];
      if (mp[i][j]) {
        ans = max(ans, k - 1);
      } else
        ans = max(ans, k);
    }
  cout << ans el;
}

int main() {

  optimize();
  //_test_
  _case_();
  return 0;
}

///////////*  N A Y E M  *///////////