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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll, ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i, m, n) for (ll(i) = (m), (i_len) = (n); (i) < (i_len); ++(i))
#define FORR(i, v) for (auto(i) : v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x)                                                                \\
  ASC((x));                                                                    \\
  REV((x))
#define pb push_back
#define eb emplace_back

int main() {
  ll H, W, M;
  cin >> H >> W >> M;
  vec S(H, 0), T(W, 0);
  set<pll> U;
  set<pll> B;
  REP(i, 0, M) {
    ll p, q;
    cin >> p >> q;
    --p;
    --q;
    ++S[p];
    ++T[q];
    U.insert({p, q});
  }

  ll ms = 0, mt = 0;
  REP(i, 0, H) ms = MAX(ms, S[i]);
  REP(i, 0, W) {
    mt = MAX(mt, T[i]);
    B.insert({T[i], i});
  }

  ll m = ms + mt;
  ll ans = 0;
  bool f = false;
  REP(i, 0, H) {
    ll a = m - S[i];
    auto from = B.lower_bound({a, 0});
    auto to = B.lower_bound({a, INF});
    if (from != to) {

      for (auto it = from; it != to; ++it) {
        ll c = it->second;
        if (U.count({i, c}))
          ans = MAX(ans, m - 1);
        else {
          ans = MAX(ans, m);
          f = true;
          break;
        }
      }
    }
    if (f)
      break;
  }
  PR(ans);

  return 0;
}

/*



*/

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll, ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i, m, n) for (ll(i) = (m), (i_len) = (n); (i) < (i_len); ++(i))
#define FORR(i, v) for (auto(i) : v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x)                                                                \\
  ASC((x));                                                                    \\
  REV((x))
#define pb push_back
#define eb emplace_back

int main() {
  ll H, W, M;
  cin >> H >> W >> M;
  vec S(H, 0), T(W, 0);
  set<pll> U;
  set<pll> A, B;
  REP(i, 0, M) {
    ll p, q;
    cin >> p >> q;
    --p;
    --q;
    ++S[p];
    ++T[q];
    U.insert({p, q});
  }

  ll ms = 0, mt = 0;
  REP(i, 0, H) {
    ms = MAX(ms, S[i]);
    A.insert({S[i], i});
  }
  REP(i, 0, W) {
    mt = MAX(mt, T[i]);
    B.insert({T[i], i});
  }

  ll m = ms + mt;
  ll ans = 0;
  REP(i, 0, H) {
    ll a = m - S[i];
    auto from = B.lower_bound({a, 0});
    auto to = B.lower_bound({a, INF});
    if (from != to) {
      for (auto it = from; it != to; ++it) {
        ll c = it->second;
        if (U.count({i, c}))
          ans = MAX(ans, m - 1);
        else
          ans = MAX(ans, m);
      }
    }
  }
  PR(ans);

  return 0;
}

/*



*/