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

#include <bits/stdc++.h>
using namespace std;

#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(12);
#define randomINT                                                              \\
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define newl cout << "\\n"
#define DISP(as)                                                               \\
  for (auto it : as)                                                           \\
    cout << it << " ";                                                         \\
  newl;
#define all(x) (x).begin(), (x).end()
#define mset(x, v) memset(x, v, sizeof(x))
#define newl cout << "\\n"
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define dline cerr << "///REACHED///\\n";
#define deb1(x) cerr << #x << " = " << x << '\\n';
#define deb2(x, y)                                                             \\
  cerr << '[' << #x << ',' << #y << "] = " << '[' << x << ',' << y << ']'      \\
       << '\\n';
#define deb3(x, y, z)                                                          \\
  cerr << '[' << #x << ',' << #y << ',' << #z << "] = " << '[' << x << ','     \\
       << y << ',' << z << ']' << '\\n';

typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef unordered_map<ll, ll> um;
typedef vector<pll> vpll;

const ll MAX5 = 1e+5 + 7;
const ll MAX7 = 1e+7 + 7;
const ll MAXN = MAX7;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
const ll MOD = 1e+9 + 7;
const ll N = 2 * 1e6;

set<ll> adj[N];
ll lvl[N] = {};
// int dx8[]={0,1,1,1,0,-1,-1,-1}, dy8[]={1,1,0,-1,-1,-1,0,1};

// <------------- Declare Variables Here ------------> //

ll T = 1;
ll n, m, p, q, k;
string s, t;

// <------------- Implement Functions Here ----------> //
set<pll, greater<pll>> X, Y;
map<ll, ll> x, y;
set<pll> ppp;

// <------------- Start of main() -------------------> //

void MAIN() {
  cin >> n >> m >> k;
  for (ll i = 0; i < k; i++) {
    cin >> p >> q;
    x[p]++;
    y[q]++;
    ppp.insert({p, q});
  }

  for (auto it : x) {
    X.insert({it.s, it.f});
  }

  for (auto it : y) {
    Y.insert({it.s, it.f});
  }

  pll maxx = *(X.begin()), maxy = *(Y.begin());
  ll maxi = maxx.f + maxy.f;

  if (ppp.find({maxx.s, maxy.s}) == ppp.end())
    cout << maxi;
  else {
    vll xx, yy;
    while ((*X.begin()).f == maxx.f) {
      xx.pb((*X.begin()).s);
      X.erase(X.begin());
    }
    while ((*Y.begin()).f == maxy.f) {
      yy.pb((*Y.begin()).s);
      Y.erase(Y.begin());
    }

    for (auto xi : xx) {
      for (auto yi : yy) {
        if (ppp.find({xi, yi}) == ppp.end()) {
          cout << maxi;
          return;
        }
      }
    }

    cout << maxi - 1;
  }
}

int main() {
  fastio;
  randomINT;
  // cin>>T;
  while (T--) {
    MAIN();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(12);
#define randomINT                                                              \\
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define newl cout << "\\n"
#define DISP(as)                                                               \\
  for (auto it : as)                                                           \\
    cout << it << " ";                                                         \\
  newl;
#define all(x) (x).begin(), (x).end()
#define mset(x, v) memset(x, v, sizeof(x))
#define newl cout << "\\n"
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define dline cerr << "///REACHED///\\n";
#define deb1(x) cerr << #x << " = " << x << '\\n';
#define deb2(x, y)                                                             \\
  cerr << '[' << #x << ',' << #y << "] = " << '[' << x << ',' << y << ']'      \\
       << '\\n';
#define deb3(x, y, z)                                                          \\
  cerr << '[' << #x << ',' << #y << ',' << #z << "] = " << '[' << x << ','     \\
       << y << ',' << z << ']' << '\\n';

typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef unordered_map<ll, ll> um;
typedef vector<pll> vpll;

const ll MAX5 = 1e+5 + 7;
const ll MAX7 = 1e+7 + 7;
const ll MAXN = MAX7;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
const ll MOD = 1e+9 + 7;
const ll N = 2 * 1e6;

set<ll> adj[N];
ll lvl[N] = {};
// int dx8[]={0,1,1,1,0,-1,-1,-1}, dy8[]={1,1,0,-1,-1,-1,0,1};

// <------------- Declare Variables Here ------------> //

ll T = 1;
ll n, m, p, q, k;
string s, t;

// <------------- Implement Functions Here ----------> //
map<ll, set<ll>, greater<ll>> X, Y;
um x, y;
set<pll> ppp;

// <------------- Start of main() -------------------> //

void MAIN() {
  cin >> n >> m >> k;
  for (ll i = 0; i < k; i++) {
    cin >> p >> q;
    x[p]++;
    y[q]++;
    ppp.insert({p, q});
  }

  for (auto it : x) {
    X[it.s].insert(it.f);
  }

  for (auto it : y) {
    Y[it.s].insert(it.f);
  }

  ll ans = 0;
  ll itr = 1;
  for (auto maxi : X) {
    ll local = maxi.f;
    for (auto it : y) {
      for (auto it2 : maxi.s)
        if (ppp.find({it2, it.f}) != ppp.end())
          ans = max(ans, local + it.s - 1);
        else
          ans = max(ans, local + it.s);
    }
    break;
  }

  for (auto maxi : Y) {
    ll local = maxi.f;
    for (auto it : x) {
      for (auto it2 : maxi.s)
        if (ppp.find({it.f, it2}) != ppp.end())
          ans = max(ans, local + it.s - 1);
        else
          ans = max(ans, local + it.s);
    }
    break;
  }

  cout << ans;
}

int main() {
  fastio;
  randomINT;
  // cin>>T;
  while (T--) {
    MAIN();
  }
  return 0;
}