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

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#define int long long
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define eb emplace_back
#define fn for (int i = 0; i < n; i++)
#define fn1 for (int i = 1; i <= n; i++)
#define fm for (int j = 0; j < m; j++)
#define fm1 for (int j = 1; j <= m; j++)
#define fi first
#define se second
#define endl '\\n'
#define PI 3.14159265358979323846
#define MOD 1000000007
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
const int N = 2e6 + 5;
const int INF = 1e18L;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
int ar[N];
int dp[N];
signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS;
  int T = 1;
  // cin >> T;
  while (T--) {
    int n, m, k;
    cin >> n >> m >> k;
    map<int, int> r, c;
    map<pair<int, int>, bool> vis;
    for (int i = 0; i < k; i++) {
      int x, y;
      cin >> x >> y;
      r[x]++;
      c[y]++;
      vis[{x, y}] = 1;
    }
    int ans = 0;
    int rmx = 0, cmx = 0;
    for (auto i : r)
      rmx = max(rmx, i.se);
    for (auto i : c)
      cmx = max(cmx, i.se);
    vector<int> rr, cc;
    for (auto i : r)
      if (rmx == i.se)
        rr.pb(i.fi);
    for (auto i : c)
      if (cmx == i.se)
        cc.pb(i.fi);
    bool ok = 1;
    for (auto i : rr) {
      for (auto j : cc)
        if (vis[{i, j}] == 0) {
          ok = 0;
          break;
        }
      if (ok == 0)
        break;
    }
    ans = rmx + cmx - ok;
    cout << ans << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#define int long long
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define eb emplace_back
#define fn for (int i = 0; i < n; i++)
#define fn1 for (int i = 1; i <= n; i++)
#define fm for (int j = 0; j < m; j++)
#define fm1 for (int j = 1; j <= m; j++)
#define fi first
#define se second
#define endl '\\n'
#define PI 3.14159265358979323846
#define MOD 1000000007
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
const int N = 2e6 + 5;
const int INF = 1e18L;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
int ar[N];
int dp[N];
signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS;
  int T = 1;
  // cin >> T;
  while (T--) {
    int n, m, k;
    cin >> n >> m >> k;
    map<int, int> r, c;
    map<pair<int, int>, bool> vis;
    for (int i = 0; i < k; i++) {
      int x, y;
      cin >> x >> y;
      r[x]++;
      c[y]++;
      vis[{x, y}] = 1;
    }
    int ans = 0;
    int rmx = 0, cmx = 0;
    for (auto i : r)
      rmx = max(rmx, i.se);
    for (auto i : c)
      cmx = max(cmx, i.se);
    vector<int> rr, cc;
    for (auto i : r)
      if (rmx == i.se)
        rr.pb(i.fi);
    for (auto i : c)
      if (cmx == i.se)
        cc.pb(i.fi);
    bool ok = 1;
    for (auto i : rr) {
      for (auto j : cc)
        if (vis[{i, j}] == 0)
          ok = 0;
    }
    ans = rmx + cmx - ok;
    cout << ans << '\\n';
  }
  return 0;
}