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

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define repi(i, x)                                                             \\
  for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
string solve(bool a) { return ((a) ? "Yes" : "No"); }
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int, int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long, long long> P;
typedef vector<P> VP;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  rep(i, v.size()) { is >> v[i]; }
  return is;
}
const long long INFLL = 1LL << 60;
const int INF = 1 << 30;
const double PI = acos(-1);
int main() {
  int h, w, m;
  int ans = 0;
  cin >> h >> w >> m;
  Pi data;
  VPi tri;
  map<int, int> X, Y;
  vector<int> x, y;
  int ux = 0, uy = 0;
  rep(i, m) {
    cin >> data;
    tri.pb(data);
    X[data.F]++;
    Y[data.S]++;
  }
  tri.eb(INF, INF);
  sort(all(tri));
  repi(itr, X) {
    if (ux < itr->S) {
      ux = itr->S;
      x.clear();
    }
    if (ux == itr->S) {
      x.pb(itr->F);
    }
  }
  repi(itr, Y) {
    if (uy < itr->S) {
      uy = itr->S;
      y.clear();
    }
    if (uy == itr->S) {
      y.pb(itr->F);
    }
  }
  //	clog << ux <<" "<< uy << endl;
  ans = ux + uy - 1;
  rep(i, x.size()) {
    rep(j, y.size()) {
      if (*lower_bound(all(tri), mp(x[i], y[j])) != mp(x[i], y[j])) {
        ans++;
        cout << ans << endl;
        return 0;
      }
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define repi(i, x)                                                             \\
  for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++)
#define all(x) (x).begin(), (x).end()
#define input(array)                                                           \\
  repi(_itr, array) { cin >> *(_itr); }
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
string solve(bool a) { return ((a) ? "Yes" : "No"); }
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int, int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long, long long> P;
typedef vector<P> VP;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}
const long long INFLL = 1LL << 60;
const int INF = 1 << 30;
const double PI = acos(-1);
int main() {
  int h, w, m;
  int ans = 0;
  cin >> h >> w >> m;
  map<int, int> disa, disb;
  int hh, ww;
  int hup = 0;
  int wup = 0;
  Vi hl, wl;
  vector<pair<int, int>> data(m + 1);
  rep(i, m) {
    cin >> hh >> ww;
    hh--;
    ww--;
    data[i] = mp(hh, ww);
    disa[hh]++;
    disb[ww]++;
    if (chmax(hup, disa[hh])) {
      hl.clear();
    }
    if (chmax(wup, disb[ww])) {
      wl.clear();
    }
    if (hup == disa[hh]) {
      hl.pb(hh);
    }
    if (wup == disb[ww]) {
      wl.pb(ww);
    }
  }

  data[m] = mp(INF, INF);
  sort(all(data));
  ans = hup + wup - 1;
  clog << endl;
  clog << hup << " " << wup << endl;
  clog << hl.size() << " " << wl.size() << endl;
  clog << hl << endl;
  clog << wl << endl;
  rep(i, hl.size()) {
    rep(j, wl.size()) {
      clog << *(lower_bound(all(data), mp(hl[i], wl[j]))) << endl;
      if (*lower_bound(all(data), mp(hl[i], wl[j])) != mp(hl[i], wl[j])) {
        ans++;
        cout << ans << endl;
        return 0;
      }
    }
  }
  cout << ans << endl;
}