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>
using namespace std;
using namespace __gnu_pbds;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define pb push_back
#define F first
#define S second
#define print(a)                                                               \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl
#define print1(a)                                                              \\
  for (auto x : a)                                                             \\
  cout << x.F << " " << x.S << endl
#define print2(a, x, y)                                                        \\
  for (int i = x; i < y; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << endl
#define mk(x, y) make_pair(x, y)
typedef long long int ll;
const ll N = 1e6 + 5;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
ll n, k, s, m, l, r, x, w, u, z, d, q, vis_x[N], vis_y[N];
pair<ll, ll> a[N];

ll pow_mod_m(ll a, ll n) {
  if (!n)
    return 1;
  ll pt = pow_mod_m(a, n / 2);
  pt *= pt;
  if (n & 1)
    pt *= a;
  return pt;
}

int main() {
  IOS;
  int ts = 1;
  /// cin >> ts ;
  while (ts--) {
    cin >> n >> m >> k;
    map<ll, vector<ll>> mp_x, mp_y;
    set<pair<ll, ll>> s;
    rep(i, 0, k) {
      cin >> a[i].F >> a[i].S;
      s.insert({a[i].F, a[i].S});
      mp_x[a[i].F].pb(i);
      mp_y[a[i].S].pb(i);
    }
    ll r = 0, c = 0;
    for (auto u : mp_x) {
      r = max((ll)u.S.size(), r);
    }
    for (auto u : mp_y) {
      c = max((ll)u.S.size(), c);
    }
    ll ans = r + c;
    set<ll> row, col;
    for (auto u : mp_x) {
      if (r == u.S.size())
        row.insert(u.F);
    }
    for (auto u : mp_y) {
      if (c == u.S.size())
        col.insert(u.F);
    }
    ll present_at_intersection = 0;
    for (auto u : s) {
      if (row.find(u.F) != row.end() and col.find(u.S) != col.end()) {
        present_at_intersection++;
      }
    }
    if (present_at_intersection == (ll)row.size() * (ll)col.size())
      ans--;
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define pb push_back
#define F first
#define S second
#define print(a)                                                               \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl
#define print1(a)                                                              \\
  for (auto x : a)                                                             \\
  cout << x.F << " " << x.S << endl
#define print2(a, x, y)                                                        \\
  for (int i = x; i < y; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << endl
#define mk(x, y) make_pair(x, y)
typedef long long int ll;
const ll N = 1e6 + 5;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
ll n, k, s, m, l, r, x, w, u, z, d, q, vis_x[N], vis_y[N];
pair<ll, ll> a[N];

ll pow_mod_m(ll a, ll n) {
  if (!n)
    return 1;
  ll pt = pow_mod_m(a, n / 2);
  pt *= pt;
  if (n & 1)
    pt *= a;
  return pt;
}

int main() {
  IOS;
  int ts = 1;
  /// cin >> ts ;
  while (ts--) {
    cin >> n >> m >> k;
    map<ll, vector<ll>> mp_x, mp_y;
    set<pair<ll, ll>> s;
    rep(i, 0, k) {
      cin >> a[i].F >> a[i].S;
      s.insert({a[i].F, a[i].S});
      mp_x[a[i].F].pb(i);
      mp_y[a[i].S].pb(i);
    }
    ll r = 0, c = 0, ans = 0;
    for (auto u : mp_x) {
      r = max((ll)u.S.size(), r);
    }
    for (auto u : mp_y) {
      c = max((ll)u.S.size(), c);
    }
    vector<ll> row, col;
    for (auto u : mp_x) {
      if (r == u.S.size())
        row.pb(u.F);
    }
    for (auto u : mp_y) {
      if (c == u.S.size())
        col.pb(u.F);
    }
    /// print(row) ;
    /// print(col);
    for (auto u : row) {
      for (auto v : col) {
        ///   cout << " current u : " << u << " v : " << v << endl;
        if (s.find({u, v}) != s.end()) {
          ans = max(r + c - 1, ans);
        } else {
          ans = max(r + c, ans);
        }
      }
    }
    cout << ans << endl;
  }
}