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

// clear adj and visited vector declared globally after each test case
// check for long long overflow
// while adding and subs check if mod becomes -ve
// while using an integer directly in a builtin function add ll
// Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
// Dont keep array name as size or any other key word
//  also tink all answers possible for a question
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef long double ld;
#define PI 3.14159265358979323846264338327950L
#define MOD 1000000007
#define sz 100005
ll inf = 1e9;
ll power(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  while (n) {
    if (n & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res % mod;
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n, m, t;
  cin >> n >> m >> t;
  ll row[n + 1];
  ll col[m + 1];
  for (ll i = 0; i <= n; i++) {
    row[i] = 0;
  }
  for (ll j = 0; j <= m; j++) {
    col[j] = 0;
  }
  map<pair<ll, ll>, ll> m1;
  for (ll i = 0; i < t; i++) {
    ll x, y;
    cin >> x >> y;
    m1[{x, y}]++;
    row[x]++;
    col[y]++;
  }
  ll maxr, maxc, ansr = -1 * (1e9), ansc = -1 * (1e9);
  ll ans = -1 * (1e9);
  vector<ll> r;
  vector<ll> c;
  bool f = 0;
  for (ll i = 1; i <= n; i++)
    ansr = max(ansr, row[i]);
  for (ll i = 1; i <= m; i++)
    ansc = max(ansc, col[i]);
  for (ll i = 1; i <= n; i++) {
    if (row[i] == ansr)
      r.push_back(i);
  }
  for (ll i = 1; i <= m; i++) {
    if (col[i] == ansc)
      c.push_back(i);
  }
  for (ll i = 0; i < r.size(); i++) {
    for (ll j = 0; j < c.size(); j++) {
      if (m1.find({r[i], c[j]}) == m1.end()) {
        f = 1;
        break;
      }
    }
  }
  if (f)
    cout << ansr + ansc;
  else
    cout << ansr + ansc - 1;

  return 0;
}

Error: TLE

Fixed submission:

// clear adj and visited vector declared globally after each test case
// check for long long overflow
// while adding and subs check if mod becomes -ve
// while using an integer directly in a builtin function add ll
// Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
// Dont keep array name as size or any other key word
//  also tink all answers possible for a question
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef long double ld;
#define PI 3.14159265358979323846264338327950L
#define MOD 1000000007
#define sz 100005
ll inf = 1e9;
ll power(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  while (n) {
    if (n & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res % mod;
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n, m, t;
  cin >> n >> m >> t;
  ll row[n + 1];
  ll col[m + 1];
  for (ll i = 0; i <= n; i++) {
    row[i] = 0;
  }
  for (ll j = 0; j <= m; j++) {
    col[j] = 0;
  }
  map<pair<ll, ll>, ll> m1;
  for (ll i = 0; i < t; i++) {
    ll x, y;
    cin >> x >> y;
    m1[{x, y}]++;
    row[x]++;
    col[y]++;
  }
  ll maxr, maxc, ansr = -1 * (1e9), ansc = -1 * (1e9);
  ll ans = -1 * (1e9);
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= m; j++) {
      // cout<<r[i]<<" "<<c[j]<<'\\n';
      ll z1 = 0;
      if (m1.find({i, j}) != m1.end())
        z1 = 1;
      ans = max(ans, row[i] + col[j] - z1);
    }
  }
  cout << ans << '\\n';
  // for(ll i=1;i<=m;i++)
  //   cout<<c[i]<<" ";
  // cout<<'\\n';
  // cout<<maxr<<" "<<maxc<<'\\n';
  // cout<<ansc+ansr;
  return 0;
}