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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(i) i.begin(), i.end()
template <typename T = ll> using v = vector<T>;
template <typename T = ll> using vv = v<v<T>>;
template <typename T = ll, typename U = ll> using p = pair<T, U>;
template <typename T> T chmin(T &a, T b) { return a = min(a, b); }
template <typename T> T chmax(T &a, T b) { return a = max(a, b); }
const ll INF = (ll)1e9;
template <int MOD> struct ModInt {
  using lint = long long;
  int val;

  // constructor
  ModInt(lint v = 0) : val(v % MOD) {
    if (val < 0)
      val += MOD;
  };

  // unary operator
  ModInt operator+() const { return ModInt(val); }
  ModInt operator-() const { return ModInt(MOD - val); }
  ModInt inv() const { return this->pow(MOD - 2); }

  // arithmetic
  ModInt operator+(const ModInt &x) const { return ModInt(*this) += x; }
  ModInt operator-(const ModInt &x) const { return ModInt(*this) -= x; }
  ModInt operator*(const ModInt &x) const { return ModInt(*this) *= x; }
  ModInt operator/(const ModInt &x) const { return ModInt(*this) /= x; }
  ModInt pow(lint n) const {
    auto x = ModInt(1);
    auto b = *this;
    while (n > 0) {
      if (n & 1)
        x *= b;
      n >>= 1;
      b *= b;
    }
    return x;
  }

  // compound assignment
  ModInt &operator+=(const ModInt &x) {
    if ((val += x.val) >= MOD)
      val -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt &x) {
    if ((val -= x.val) < 0)
      val += MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt &x) {
    val = lint(val) * x.val % MOD;
    return *this;
  }
  ModInt &operator/=(const ModInt &x) { return *this *= x.inv(); }

  // compare
  bool operator==(const ModInt &b) const { return val == b.val; }
  bool operator!=(const ModInt &b) const { return val != b.val; }

  // I/O
  friend std::istream &operator>>(std::istream &is, ModInt &x) noexcept {
    return is >> x.val;
  }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &x) noexcept {
    return os << x.val;
  }
};

constexpr int MOD = 1e9 + 7;
using mint = ModInt<MOD>;
void a() {
  ll N, X, T;
  cin >> N >> X >> T;
  cout << N / X * T + (N % X != 0 ? T : 0) << endl;
}

void b() {
  char c = '0';
  ll num = 0;
  while ('0' <= c && c <= '9') {
    scanf("%c", &c);
    if (c - '0' < 0)
      break;
    num += c - '0';
    num %= 9;
  }

  if (num == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

void c() {
  ll N;
  cin >> N;
  auto A = v<>(N, 0);
  ll _max = 0;
  ll ans = 0;
  rep(i, N) {
    cin >> A[i];
    if (A[i] < _max) {
      ans += _max - A[i];
    }
    chmax(_max, A[i]);
  }
  cout << ans << endl;
}

void d() {
  ll H, W;
  cin >> H >> W;
  auto G = v<string>(H);
  ll ch, cw, dh, dw;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  rep(i, H) { cin >> G[i]; }
  auto dist = v<v<>>(H, v<>(W, INF));

  deque<p<>> q;
  q.push_front(p<>(ch, cw));

  dist[ch][cw] = 0;
  while (!q.empty()) {
    auto u = q.front();
    q.pop_front();
    if (dh == u.first && dw == u.second) {
      cout << dist[dh][dw] << endl;
      return;
    }

    static const ll dh[4] = {0, 1, 0, -1};
    static const ll dw[4] = {1, 0, -1, 0};

    rep(next, 4) {
      ll nh = u.first + dh[next];
      ll nw = u.second + dw[next];
      if (0 <= nh && nh < H && 0 <= nw && nw < W && G[nh][nw] == '.' &&
          dist[nh][nw] > dist[u.first][u.second]) {
        dist[nh][nw] = dist[u.first][u.second];
        q.push_front(p<>(nh, nw));
      }
    }

    for (ll ddh = -2; ddh <= 2; ddh++) {
      for (ll ddw = -2; ddw <= 2; ddw++) {
        ll nh = u.first + ddh;
        ll nw = u.second + ddw;
        if (0 <= nh && nh < H && 0 <= nw && nw < W && G[nh][nw] == '.' &&
            dist[nh][nw] > dist[u.first][u.second] + 1) {
          dist[nh][nw] = dist[u.first][u.second] + 1;
          q.push_back(p<>(nh, nw));
        }
      }
    }
  }

  cout << -1 << endl;
}

void e() {
  ll H, W, M;
  cin >> H >> W >> M;
  auto cnth = map<ll, ll>();
  auto cntw = map<ll, ll>();

  auto hw = set<p<>>();
  auto _maxh = 0LL;
  auto _maxw = 0LL;
  rep(i, M) {
    ll s, t;
    cin >> s >> t;
    hw.insert({--s, --t});
    chmax(_maxh, ++cnth[s]);
    chmax(_maxw, ++cntw[t]);
  }

  ll ans = _maxh + _maxw;

  auto hh = v<>();
  auto ww = v<>();

  for (auto i : cnth)
    if (i.second == _maxh)
      hh.push_back(i.first);
  for (auto i : cntw)
    if (i.second == _maxw)
      ww.push_back(i.first);

  for (auto j : hh)
    for (auto k : ww) {
      if (hw.find({j, k}) == hw.end()) {
        cout << ans << endl;
        return;
      }
    }

  cout << --ans << endl;
}

int main(void) {
  e();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(i) i.begin(), i.end()
template <typename T = ll> using v = vector<T>;
template <typename T = ll> using vv = v<v<T>>;
template <typename T = ll, typename U = ll> using p = pair<T, U>;
template <typename T> T chmin(T &a, T b) { return a = min(a, b); }
template <typename T> T chmax(T &a, T b) { return a = max(a, b); }
const ll INF = (ll)1e9;
template <int MOD> struct ModInt {
  using lint = long long;
  int val;

  // constructor
  ModInt(lint v = 0) : val(v % MOD) {
    if (val < 0)
      val += MOD;
  };

  // unary operator
  ModInt operator+() const { return ModInt(val); }
  ModInt operator-() const { return ModInt(MOD - val); }
  ModInt inv() const { return this->pow(MOD - 2); }

  // arithmetic
  ModInt operator+(const ModInt &x) const { return ModInt(*this) += x; }
  ModInt operator-(const ModInt &x) const { return ModInt(*this) -= x; }
  ModInt operator*(const ModInt &x) const { return ModInt(*this) *= x; }
  ModInt operator/(const ModInt &x) const { return ModInt(*this) /= x; }
  ModInt pow(lint n) const {
    auto x = ModInt(1);
    auto b = *this;
    while (n > 0) {
      if (n & 1)
        x *= b;
      n >>= 1;
      b *= b;
    }
    return x;
  }

  // compound assignment
  ModInt &operator+=(const ModInt &x) {
    if ((val += x.val) >= MOD)
      val -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt &x) {
    if ((val -= x.val) < 0)
      val += MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt &x) {
    val = lint(val) * x.val % MOD;
    return *this;
  }
  ModInt &operator/=(const ModInt &x) { return *this *= x.inv(); }

  // compare
  bool operator==(const ModInt &b) const { return val == b.val; }
  bool operator!=(const ModInt &b) const { return val != b.val; }

  // I/O
  friend std::istream &operator>>(std::istream &is, ModInt &x) noexcept {
    return is >> x.val;
  }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &x) noexcept {
    return os << x.val;
  }
};

constexpr int MOD = 1e9 + 7;
using mint = ModInt<MOD>;
void a() {
  ll N, X, T;
  cin >> N >> X >> T;
  cout << N / X * T + (N % X != 0 ? T : 0) << endl;
}

void b() {
  char c = '0';
  ll num = 0;
  while ('0' <= c && c <= '9') {
    scanf("%c", &c);
    if (c - '0' < 0)
      break;
    num += c - '0';
    num %= 9;
  }

  if (num == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

void c() {
  ll N;
  cin >> N;
  auto A = v<>(N, 0);
  ll _max = 0;
  ll ans = 0;
  rep(i, N) {
    cin >> A[i];
    if (A[i] < _max) {
      ans += _max - A[i];
    }
    chmax(_max, A[i]);
  }
  cout << ans << endl;
}

void d() {
  ll H, W;
  cin >> H >> W;
  auto G = v<string>(H);
  ll ch, cw, dh, dw;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  rep(i, H) { cin >> G[i]; }
  auto dist = v<v<>>(H, v<>(W, INF));

  deque<p<>> q;
  q.push_front(p<>(ch, cw));

  dist[ch][cw] = 0;
  while (!q.empty()) {
    auto u = q.front();
    q.pop_front();
    if (dh == u.first && dw == u.second) {
      cout << dist[dh][dw] << endl;
      return;
    }

    static const ll dh[4] = {0, 1, 0, -1};
    static const ll dw[4] = {1, 0, -1, 0};

    rep(next, 4) {
      ll nh = u.first + dh[next];
      ll nw = u.second + dw[next];
      if (0 <= nh && nh < H && 0 <= nw && nw < W && G[nh][nw] == '.' &&
          dist[nh][nw] > dist[u.first][u.second]) {
        dist[nh][nw] = dist[u.first][u.second];
        q.push_front(p<>(nh, nw));
      }
    }

    for (ll ddh = -2; ddh <= 2; ddh++) {
      for (ll ddw = -2; ddw <= 2; ddw++) {
        ll nh = u.first + ddh;
        ll nw = u.second + ddw;
        if (0 <= nh && nh < H && 0 <= nw && nw < W && G[nh][nw] == '.' &&
            dist[nh][nw] > dist[u.first][u.second] + 1) {
          dist[nh][nw] = dist[u.first][u.second] + 1;
          q.push_back(p<>(nh, nw));
        }
      }
    }
  }

  cout << -1 << endl;
}

void e() {
  ll H, W, M;
  cin >> H >> W >> M;
  auto cnth = map<ll, ll>();
  auto cntw = map<ll, ll>();

  auto hw = v<p<>>(M, p<>(0, 0));
  auto _maxh = 0LL;
  auto _maxw = 0LL;
  rep(i, M) {
    cin >> hw[i].first >> hw[i].second;
    hw[i].first--;
    hw[i].second--;
    chmax(_maxh, ++cnth[hw[i].first]);
    chmax(_maxw, ++cntw[hw[i].second]);
  }

  ll ans = _maxh + _maxw;

  auto hh = v<>();
  auto ww = v<>();

  for (auto i : cnth)
    if (i.second == _maxh)
      hh.push_back(i.first);
  for (auto i : cntw)
    if (i.second == _maxw)
      ww.push_back(i.first);

  for (auto j : hh)
    for (auto k : ww) {
      if (find(all(hw), p<>(j, k)) == hw.end()) {
        cout << ans << endl;
        return;
      }
    }

  cout << --ans << endl;
}

int main(void) {
  e();
  return 0;
}