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 __gnu_pbds;
using namespace std;

template <typename T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ull unsigned long long int
#define ll long long int
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define MOD (ull)(double)(1e9 + 7)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vb vector<bool>
#define vd vector<double>
#define vll vector<ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define vvi vector<vi>
#define vvll vector<vll>
#define vs vector<string>
#define pull pair<ull, ull>
#define F first
#define S second
#define m_p make_pair
#define debug(x) cout << #x << " " << x << endl
#define printArr(a, start, end)                                                \\
  for (int i = start; i < end; ++i)                                            \\
    cout << a[i] << " ";                                                       \\
  cout << '\\n';
#define readArr(a, start, end)                                                 \\
  for (int i = start; i < end; ++i)                                            \\
    cin >> a[i];
#define read2D(mat, n, m)                                                      \\
  for (int i = 0; i < n; ++i)                                                  \\
    for (int j = 0; j < m; ++j)                                                \\
      cin >> mat[i][j];

const long double PI = 3.141592653589793238462643383279502884197169399;

// Find Set LSB = (x&(-x)), isPowerOfTwo = (x & (x-1))

const int N = 3e5 + 2;

void solve() {
  int h, w, m;
  cin >> h >> w >> m;

  set<pii> s;
  vpii row(N, {0, 0}), col(N, {0, 0});
  for (int i = 0; i < N; ++i)
    row[i].F = i, col[i].F = i;
  int x, y;
  for (int i = 0; i < m; ++i) {
    cin >> x >> y;
    ++row[x].S;
    ++col[y].S;
    s.insert({x, y});
  }

  sort(all(row), [](pii p1, pii p2) -> bool { return p1.S > p2.S; });
  sort(all(col), [](pii p1, pii p2) -> bool { return p1.S > p2.S; });

  //	for(int i=0 ; row[i].S ; ++i)cout << row[i].F << " " << row[i].S <<
  //endl; 	for(int i=0 ; col[i].S ; ++i)cout << col[i].F << " " << col[i].S <<
  //endl;

  int mxR = row[0].S, mxC = col[0].S;
  if (!mxR) {
    cout << "0";
    return;
  }
  int ans = mxR + mxC - 1;
  vi r, c;
  for (int i = 0; mxR == row[i].S; ++i)
    r.pb(row[i].F);
  for (int i = 0; mxC == col[i].S; ++i)
    c.pb(col[i].F);

  for (int i = 0; i < r.size(); ++i) {
    for (int j = 0; j < c.size(); ++j) {
      if (s.find({r[i], c[j]}) == s.end()) {
        ans = mxR + mxC;
        break;
      }
    }
    if (ans == mxR + mxC)
      break;
  }
  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cout << fixed << setprecision(10);

  int tt = 1;
  // cin >> tt;

  while (tt--) {
    solve();
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

template <typename T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ull unsigned long long int
#define ll long long int
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define MOD (ull)(double)(1e9 + 7)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vb vector<bool>
#define vd vector<double>
#define vll vector<ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define vvi vector<vi>
#define vvll vector<vll>
#define vs vector<string>
#define pull pair<ull, ull>
#define F first
#define S second
#define m_p make_pair
#define debug(x) cout << #x << " " << x << endl
#define printArr(a, start, end)                                                \\
  for (int i = start; i < end; ++i)                                            \\
    cout << a[i] << " ";                                                       \\
  cout << '\\n';
#define readArr(a, start, end)                                                 \\
  for (int i = start; i < end; ++i)                                            \\
    cin >> a[i];
#define read2D(mat, n, m)                                                      \\
  for (int i = 0; i < n; ++i)                                                  \\
    for (int j = 0; j < m; ++j)                                                \\
      cin >> mat[i][j];

const long double PI = 3.141592653589793238462643383279502884197169399;

// Find Set LSB = (x&(-x)), isPowerOfTwo = (x & (x-1))

const int N = 3e5 + 2;

void solve() {
  int h, w, m;
  cin >> h >> w >> m;

  set<pii> s;
  vpii row(N, {0, 0}), col(N, {0, 0});
  for (int i = 0; i < N; ++i)
    row[i].F = i, col[i].F = i;
  int x, y;
  for (int i = 0; i < m; ++i) {
    cin >> x >> y;
    ++row[x].S;
    ++col[y].S;
    s.insert({x, y});
  }

  sort(all(row), [](pii p1, pii p2) -> bool { return p1.S > p2.S; });
  sort(all(col), [](pii p1, pii p2) -> bool { return p1.S > p2.S; });

  //	for(int i=0 ; row[i].S ; ++i)cout << row[i].F << " " << row[i].S <<
  //endl; 	for(int i=0 ; col[i].S ; ++i)cout << col[i].F << " " << col[i].S <<
  //endl;

  int ans = row[0].S + col[0].S - (s.find({row[0].F, col[0].F}) != s.end());
  for (int i = 0; i < N; ++i) {
    if (!row[i].S)
      break;
    for (int j = 0; j < N; ++j) {
      int cnt = row[i].S + col[j].S;
      if (cnt < ans)
        break;
      cnt -= (s.find({row[i].F, col[j].F}) != s.end());
      ans = max(ans, cnt);
    }
  }

  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cout << fixed << setprecision(10);

  int tt = 1;
  // cin >> tt;

  while (tt--) {
    solve();
  }
}