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

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
#define PUTS(x) std::cout << (x) << endl;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define PB push_back
#define MP make_pair
#define F first
#define S second

template <typename T> struct countmap {
  std::map<T, long long> dict;

  countmap() { dict = map<T, long long>(); }

  bool contains(T key) { return (dict.find(key) != dict.end()); }

  long long operator[](T key) {
    if (dict.find(key) != dict.end()) {
      return dict[key];
    } else {
      return 0;
    }
  }

  // key に対応する値があれば inc 増加させる，なければキーを新設して inc
  // をセット
  void incrementFor(T key, long long inc = 1) {
    if (dict.find(key) != dict.end()) {
      dict[key] += inc;
    } else {
      dict.emplace(key, inc);
    }
  }

  // 登場回数の多い順に (値, 登場回数) のリストを取得
  vector<pair<T, long long>> most_common() {
    auto result = vector<pair<T, long long>>();

    for (const auto &[key, value] : dict) {
      result.push_back(make_pair(key, value));
    }

    sort(result.begin(), result.end(),
         [](const auto &p1, const auto &p2) { return p1.second > p2.second; });

    return result;
  }

  // for (auto &p: dict) でループできるように
  auto begin() const { return dict.begin(); }

  auto end() const { return dict.end(); }
};

using P = pair<Int, Int>;

int main() {
  Int H, W, M;
  cin >> H >> W >> M;
  countmap<Int> hc, wc;
  auto s = set<P>();
  rep(i, M) {
    Int h, w;
    cin >> h >> w;
    hc.incrementFor(h);
    wc.incrementFor(w);
    s.insert(MP(h, w));
  }

  auto hs = hc.most_common();
  auto ws = wc.most_common();

  auto ans = 0LL;
  auto prev_hv = -1LL;
  auto found = false;
  for (auto &hp : hs) {
    auto x = hp.F;
    auto hv = hp.S;
    if (prev_hv > hv) {
      break;
    }
    auto prev_wv = -1LL;
    for (auto &wp : ws) {
      auto y = wp.F;
      auto wv = wp.S;
      if (prev_wv > wv) {
        break;
      }
      auto a = hv + wv;
      if (s.find(MP(x, y)) != s.end()) {
        a--;
      } else {
        found = true;
      }
      ans = max(ans, a);
      prev_wv = wv;
      if (found) {
        break;
      }
    }
    prev_hv = hv;
    if (found) {
      break;
    }
  }
  PUTS(ans)
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
#define PUTS(x) cout << (x) << endl;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define PB push_back
#define MP make_pair
#define F first
#define S second

template <typename T> struct countmap {
  std::map<T, long long> dict;

  countmap() { dict = map<T, long long>(); }

  bool contains(T key) { return (dict.find(key) != dict.end()); }

  long long operator[](T key) {
    if (dict.find(key) != dict.end()) {
      return dict[key];
    } else {
      return 0;
    }
  }

  // key に対応する値があれば inc 増加させる，なければキーを新設して inc
  // をセット
  void incrementFor(T key, long long inc = 1) {
    if (dict.find(key) != dict.end()) {
      dict[key] += inc;
    } else {
      dict.emplace(key, inc);
    }
  }

  // 登場回数の多い順に (値, 登場回数) のリストを取得
  vector<pair<T, long long>> most_common() {
    auto result = vector<pair<T, long long>>();

    for (const auto &[key, value] : dict) {
      result.push_back(make_pair(key, value));
    }

    sort(result.begin(), result.end(),
         [](const auto &p1, const auto &p2) { return p1.second > p2.second; });

    return result;
  }

  // for (auto &p: dict) でループできるように
  auto begin() const { return dict.begin(); }

  auto end() const { return dict.end(); }
};

using P = pair<Int, Int>;

int main() {
  Int H, W, M;
  cin >> H >> W >> M;
  countmap<Int> hc, wc;
  auto s = set<P>();
  rep(i, M) {
    Int h, w;
    cin >> h >> w;
    hc.incrementFor(h);
    wc.incrementFor(w);
    s.insert(MP(h, w));
  }

  auto hs = hc.most_common();
  auto ws = wc.most_common();

  auto ans = 0LL;
  auto prev_hv = -1LL;
  for (auto &hp : hs) {
    auto x = hp.F;
    auto hv = hp.S;
    if (prev_hv > hv) {
      break;
    }
    auto prev_wv = -1LL;
    for (auto &wp : ws) {
      auto y = wp.F;
      auto wv = wp.S;
      if (prev_wv > wv) {
        break;
      }
      auto a = hv + wv;
      if (s.find(MP(x, y)) != s.end()) {
        a--;
      }
      ans = max(ans, a);
      prev_wv = wv;
    }
    prev_hv = hv;
  }
  PUTS(ans)
}