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

#ifdef _DEBUG
#include "../../library/library/debug_template.cpp"
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;

struct init {
  init() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} init_;

template <class T> struct Point2D {
  T x, y;
  constexpr Point2D(T x = 0, T y = 0) noexcept : x(x), y(y){};
  constexpr Point2D(pair<T, T> &p) noexcept : x(p.first), y(p.second){};
  constexpr bool operator==(const Point2D &rhs) const noexcept {
    return x == rhs.x && y == rhs.y;
  }
  constexpr bool operator!=(const Point2D &rhs) const noexcept {
    return !((*this) == rhs);
  }
  constexpr bool operator<(const Point2D &rhs) const noexcept {
    return x < rhs.x || (x == rhs.x && y < rhs.y);
  }
  constexpr bool operator>(const Point2D &rhs) const noexcept {
    return rhs < (*this);
  }
  constexpr bool operator<=(const Point2D &rhs) const noexcept {
    return !((*this) > rhs);
  }
  constexpr bool operator>=(const Point2D &rhs) const noexcept {
    return !((*this) < rhs);
  }
  constexpr Point2D operator+(const Point2D &rhs) const noexcept {
    return {x + rhs.x, y + rhs.y};
  }
  constexpr Point2D operator-(const Point2D &rhs) const noexcept {
    return {x - rhs.x, y - rhs.y};
  }
  constexpr Point2D operator*(const T &k) const noexcept {
    return {k * x, k * y};
  }
  constexpr Point2D operator/(const T &k) const noexcept {
    return {x / k, y / k};
  }
  constexpr Point2D &operator+=(const Point2D &rhs) noexcept {
    return ((*this) = (*this) + rhs);
  }
  constexpr Point2D &operator-=(const Point2D &rhs) noexcept {
    return ((*this) = (*this) - rhs);
  }
  constexpr Point2D &operator*=(const T &k) noexcept {
    return ((*this) = (*this) * k);
  }
  constexpr Point2D &operator/=(const T &k) noexcept {
    return ((*this) = (*this) / k);
  }
  constexpr Point2D operator-() const noexcept { return {-x, -y}; }
  constexpr T operator*(const Point2D &rhs) const noexcept {
    return x * rhs.x + y * rhs.y;
  }
  constexpr Point2D nor() const noexcept { return {y, -x}; }
  constexpr long double hypot() const noexcept { return ::hypotl(x, y); }
  friend istream &operator>>(istream &is, Point2D &p) {
    return is >> p.x >> p.y;
  }
  friend ostream &operator<<(ostream &os, const Point2D &p) {
    return os << p.x << " " << p.y;
  }
  template <class U> U &operator[](vector<vector<U>> &v) { return v[x][y]; }
};

using pnt = Point2D<lint>;

template <class T> inline bool chmax(T &a, const T b) {
  return a < b && (a = b, true);
}

int main() {

  int h, w, m;
  cin >> h >> w >> m;

  set<pnt> p;
  map<int, int> row, col;
  for (int i = 0; i < m; i++) {
    int r, c;
    cin >> r >> c;
    r--, c--;
    p.emplace(r, c);
    row[r]++;
    col[c]++;
  }

  vector<int> indr;
  int max_r = 0;
  for (const auto [r, nr] : row) {
    if (chmax(max_r, nr)) {
      indr.clear();
    }
    if (max_r == nr) {
      indr.emplace_back(r);
    }
  }

  vector<int> indc;
  int max_c = 0;
  for (const auto [c, nc] : col) {
    if (chmax(max_c, nc)) {
      indc.clear();
    }
    if (max_c == nc) {
      indc.emplace_back(c);
    }
  }

  DMP(indr, indc);

  if (indr.size() * indc.size() > m) {
    cout << max_r + max_c << "\\n";
    return 0;
  }

  for (const auto &r : indr) {
    for (const auto &c : indc) {
      DMP(r, c);
      if (p.find(pnt(r, c)) == p.end()) {
        cout << max_r + max_c << "\\n";
        return 0;
      }
    }
  }

  cout << max_r + max_c - 1 << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#ifdef _DEBUG
#include "../../library/library/debug_template.cpp"
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;

struct init {
  init() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} init_;

template <class T> struct Point2D {
  T x, y;
  constexpr Point2D(T x = 0, T y = 0) noexcept : x(x), y(y){};
  constexpr Point2D(pair<T, T> &p) noexcept : x(p.first), y(p.second){};
  constexpr bool operator==(const Point2D &rhs) const noexcept {
    return x == rhs.x && y == rhs.y;
  }
  constexpr bool operator!=(const Point2D &rhs) const noexcept {
    return !((*this) == rhs);
  }
  constexpr bool operator<(const Point2D &rhs) const noexcept {
    return x < rhs.x || (x == rhs.x && y < rhs.y);
  }
  constexpr bool operator>(const Point2D &rhs) const noexcept {
    return rhs < (*this);
  }
  constexpr bool operator<=(const Point2D &rhs) const noexcept {
    return !((*this) > rhs);
  }
  constexpr bool operator>=(const Point2D &rhs) const noexcept {
    return !((*this) < rhs);
  }
  constexpr Point2D operator+(const Point2D &rhs) const noexcept {
    return {x + rhs.x, y + rhs.y};
  }
  constexpr Point2D operator-(const Point2D &rhs) const noexcept {
    return {x - rhs.x, y - rhs.y};
  }
  constexpr Point2D operator*(const T &k) const noexcept {
    return {k * x, k * y};
  }
  constexpr Point2D operator/(const T &k) const noexcept {
    return {x / k, y / k};
  }
  constexpr Point2D &operator+=(const Point2D &rhs) noexcept {
    return ((*this) = (*this) + rhs);
  }
  constexpr Point2D &operator-=(const Point2D &rhs) noexcept {
    return ((*this) = (*this) - rhs);
  }
  constexpr Point2D &operator*=(const T &k) noexcept {
    return ((*this) = (*this) * k);
  }
  constexpr Point2D &operator/=(const T &k) noexcept {
    return ((*this) = (*this) / k);
  }
  constexpr Point2D operator-() const noexcept { return {-x, -y}; }
  constexpr T operator*(const Point2D &rhs) const noexcept {
    return x * rhs.x + y * rhs.y;
  }
  constexpr Point2D nor() const noexcept { return {y, -x}; }
  constexpr long double hypot() const noexcept { return ::hypotl(x, y); }
  friend istream &operator>>(istream &is, Point2D &p) {
    return is >> p.x >> p.y;
  }
  friend ostream &operator<<(ostream &os, const Point2D &p) {
    return os << p.x << " " << p.y;
  }
  template <class U> U &operator[](vector<vector<U>> &v) { return v[x][y]; }
};

using pnt = Point2D<lint>;

template <class T> inline bool chmax(T &a, const T b) {
  return a < b && (a = b, true);
}

int main() {

  int h, w, m;
  cin >> h >> w >> m;

  set<pnt> p;
  map<int, int> row, col;

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    p.emplace(a, b);
    row[a]++;
    col[b]++;
  }

  int ans = 0;
  for (const auto &[r, nr] : row) {
    for (const auto &[c, nc] : col) {
      chmax(ans, nr + nc - (p.find(pnt(r, c)) != p.end()));
    }
  }

  cout << ans << "\\n";

  return 0;
}