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

#include <bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff
typedef long long ll;
typedef long double ld;

typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<ld> vld;

#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()

int main() {
  fastIO;
  int H, W, m;
  cin >> H >> W >> m;
  int maxi = 0, maxj = 0;
  vi h(m), w(m), x, y;
  vi hor(300001, 0), ver(300001, 0);
  map<pair<int, int>, bool> vis;

  for (int i = 0; i < m; i++) {
    cin >> h[i] >> w[i];
    hor[h[i]]++;
    maxi = max(maxi, hor[h[i]]);
    ver[w[i]]++;
    maxj = max(maxj, ver[w[i]]);
    vis[make_pair(h[i], w[i])] = true;
  }

  int n = max(H, W), ans = maxi + maxj;
  for (int i = 1; i <= n; i++) {
    if (hor[i] == maxi)
      x.pb(i);
    if (ver[i] == maxj)
      y.pb(i);
  }

  for (int i = 0; i < x.size(); i++)
    for (int j = 0; j < y.size(); j++)
      if (vis[make_pair(x[i], y[j])] == false) {
        cout << ans << endl;
        return 0;
      }

  cout << ans - 1 << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff
typedef long long ll;
typedef long double ld;

typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<ld> vld;

#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()

struct pair_hash {
  template <class T1, class T2>
  std::size_t operator()(std::pair<T1, T2> const &pair) const {
    std::size_t h1 = std::hash<T1>()(pair.first);
    std::size_t h2 = std::hash<T2>()(pair.second);

    return h1 ^ h2;
  }
};

int main() {
  fastIO;
  int H, W, m;
  cin >> H >> W >> m;
  int maxi = 0, maxj = 0;
  vi h(m), w(m);
  vi hor(300001, 0), ver(300001, 0);
  unordered_set<pair<int, int>, pair_hash> s;
  unordered_set<int> x, y;

  for (int i = 0; i < m; i++) {
    cin >> h[i] >> w[i];
    hor[h[i]]++;
    if (maxi < hor[h[i]])
      maxi = hor[h[i]];
    ver[w[i]]++;
    if (maxj < ver[w[i]])
      maxj = ver[w[i]];
    s.insert({h[i], w[i]});
  }

  for (int i = 1; i <= 300000; i++) {
    if (hor[i] == maxi)
      x.insert(i);
    if (ver[i] == maxj)
      y.insert(i);
  }

  int ans = maxi + maxj;
  for (int i = 1; i <= H; i++)
    for (int j = 1; j <= W; j++)
      if (x.count(i) && y.count(j) && s.count({i, j}) == 0) {
        cout << ans << endl;
        return 0;
      }

  cout << ans - 1 << endl;

  return 0;
}