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

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 9999999999999999;
using ll = long long;
int main() {
  ll H, W, M;
  cin >> H >> W >> M;
  vector<vector<ll>> a(M, vector<ll>(2));
  vector<ll> hhs(H + 1);
  vector<ll> wws(W + 1);
  for (ll i = 0; i < M; i++) {
    cin >> a[i][0] >> a[i][1];
    hhs[a[i][0]]++;
    wws[a[i][1]]++;
  }
  vector<vector<ll>> hh((H + 1), vector<ll>(2));
  vector<vector<ll>> ww((W + 1), vector<ll>(2));
  for (ll i = 0; i < M; i++) {
    hh[a[i][0]][0]++;
  }
  for (ll i = 0; i < M; i++) {
    ww[a[i][1]][0]++;
  }
  for (ll i = 0; i < (H + 1); i++) {
    hh[i][1] = i;
  }
  for (ll i = 0; i < (W + 1); i++) {
    ww[i][1] = i;
  }

  sort(hh.begin(), hh.end());
  sort(ww.begin(), ww.end());

  reverse(hh.begin(), hh.end());
  reverse(ww.begin(), ww.end());

  vector<ll> hhmax;
  ll hhmaxans = hh[0][0];
  vector<ll> wwmax;
  ll wwmaxans = ww[0][0];
  // cout <<hh[0][0]+ww[0][0] << endl;
  ll lo = 0;
  while (1) {
    if (hh[lo][0] == hhmaxans) {
      hhmax.push_back(hh[lo][1]);
      lo++;
    } else {
      break;
    }
  }
  lo = 0;

  while (1) {
    if (ww[lo][0] == wwmaxans) {
      wwmax.push_back(ww[lo][1]);
      lo++;
    } else {
      break;
    }
  }
  ll anstemp = hh[0][0] + ww[0][0];
  ll temp = 0;
  for (ll i = 0; i < M; i++) {
    // cout << hhs[a[i][0]] << " " << wws[a[i][1]] << endl;
    if (hhs[a[i][0]] + wws[a[i][1]] == anstemp) {
      temp++;
    }
  }

  ll anstemptemp = hhmax.size() * wwmax.size();

  if (temp != anstemptemp) {
    cout << hh[0][0] + ww[0][0] << endl;
  } else {
    cout << hh[0][0] + ww[0][0] - 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 9999999999999999;
using ll = long long;
int main() {
  ll H, W, M;
  cin >> H >> W >> M;
  vector<vector<ll>> a(M, vector<ll>(2));
  for (ll i = 0; i < M; i++) {
    cin >> a[i][0] >> a[i][1];
  }
  vector<vector<ll>> hh((H + 1), vector<ll>(2));
  vector<vector<ll>> ww((W + 1), vector<ll>(2));
  for (ll i = 0; i < M; i++) {
    hh[a[i][0]][0]++;
  }
  for (ll i = 0; i < M; i++) {
    ww[a[i][1]][0]++;
  }
  for (ll i = 0; i < (H + 1); i++) {
    hh[i][1] = i;
  }
  for (ll i = 0; i < (W + 1); i++) {
    ww[i][1] = i;
  }

  sort(hh.begin(), hh.end());
  sort(ww.begin(), ww.end());

  reverse(hh.begin(), hh.end());
  reverse(ww.begin(), ww.end());

  vector<ll> hhmax;
  ll hhmaxans = hh[0][0];
  vector<ll> wwmax;
  ll wwmaxans = ww[0][0];
  // cout <<hh[0][0]+ww[0][0] << endl;
  ll lo = 0;
  while (1) {
    if (hh[lo][0] == hhmaxans) {
      hhmax.push_back(hh[lo][1]);
      lo++;
    } else {
      break;
    }
  }
  lo = 0;

  while (1) {
    if (ww[lo][0] == wwmaxans) {
      wwmax.push_back(ww[lo][1]);
      lo++;
    } else {
      break;
    }
  }

  // cout << hhmax.size() << wwmax.size() << endl;
  ll hon = 0;
  ll mon = 0;
  for (ll i = 0; i < hhmax.size(); i++) {
    for (ll k = 0; k < wwmax.size(); k++) {
      hon = 0;
      for (ll e = 0; e < M; e++) {
        if ((a[e][0] == hhmax[i]) && (a[e][1] == wwmax[k])) {
          hon = 1;
        }
      }
      if (hon == 1) {
        mon = 1;
      } else {
        mon = 0;
        i = hhmax.size() + 1;
        k = wwmax.size() + 1;
      }
    }
  }
  if (mon == 0) {
    cout << hh[0][0] + ww[0][0] << endl;
  } else {
    cout << hh[0][0] + ww[0][0] - 1 << endl;
  }
  return 0;
}