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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
typedef long long ll;

#ifdef _DEBUG
inline void dump() { cerr << endl; }
template <typename Head> void dump(Head &&head) {
  cerr << head;
  dump();
}
template <typename Head, typename... Tail>
void dump(Head &&head, Tail &&...tail) {
  cerr << head << ", ";
  dump(forward<Tail>(tail)...);
}
#define debug(...)                                                             \\
  do {                                                                         \\
    cerr << __LINE__ << ":\\t" << #__VA_ARGS__ << " = ";                        \\
    dump(__VA_ARGS__);                                                         \\
  } while (false)
#else
#define dump(...)
#define debug(...)
#endif

template <typename T> struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

/////////////////////////////////////////////////////////////////////

const ll inf = 1LL << 60;

int main() {
  ll H;
  cin >> H;
  ll W;
  cin >> W;
  ll m;
  cin >> m;
  vector<ll> h(H);
  vector<ll> w(W);
  set<pair<ll, ll>> st;
  for (int i = 0; i < m; i++) {
    ll y, x;
    cin >> y >> x;
    y--;
    x--;
    h[y]++;
    w[x]++;
    st.insert(make_pair(y, x));
  }

  ll hmax = 0;
  ll wmax = 0;
  rep(i, H) hmax = max(hmax, h[i]);
  rep(i, W) wmax = max(wmax, w[i]);

  vector<ll> hh, ww;
  rep(i, H) if (h[i] == hmax) hh.push_back(i);
  rep(i, W) if (w[i] == wmax) ww.push_back(i);

  ll f = -1;
  for (auto i : hh) {
    if (f == 0)
      break;
    for (auto j : ww) {
      if (st.find(make_pair(i, j)) == st.end()) {
        f = 0;
        break;
      }
    }
  }
  cout << hmax + wmax + f << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
typedef long long ll;

#ifdef _DEBUG
inline void dump() { cerr << endl; }
template <typename Head> void dump(Head &&head) {
  cerr << head;
  dump();
}
template <typename Head, typename... Tail>
void dump(Head &&head, Tail &&...tail) {
  cerr << head << ", ";
  dump(forward<Tail>(tail)...);
}
#define debug(...)                                                             \\
  do {                                                                         \\
    cerr << __LINE__ << ":\\t" << #__VA_ARGS__ << " = ";                        \\
    dump(__VA_ARGS__);                                                         \\
  } while (false)
#else
#define dump(...)
#define debug(...)
#endif

template <typename T> struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

/////////////////////////////////////////////////////////////////////

const ll inf = 1LL << 60;

int main() {
  ll H;
  cin >> H;
  ll W;
  cin >> W;
  ll m;
  cin >> m;
  vector<ll> h(H);
  vector<ll> w(W);
  set<pair<ll, ll>> st;
  for (int i = 0; i < m; i++) {
    ll y, x;
    cin >> y >> x;
    y--;
    x--;
    h[y]++;
    w[x]++;
    st.insert(make_pair(y, x));
  }

  ll hmax = 0;
  ll wmax = 0;
  rep(i, H) hmax = max(hmax, h[i]);
  rep(i, W) wmax = max(wmax, w[i]);

  vector<ll> hh, ww;
  rep(i, H) if (h[i] == hmax) hh.push_back(i);
  rep(i, W) if (w[i] == wmax) ww.push_back(i);

  ll f = -1;
  for (auto i : hh)
    for (auto j : ww) {
      if (st.find(make_pair(i, j)) == st.end())
        f = 0;
    }
  cout << hmax + wmax + f << endl;

  return 0;
}