Problem p02580 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return 0;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> h(M), w(M);
  rep(i, 0, M) { cin >> h[i] >> w[i]; }
  // yoko
  vector<int> yoko;
  rep(i, 0, M) { yoko.push_back(w[i]); }
  sort(all(yoko));
  yoko.erase(unique(all(yoko)), yoko.end());
  int yoko_size = (int)yoko.size();
  vector<int> yoko_cnt(yoko_size, 0);
  rep(i, 0, M) {
    ++yoko_cnt[(int)(lower_bound(all(yoko), w[i]) - yoko.begin())];
  }
  // tate
  vector<int> tate;
  rep(i, 0, M) { tate.push_back(h[i]); }
  sort(all(tate));
  tate.erase(unique(all(tate)), tate.end());
  int tate_size = (int)tate.size();
  vector<int> tate_cnt(tate_size, 0);
  rep(i, 0, M) {
    ++tate_cnt[(int)(lower_bound(all(tate), h[i]) - tate.begin())];
  }
  vector<vector<int>> yoko_pos(tate_size);
  rep(i, 0, M) {
    int h_idx = (int)(lower_bound(all(tate), h[i]) - tate.begin());
    yoko_pos[h_idx].push_back(
        (int)(lower_bound(all(yoko), w[i]) - yoko.begin()));
  }
  int ans = 0;
  multiset<int> others, same;
  rep(i, 0, yoko_size) { others.insert(yoko_cnt[i]); }
  rep(i, 0, tate_size) {
    // 同じ行のものを分ける
    for (auto &w_idx : yoko_pos[i]) {
      others.erase(others.find(yoko_cnt[w_idx]));
      same.insert(yoko_cnt[w_idx] - 1);
    }
    chmax(ans, tate_cnt[i] + max(*others.rbegin(), *same.rbegin()));
    for (auto &w_idx : yoko_pos[i]) {
      same.erase(same.find(yoko_cnt[w_idx] - 1));
      others.insert(yoko_cnt[w_idx]);
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return 0;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> h(M), w(M);
  rep(i, 0, M) { cin >> h[i] >> w[i]; }
  // yoko
  vector<int> yoko;
  rep(i, 0, M) { yoko.push_back(w[i]); }
  sort(all(yoko));
  yoko.erase(unique(all(yoko)), yoko.end());
  int yoko_size = (int)yoko.size();
  vector<int> yoko_cnt(yoko_size, 0);
  rep(i, 0, M) {
    ++yoko_cnt[(int)(lower_bound(all(yoko), w[i]) - yoko.begin())];
  }
  // tate
  vector<int> tate;
  rep(i, 0, M) { tate.push_back(h[i]); }
  sort(all(tate));
  tate.erase(unique(all(tate)), tate.end());
  int tate_size = (int)tate.size();
  vector<int> tate_cnt(tate_size, 0);
  rep(i, 0, M) {
    ++tate_cnt[(int)(lower_bound(all(tate), h[i]) - tate.begin())];
  }
  vector<vector<int>> yoko_pos(tate_size);
  rep(i, 0, M) {
    int h_idx = (int)(lower_bound(all(tate), h[i]) - tate.begin());
    yoko_pos[h_idx].push_back(
        (int)(lower_bound(all(yoko), w[i]) - yoko.begin()));
  }
  int ans = 0;
  multiset<int> others, same;
  rep(i, 0, yoko_size) { others.insert(yoko_cnt[i]); }
  rep(i, 0, tate_size) {
    // 同じ行のものを分ける
    for (auto &w_idx : yoko_pos[i]) {
      others.erase(others.find(yoko_cnt[w_idx]));
      same.insert(yoko_cnt[w_idx] - 1);
    }
    if (!others.empty()) {
      chmax(ans, tate_cnt[i] + *others.rbegin());
    }
    if (!same.empty()) {
      chmax(ans, tate_cnt[i] + *same.rbegin());
    }
    for (auto &w_idx : yoko_pos[i]) {
      same.erase(same.find(yoko_cnt[w_idx] - 1));
      others.insert(yoko_cnt[w_idx]);
    }
  }
  cout << ans << endl;
  return 0;
}