Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using ll = long long;
using P = pair<ll, ll>;
const long double PI = acos(-1.0L);
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; }

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> hvec(h, 0);
  vector<int> wvec(w, 0);
  vector<vector<int>> checker(h, vector<int>());
  vector<vector<int>> checker2(w, vector<int>());
  for (int i = 0; i < m; ++i) {
    int ch, cw;
    cin >> ch >> cw;
    ch--;
    cw--;
    hvec[ch]++;
    wvec[cw]++;
    checker[ch].emplace_back(cw);
    checker2[cw].emplace_back(ch);
  }

  for (int i = 0; i < h; ++i) {
    sort(checker[i].begin(), checker[i].end());
  }
  for (int i = 0; i < w; ++i) {
    sort(checker2[i].begin(), checker2[i].end());
  }
  int hmax = 0;
  for (int i = 0; i < h; ++i) {
    if (hmax <= hvec[i]) {
      hmax = hvec[i];
    }
  }
  // hの候補
  vector<int> hc;
  for (int i = 0; i < h; ++i) {
    if (hmax == hvec[i])
      hc.emplace_back(i);
  }

  int wmax = 0;
  for (int i = 0; i < w; ++i) {
    if (wmax <= wvec[i]) {
      wmax = wvec[i];
    }
  }

  // wの候補
  vector<int> wc;
  for (int i = 0; i < w; ++i) {
    if (wmax == wvec[i])
      wc.emplace_back(i);
  }

  int ans = 0;
  int hlen = hc.size();
  int wlen = wc.size();
  for (int i = 0; i < hlen; ++i) {
    for (int j = 0; j < wlen; ++j) {
      int cch = hc[i], ccw = wc[j];
      int ccc = hvec[cch] + wvec[ccw];
      int cchhlen = checker[cch].size();
      int ccwwlen = checker2[ccw].size();
      int lc, uc;
      if (cchhlen < ccwwlen) {
        lc = lower_bound(checker[cch].begin(), checker[cch].end(), ccw) -
             checker[cch].begin();
        uc = upper_bound(checker[cch].begin(), checker[cch].end(), ccw) -
             checker[cch].begin();
      } else {
        lc = lower_bound(checker2[ccw].begin(), checker2[ccw].end(), cch) -
             checker2[ccw].begin();
        uc = upper_bound(checker2[ccw].begin(), checker2[ccw].end(), cch) -
             checker2[ccw].begin();
      }
      if (lc != uc)
        ccc--;
      chmax(ans, ccc);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using ll = long long;
using P = pair<ll, ll>;
const long double PI = acos(-1.0L);
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; }

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> hvec(h, 0);
  vector<int> wvec(w, 0);
  vector<vector<int>> checker(h, vector<int>());
  vector<vector<int>> checker2(w, vector<int>());
  for (int i = 0; i < m; ++i) {
    int ch, cw;
    cin >> ch >> cw;
    ch--;
    cw--;
    hvec[ch]++;
    wvec[cw]++;
    checker[ch].emplace_back(cw);
    checker2[cw].emplace_back(ch);
  }

  for (int i = 0; i < h; ++i) {
    sort(checker[i].begin(), checker[i].end());
  }
  for (int i = 0; i < w; ++i) {
    sort(checker2[i].begin(), checker2[i].end());
  }
  int hmax = 0;
  for (int i = 0; i < h; ++i) {
    if (hmax <= hvec[i]) {
      hmax = hvec[i];
    }
  }
  // hの候補
  vector<int> hc;
  for (int i = 0; i < h; ++i) {
    if (hmax == hvec[i])
      hc.emplace_back(i);
  }

  int wmax = 0;
  for (int i = 0; i < w; ++i) {
    if (wmax <= wvec[i]) {
      wmax = wvec[i];
    }
  }

  // wの候補
  vector<int> wc;
  for (int i = 0; i < w; ++i) {
    if (wmax == wvec[i])
      wc.emplace_back(i);
  }

  int ans = 0;
  int hlen = hc.size();
  int wlen = wc.size();
  if (hlen == h && wlen == w) {
    if (h == 1 && w == 1)
      cout << 1 << endl;
    else
      cout << 2 << endl;
    return 0;
  }
  for (int i = 0; i < hlen; ++i) {
    for (int j = 0; j < wlen; ++j) {
      int cch = hc[i], ccw = wc[j];
      int ccc = hvec[cch] + wvec[ccw];
      int cchhlen = checker[cch].size();
      int ccwwlen = checker2[ccw].size();
      int lc, uc;
      if (cchhlen < ccwwlen) {
        lc = lower_bound(checker[cch].begin(), checker[cch].end(), ccw) -
             checker[cch].begin();
        uc = upper_bound(checker[cch].begin(), checker[cch].end(), ccw) -
             checker[cch].begin();
      } else {
        lc = lower_bound(checker2[ccw].begin(), checker2[ccw].end(), cch) -
             checker2[ccw].begin();
        uc = upper_bound(checker2[ccw].begin(), checker2[ccw].end(), cch) -
             checker2[ccw].begin();
      }
      if (lc != uc)
        ccc--;
      chmax(ans, ccc);
    }
  }
  cout << ans << endl;
}