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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> Pi;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, n) for (int i = 1; i <= (n); i++)
#define rep3(i, i0, n) for (int i = i0; i < (n); i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
  if (a.first != b.first) {
    return a.first < b.first;

  } else {
    return a.second < b.second;
  }
}

bool In_map(ll y, ll x, ll h, ll w) {
  if (y < 0 || x < 0 || y >= h || x >= w) {
    return 0;
  } else {
    return 1;
  }
}
const vector<ll> dx{1, 0, -1, 0};
const vector<ll> dy{0, 1, 0, -1};

int main() {
  ll H, W, M;
  cin >> H >> W >> M;
  vector<Pi> HB(300005);
  vector<Pi> WB(300005);
  map<Pi, bool> m;
  rep(i, 300005) {
    HB[i].second = i;
    WB[i].second = i;
  }
  rep(i, M) {
    ll h, w;
    cin >> h >> w;
    h--;
    w--;
    HB[h].first++;
    WB[w].first++;
    m[mp(h, w)] = 1;
  }
  sort(all(HB));
  reverse(all(HB));
  sort(all(WB));
  reverse(all(WB));
  ll mx = HB[0].first + WB[0].first;
  rep(i, 300005) {
    rep(j, 300005) {
      if (WB[j].first < WB[0].first)
        break;
      if (HB[i].first + WB[j].first == mx) {
        if (m[mp(HB[i].second, WB[j].second)] == 0) {
          cout << mx << endl;
          return 0;
        }
      } else if (HB[i].first < HB[0].first) {
        cout << mx - 1 << endl;
        return 0;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> Pi;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, n) for (int i = 1; i <= (n); i++)
#define rep3(i, i0, n) for (int i = i0; i < (n); i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
  if (a.first != b.first) {
    return a.first < b.first;

  } else {
    return a.second < b.second;
  }
}

bool In_map(ll y, ll x, ll h, ll w) {
  if (y < 0 || x < 0 || y >= h || x >= w) {
    return 0;
  } else {
    return 1;
  }
}
const vector<ll> dx{1, 0, -1, 0};
const vector<ll> dy{0, 1, 0, -1};

int main() {
  ll H, W, M;
  cin >> H >> W >> M;
  vector<Pi> HB(300005);
  vector<Pi> WB(300005);
  map<Pi, bool> m;
  rep(i, 300005) {
    HB[i].second = i;
    WB[i].second = i;
  }
  rep(i, M) {
    ll h, w;
    cin >> h >> w;
    h--;
    w--;
    HB[h].first++;
    WB[w].first++;
    m[mp(h, w)] = 1;
  }
  sort(all(HB));
  reverse(all(HB));
  sort(all(WB));
  reverse(all(WB));
  ll mx = HB[0].first + WB[0].first;
  rep(i, 300005) {
    rep(j, 300005) {
      if (HB[i].first + WB[j].first == mx) {
        if (m[mp(HB[i].second, WB[j].second)] == 0) {
          cout << mx << endl;
          return 0;
        }
      } else if (HB[i].first < HB[0].first && WB[i].first < WB[0].first) {
        cout << mx - 1 << endl;
        return 0;
      }
    }
  }

  return 0;
}