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

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

using Int = long long;
const char newl = '\\n';

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <typename T> void drop(const T &x) {
  cout << x << endl;
  exit(0);
}
template <typename T = int> vector<T> read(size_t n) {
  vector<T> ts(n);
  for (size_t i = 0; i < n; i++)
    cin >> ts[i];
  return ts;
}

template <typename TV, const int N> void read_tuple_impl(TV &) {}
template <typename TV, const int N, typename Head, typename... Tail>
void read_tuple_impl(TV &ts) {
  get<N>(ts).emplace_back(*(istream_iterator<Head>(cin)));
  read_tuple_impl<TV, N + 1, Tail...>(ts);
}
template <typename... Ts> decltype(auto) read_tuple(size_t n) {
  tuple<vector<Ts>...> ts;
  for (size_t i = 0; i < n; i++)
    read_tuple_impl<decltype(ts), 0, Ts...>(ts);
  return ts;
}

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  int h, w, m;
  cin >> h >> w >> m;
  auto [ys, xs] = read_tuple<int, int>(m);

  using P = pair<int, int>;
  set<P> sp;
  for (int i = 0; i < m; i++)
    sp.emplace(ys[i], xs[i]);

  map<int, int> cy, cx;
  for (int y : ys)
    cy[y]++;
  for (int x : xs)
    cx[x]++;

  int my = 0, mx = 0;
  for (auto [y, c] : cy)
    chmax(my, c);
  for (auto [x, d] : cx)
    chmax(mx, d);

  auto filter = [&](auto mp, int target) {
    vector<int> vs;
    for (auto [key, val] : mp)
      if (val == target)
        vs.emplace_back(key);
    return vs;
  };

  auto yy = filter(cy, my);
  auto xx = filter(cx, mx);
  for (auto y : yy)
    for (auto x : xx)
      if (!sp.count(P(y, x)))
        drop(my + mx);

  cout << my + mx - 1 << newl;
  return 0;
}


Error: TLE

Fixed submission:

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

using Int = long long;
const char newl = '\\n';

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <typename T> void drop(const T &x) {
  cout << x << endl;
  exit(0);
}
template <typename T = int> vector<T> read(size_t n) {
  vector<T> ts(n);
  for (size_t i = 0; i < n; i++)
    cin >> ts[i];
  return ts;
}

template <typename TV, const int N> void read_tuple_impl(TV &) {}
template <typename TV, const int N, typename Head, typename... Tail>
void read_tuple_impl(TV &ts) {
  get<N>(ts).emplace_back(*(istream_iterator<Head>(cin)));
  read_tuple_impl<TV, N + 1, Tail...>(ts);
}
template <typename... Ts> decltype(auto) read_tuple(size_t n) {
  tuple<vector<Ts>...> ts;
  for (size_t i = 0; i < n; i++)
    read_tuple_impl<decltype(ts), 0, Ts...>(ts);
  return ts;
}

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  int h, w, m;
  cin >> h >> w >> m;
  auto [ys, xs] = read_tuple<int, int>(m);

  using P = pair<int, int>;
  set<P> sp;
  for (int i = 0; i < m; i++)
    sp.emplace(ys[i], xs[i]);

  map<int, int> cy, cx;
  for (int y : ys)
    cy[y]++;
  for (int x : xs)
    cx[x]++;

  int my = 0, mx = 0;
  for (auto [y, c] : cy)
    chmax(my, c);
  for (auto [x, d] : cx)
    chmax(mx, d);

  for (auto [y, c] : cy)
    for (auto [x, d] : cx)
      if (c == my and d == mx and !sp.count(P(y, x)))
        drop(my + mx);

  cout << my + mx - 1 << newl;
  return 0;
}