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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define endl "\\n"
#define ll long long
#define pb emplace_back
#define fi first
#define se second
#define pll pair<ll, ll>
#define ull unsigned long long
#define ld long double

#define debug(x) cout << #x << " :: " << x << endl;
#define debug2(x, y)                                                           \\
  cout << #x << " :: " << x << "\\t" << #y << " :: " << y << endl;
#define debug3(x, y, z)                                                        \\
  cout << #x << " :: " << x << "\\t" << #y << " :: " << y << "\\t" << #z         \\
       << " :: " << z << endl;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define repa(i, a, b) for (ll i = (a); i <= (b); i++)
#define all(x) (x).begin(), (x).end()
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
/*
 Ordered set docs:
 order_of_key (k) : Number of items strictly smaller than k .
 find_by_order(k) : K-th element in a set (counting from zero).
*/
#define blc boost::lexical_cast<long long>
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
using namespace std;
using namespace __gnu_pbds;
const int MOD = 1e9 + 7;

ll add(ll x, ll y) {
  ll res = x + y;
  return (res >= MOD ? res - MOD : res);
}

ll sub(ll x, ll y) {
  ll res = x - y;
  return (res < 0 ? res + MOD : res);
}

ll lcm(ll x, ll y) {
  ll res = (x * y) / __gcd(x, y);
  return res;
}

ll powerLL(ll x, ll n, ll mod) {
  ll result = 1;
  while (n) {
    if (n & 1)
      result = (result % mod * x % mod) % mod;
    n = n / 2;
    x = (x % mod * x % mod) % mod;
  }
  return result;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

struct hash_pair {
  template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const {
    auto hash1 = hash<T1>{}(p.first);
    auto hash2 = hash<T2>{}(p.second);
    return hash1 ^ hash2;
  }
};
void solve() {
  ll m, n, k;
  cin >> m >> n >> k;
  unordered_map<ll, ll> mr;
  unordered_map<ll, ll> mc;
  // unordered_map<ll, bool> mpp;
  set<pair<ll, ll>> mpp;
  ll maxir = 0;
  ll maxic = 0;
  rep(i, k) {
    ll x, y;
    cin >> x >> y;
    mr[x]++;
    mc[y]++;
    // ll v1 = max(x,y);
    // ll v2 = min(x,y);
    // ll v = (v1*(v1+1))/2+v2;
    mpp.emplace(x, y);
  }
  ll vx = 0, vy = 0;
  vector<ll> v1;
  vector<ll> v2;
  for (auto val : mr) {
    if (val.se > maxir) {
      maxir = val.se;
      vx = val.fi;
    }
  }
  for (auto val : mr) {
    if (val.se == maxir)
      v1.push_back(val.fi);
  }
  for (auto val : mc) {
    if (val.se > maxic) {
      maxic = val.se;
      vy = val.fi;
    }
  }
  for (auto val : mc) {
    if (val.se == maxic)
      v2.push_back(val.fi);
  }
  if ((ll)v1.size() * v2.size() > k) {
    cout << maxir + maxic << endl;
    return;
  }
  for (auto val1 : v1) {
    for (auto val2 : v2) {
      if (!mpp.count({val1, val2})) {
        cout << maxir + maxic << endl;
        return;
      }
    }
  }
  cout << maxir + maxic - 1 << endl;
}
int32_t main() {
  fast;
  int t;
  t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define endl "\\n"
#define ll long long
#define pb emplace_back
#define fi first
#define se second
#define pll pair<ll, ll>
#define ull unsigned long long
#define ld long double

#define debug(x) cout << #x << " :: " << x << endl;
#define debug2(x, y)                                                           \\
  cout << #x << " :: " << x << "\\t" << #y << " :: " << y << endl;
#define debug3(x, y, z)                                                        \\
  cout << #x << " :: " << x << "\\t" << #y << " :: " << y << "\\t" << #z         \\
       << " :: " << z << endl;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define repa(i, a, b) for (ll i = (a); i <= (b); i++)
#define all(x) (x).begin(), (x).end()
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
/*
 Ordered set docs:
 order_of_key (k) : Number of items strictly smaller than k .
 find_by_order(k) : K-th element in a set (counting from zero).
*/
#define blc boost::lexical_cast<long long>
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
using namespace std;
using namespace __gnu_pbds;
const int MOD = 1e9 + 7;

ll add(ll x, ll y) {
  ll res = x + y;
  return (res >= MOD ? res - MOD : res);
}

ll sub(ll x, ll y) {
  ll res = x - y;
  return (res < 0 ? res + MOD : res);
}

ll lcm(ll x, ll y) {
  ll res = (x * y) / __gcd(x, y);
  return res;
}

ll powerLL(ll x, ll n, ll mod) {
  ll result = 1;
  while (n) {
    if (n & 1)
      result = (result % mod * x % mod) % mod;
    n = n / 2;
    x = (x % mod * x % mod) % mod;
  }
  return result;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

struct hash_pair {
  template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const {
    auto hash1 = hash<T1>{}(p.first);
    auto hash2 = hash<T2>{}(p.second);
    return hash1 ^ hash2;
  }
};
void solve() {
  ll m, n, k;
  cin >> m >> n >> k;
  unordered_map<ll, ll> mr;
  unordered_map<ll, ll> mc;
  unordered_map<pair<ll, ll>, bool, hash_pair> mpp;
  ll maxir = 0;
  ll maxic = 0;
  rep(i, k) {
    ll x, y;
    cin >> x >> y;
    mr[x]++;
    mc[y]++;
    mpp[{x, y}] = true;
  }
  ll vx = 0, vy = 0;
  vector<ll> v1;
  vector<ll> v2;
  for (auto val : mr) {
    if (val.se > maxir) {
      maxir = val.se;
      vx = val.fi;
    }
  }
  for (auto val : mr) {
    if (val.se == maxir)
      v1.push_back(val.fi);
  }
  for (auto val : mc) {
    if (val.se > maxic) {
      maxic = val.se;
      vy = val.fi;
    }
  }
  for (auto val : mc) {
    if (val.se == maxic)
      v2.push_back(val.fi);
  }
  for (auto val1 : v2) {
    for (auto val2 : v1) {
      if (mpp.find({val2, val1}) == mpp.end()) {
        cout << maxir + maxic << endl;
        return;
      }
    }
  }
  cout << maxir + maxic - 1 << endl;
}
int32_t main() {
  fast;
  int t;
  t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}