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

#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define umap unordered_map
#define mod 1000000007
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define MN(a, b, c) min(a, min(b, c))
#define MX(a, b, c) max(a, max(b, c))
#define pr1 pair<ll, ll>
#define F first
#define S second
#define mP make_pair
#define f(i, n) for (int i = 0; i < n; i++)
#define f1(i, x, y) for (int i = x; i <= y; i++)
#define f2(i, x, y) for (int i = x; i >= y; i--)
#define yes                                                                    \\
  cout << "YES"                                                                \\
       << "\\n"
#define no                                                                     \\
  cout << "NO"                                                                 \\
       << "\\n"
#define modsum(a, b) ((a % mod) + (b % mod)) % mod
#define modpro(a, b) ((a % mod) * (b % mod)) % mod
//__builtin_popcount(x)
//__builtin_parity(x)   =(number of set bits)%2
//__builtin_clz(x)      to count the number of leading  zeroes
//__builtin_ctz(x)      to count the number of trailing zeroes
//__gcd(a,b)
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int R, C, M;
  cin >> R >> C >> M;
  int mx = 0;
  int x, y;
  map<pair<int, int>, int> vis;
  umap<int, int> fr, fc;
  int fmxr = 0, fmxc = 0;
  int mxr = 0, mxc = 0;
  f1(i, 1, M) {
    cin >> x >> y;
    fr[x]++;
    fc[y]++;
    vis[{x, y}] = 1;
    mxr = max(mxr, fr[x]);
    mxc = max(mxc, fc[y]);
  }
  vector<int> MX, MY;
  for (auto it = fr.begin(); it != fr.end(); ++it) {
    if ((it->S) == mxr) {
      MX.pb(it->F);
    }
  }
  for (auto it = fc.begin(); it != fc.end(); ++it) {
    if ((it->S) == mxc) {
      MY.pb(it->F);
    }
  }
  y = 0;
  for (int i = 0; i < MX.size(); i++) {
    for (int j = 0; j < MY.size(); j++) {
      if (vis[{MX[i], MY[j]}] == 0) {
        y = 1;
        break;
      }
    }
  }
  if (y)
    cout << mxc + mxr;
  else
    cout << mxc + mxr - 1;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define umap unordered_map
#define mod 1000000007
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define MN(a, b, c) min(a, min(b, c))
#define MX(a, b, c) max(a, max(b, c))
#define pr1 pair<ll, ll>
#define F first
#define S second
#define mP make_pair
#define f(i, n) for (int i = 0; i < n; i++)
#define f1(i, x, y) for (int i = x; i <= y; i++)
#define f2(i, x, y) for (int i = x; i >= y; i--)
#define yes                                                                    \\
  cout << "YES"                                                                \\
       << "\\n"
#define no                                                                     \\
  cout << "NO"                                                                 \\
       << "\\n"
#define modsum(a, b) ((a % mod) + (b % mod)) % mod
#define modpro(a, b) ((a % mod) * (b % mod)) % mod
//__builtin_popcount(x)
//__builtin_parity(x)   =(number of set bits)%2
//__builtin_clz(x)      to count the number of leading  zeroes
//__builtin_ctz(x)      to count the number of trailing zeroes
//__gcd(a,b)
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int R, C, M;
  cin >> R >> C >> M;
  int mx = 0;
  int x, y;
  map<pair<int, int>, int> vis;
  umap<int, int> fr, fc;
  f1(i, 1, M) {
    cin >> x >> y;
    vis[{x, y}] = 1;
    fr[x]++;
    fc[y]++;
  }
  for (auto it1 = fr.begin(); it1 != fr.end(); ++it1) {
    x = it1->first;
    int v1, v2;
    v1 = it1->second;
    for (auto it2 = fc.begin(); it2 != fc.end(); ++it2) {
      y = it2->F, v2 = it2->S;
      if (vis[{x, y}] == 1) {
        mx = max(mx, v1 + v2 - 1);
      } else {
        mx = max(mx, v1 + v2);
      }
    }
  }
  cout << mx;
}