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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define ALL(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define DEBUG_VLL(vec)                                                         \\
  for (int sz = 0; sz < int(vec.size()); sz++)                                 \\
    std::cerr << vec[sz] << (sz == vec.size() - 1 ? '\\n' : ' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

using PI = P<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w, m;
  cin >> h >> w >> m;
  V<int> gyou(h, 0), retsu(w, 0);

  set<PI> hw;
  for (int i = 0; i < m; i++) {
    int hh, ww;
    cin >> hh >> ww;
    hh--, ww--;
    gyou[hh]++, retsu[ww]++;
    hw.insert(make_pair(hh, ww));
  }
  V<int> ordh(h);
  iota(ALL(ordh), 0);
  sort(ALL(ordh), [&](int i, int j) { return gyou[i] > gyou[j]; });
  V<int> ordw(w);
  iota(ALL(ordw), 0);
  sort(ALL(ordw), [&](int i, int j) { return retsu[i] > retsu[j]; });

  int ans = 0;
  for (int i = 0; i < h; i++) {
    if (i > 0 && gyou[ordh[i]] < gyou[ordh[i - 1]])
      break;
    for (int j = 0; j < w; j++) {
      if (j > 0 && retsu[ordw[j]] < retsu[ordw[j - 1]])
        break;
      int tmp = gyou[ordh[i]] + retsu[ordw[j]];
      if (hw.find(make_pair(ordh[i], ordw[j])) != hw.end()) {
        tmp--;
        chmax(ans, tmp);
      } else {
        chmax(ans, tmp);
        break;
      }
    }
  }
  cout << ans << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define ALL(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define DEBUG_VLL(vec)                                                         \\
  for (int sz = 0; sz < int(vec.size()); sz++)                                 \\
    std::cerr << vec[sz] << (sz == vec.size() - 1 ? '\\n' : ' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

using PI = P<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w, m;
  cin >> h >> w >> m;
  V<int> gyou(h, 0), retsu(w, 0);

  set<PI> hw;
  for (int i = 0; i < m; i++) {
    int hh, ww;
    cin >> hh >> ww;
    hh--, ww--;
    gyou[hh]++, retsu[ww]++;
    hw.insert(make_pair(hh, ww));
  }
  V<int> ordh(h);
  iota(ALL(ordh), 0);
  sort(ALL(ordh), [&](int i, int j) { return gyou[i] < gyou[j]; });
  V<int> ordw(w);
  iota(ALL(ordw), 0);
  sort(ALL(ordw), [&](int i, int j) { return retsu[i] < retsu[j]; });

  int ans = 0;
  for (int i = 0; i < h; i++) {
    if (i > 0 && gyou[ordh[i]] < gyou[ordh[i - 1]])
      break;
    for (int j = 0; j < w; j++) {
      if (j > 0 && retsu[ordw[j]] < retsu[ordw[j - 1]])
        break;
      int tmp = gyou[ordh[i]] + retsu[ordw[j]];
      if (hw.find(make_pair(ordh[i], ordw[j])) != hw.end())
        tmp--;
      chmax(ans, tmp);
    }
  }
  cout << ans << '\\n';
  return 0;
}