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>

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target(                                                            \\
    "avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace __gnu_pbds;
using namespace std;

#define ld long double
#define pb push_back
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define int long long
#define ull unsigned long long
#define pii pair<int, int>
#define vii vector<int>
#define f first
#define s second
// #define endl "\\n"
#define all(container) container.begin(), container.end()
#define onlyunique(v) v.erase(unique(all(v)), v.end());

#define TRACE
#ifdef AYUSH
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const unordered_set<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const ordered_set<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const multiset<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << '{' << v.first << ' ' << v.second << '}';
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const unordered_map<T, S> &v) {
  for (auto i : v)
    os << '(' << i.first << "=>" << i.second << ')' << ' ';
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
  for (auto i : v)
    os << '(' << i.first << "=>" << i.second << ')' << ' ';
  return os;
}
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero)

int const mod = 1e9 + 7;
int const inf = 1e18;

int power(int x, int y, int mod = 2e18) {
  int ans = 1;
  x %= mod;
  while (y) {
    if (y & 1)
      ans = (x * ans) % mod;
    x = (x * x) % mod;
    y >>= 1;
  }
  return ans;
}
int modinv(int a, int m) { return power(a, m - 2, m); }
const ld PI = 3.1415926535897932384626433832795;

// unordered_map<int, set<int>, > hor, ver;
vector<set<int>> hor, ver;
map<int, vector<int>> mp;
map<int, vector<set<int>>> mp1;

void solve() {
  int n, m, x;
  cin >> n >> m >> x;
  hor.assign(n, set<int>());
  ver.assign(m, set<int>());
  for (int i = 0; i < x; ++i) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    hor[u].insert(v);
    ver[v].insert(u);
  }
  int MAX1 = 0, MAX2 = 0;
  int max1 = 0, max2 = 0;
  for (int i = 0; i < m; ++i) {
    int sz = ver[i].size();
    mp[sz].pb(i);
    if (mp[sz].size() <= 1) {
      if (sz > MAX1) {
        MAX2 = MAX1;
        MAX1 = sz;
      } else if (sz > MAX2 || sz == MAX1) {
        MAX2 = sz;
      }
    }
  }
  trace(mp);
  for (auto x : hor) {
    int sz = (x).size();
    // if(mp1[sz].size() <= 1) {
    mp1[sz].pb(x);
    if (sz > max1) {
      max2 = max1;
      max1 = sz;
    } else if (sz > max2 || sz == max1) {
      max2 = sz;
    }
    // }
  }
  trace(max1, max2);
  int ans = 0;
  for (auto i : {max1, max2}) {
    for (auto y : mp1[i]) {
      for (int j : {MAX1, MAX2}) {
        int sub = 0;
        trace(mp[j], y);
        for (auto xx : mp[j]) {
          if (y.count(xx)) {
            ans = max(ans, i + j - 1);
          } else {
            ans = max(ans, i + j);
            break;
          }
        }
      }
    }
  }
  cout << ans << endl;
}
signed main() {
  IOS int t = 1, T = 1;
  // cin>>t;
  while (t--) {
    // cout<<"Case #"<<T<<": ";
    solve();
    // ++T;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target(                                                            \\
    "avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace __gnu_pbds;
using namespace std;

#define ld long double
#define pb push_back
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define int long long
#define ull unsigned long long
#define pii pair<int, int>
#define vii vector<int>
#define f first
#define s second
#define endl "\\n"
#define all(container) container.begin(), container.end()
#define onlyunique(v) v.erase(unique(all(v)), v.end());

#define TRACE
#ifdef AYUSH
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const unordered_set<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const ordered_set<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const multiset<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << '{' << v.first << ' ' << v.second << '}';
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const unordered_map<T, S> &v) {
  for (auto i : v)
    os << '(' << i.first << "=>" << i.second << ')' << ' ';
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
  for (auto i : v)
    os << '(' << i.first << "=>" << i.second << ')' << ' ';
  return os;
}
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero)

int const mod = 1e9 + 7;
int const inf = 1e18;

int power(int x, int y, int mod = 2e18) {
  int ans = 1;
  x %= mod;
  while (y) {
    if (y & 1)
      ans = (x * ans) % mod;
    x = (x * x) % mod;
    y >>= 1;
  }
  return ans;
}
int modinv(int a, int m) { return power(a, m - 2, m); }
const ld PI = 3.1415926535897932384626433832795;

unordered_map<int, set<int>> hor, ver;
map<int, vector<set<int>>> mp;

void solve() {
  int n, m, x;
  cin >> n >> m >> x;
  for (int i = 0; i < x; ++i) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    hor[u].insert(v);
    ver[v].insert(u);
  }
  int MAX1 = 0, MAX2 = 0;
  for (auto x : ver) {
    int sz = (x.s).size();
    mp[sz].pb(x.s);
    if (sz > MAX1) {
      MAX2 = MAX1;
      MAX1 = sz;
    } else if (sz > MAX2 || sz == MAX1) {
      MAX2 = sz;
    }
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int temp = hor[i].size();
    bool ok = false;
    for (int j : {MAX1, MAX2}) {
      for (auto x : mp[j]) {
        if (x.count(i)) {
          ans = max(ans, temp + j - 1);
        } else {
          ans = max(ans, temp + j);
          ok = true;
          break;
        }
      }
      if (ok)
        break;
    }
  }
  cout << ans << endl;
}
signed main() {
  IOS int t = 1, T = 1;
  // cin>>t;
  while (t--) {
    // cout<<"Case #"<<T<<": ";
    solve();
    // ++T;
  }
  return 0;
}