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

#pragma region head
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
template <class T> using vv = vector<vector<T>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
  preprocess() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} ____;
#pragma endregion head

#pragma region library
#pragma endregion library

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<pi> bomb;
  vi cntx(h), cnty(w);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    bomb.emplace_back(a, b);
    cntx[a]++;
    cnty[b]++;
  }
  int mxx = *max_element(all(cntx));
  int mxy = *max_element(all(cnty));
  vi xidx(h, -1), yidx(w, -1);
  int xcnt = 0, ycnt = 0;
  rep(i, h) {
    if (mxx == cntx[i])
      xidx[i] = xcnt++;
  }
  rep(i, w) {
    if (mxy == cnty[i])
      yidx[i] = ycnt++;
  }
  vi cnt(xcnt);
  rep(i, m) {
    int a, b;
    tie(a, b) = bomb[i];
    if (xidx[a] != -1 && yidx[b] != -1) {
      cnt[xidx[a]]++;
    }
  }
  int minus = 1;
  rep(i, xcnt) {
    if (cnt[i] != ycnt)
      minus = 0;
  }
  cout << mxx + mxy - minus << '\\n';
}


Error: TLE

Fixed submission:

#pragma region head
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
template <class T> using vv = vector<vector<T>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
  preprocess() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} ____;
#pragma endregion head

#pragma region library
#pragma endregion library

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<pi> bomb;
  vi cntx(h), cnty(w);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    bomb.emplace_back(a, b);
    cntx[a]++;
    cnty[b]++;
  }
  int mxx = *max_element(all(cntx));
  int mxy = *max_element(all(cnty));
  set<int> xidx, yidx;
  rep(i, h) {
    if (mxx == cntx[i])
      xidx.emplace(i);
  }
  rep(i, w) {
    if (mxy == cnty[i])
      yidx.emplace(i);
  }
  vi cnt(xidx.size());
  rep(i, m) {
    int a, b;
    tie(a, b) = bomb[i];
    if (xidx.count(a) && yidx.count(b)) {
      int id = distance(xidx.begin(), xidx.lower_bound(a));
      cnt[id]++;
    }
  }
  int minus = 1;
  rep(i, xidx.size()) {
    if (cnt[i] != (int)yidx.size())
      minus = 0;
  }
  cout << mxx + mxy - minus << '\\n';
}