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

/*"for if God is with them who can be against them."
   it's beter to die struggling than to abandon the effort where there is still
   a chance of accomlishing something more..NEVER GIVE UP.
   */

/* "IF THERE IS NO WAY OUT LOOK AT THE BOTTOM". */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define endl '\\n'
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define all(a) a.begin(), a.end()
#define watch(x) cout << (#x) << " is " << (x) << endl
void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  map<int, int> x;
  map<int, int> y;
  vector<pii> p(m);
  map<pii, int> mark;
  for (int i = 0; i < m; i++) {
    cin >> p[i].F >> p[i].S;
    mark[p[i]]++;
    x[p[i].F]++;
    y[p[i].S]++;
  }
  int ans1 = 0, ans2 = 0;
  for (auto v : x) {
    ans1 = max(ans1, v.S);
  }
  for (auto v : y) {
    ans2 = max(ans2, v.S);
  }
  vector<int> r;
  vector<int> c;
  for (auto v : x) {
    if (v.S == ans1) {
      r.PB(v.F);
    }
  }
  for (auto v : y) {
    if (v.S == ans2) {
      c.PB(v.F);
    }
  }
  ll k = 1LL * (r.size()) * (c.size());
  if (k > 300000LL) {
    cout << ans1 + ans2 << endl;
    return;
  }
  for (auto p : r) {
    for (auto q : c) {
      if (mark[{p, q}] == 0) {
        cout << ans1 + ans2 << endl;
        return;
      }
    }
  }
  cout << ans1 + ans2 - 1 << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input1.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  IOS;
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
#ifndef ONLINE_JUDGE
  cout << "\\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\\n";
#endif
  return 0;
  /*TRY AGAIN, NO MATTER HOW MANY TIMES YOU FAILED ALWAYS TRY ONCE MORE*/
}

Error: TLE

Fixed submission:

/*"for if God is with them who can be against them."
   it's beter to die struggling than to abandon the effort where there is still
   a chance of accomlishing something more..NEVER GIVE UP.
   */

/* "IF THERE IS NO WAY OUT LOOK AT THE BOTTOM". */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define endl '\\n'
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define all(a) a.begin(), a.end()
#define watch(x) cout << (#x) << " is " << (x) << endl
void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  map<int, int> x;
  map<int, int> y;
  vector<pii> p(m);
  map<pii, int> mark;
  for (int i = 0; i < m; i++) {
    cin >> p[i].F >> p[i].S;
    mark[p[i]]++;
    x[p[i].F]++;
    y[p[i].S]++;
  }
  int ans = 0;
  for (auto v : x) {
    for (auto z : y) {
      if (mark[{v.F, z.F}] == 1) {
        ans = max(ans, v.S + z.S - 1);
      } else
        ans = max(ans, v.S + z.S);
    }
  }
  cout << ans << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input1.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  IOS;
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
#ifndef ONLINE_JUDGE
  cout << "\\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\\n";
#endif
  return 0;
  /*TRY AGAIN, NO MATTER HOW MANY TIMES YOU FAILED ALWAYS TRY ONCE MORE*/
}