Problem p02580 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define lli long long int
#define double long double
#define all(x) x.begin(), x.end()
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define siz(x) x.size()
#define prs(x, y) x.find(y) != x.end()
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define forn(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
const int INF = 1 << 29;
typedef long long ll;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n >> b) & 1; }
inline void set_bit(int &n, int b) { n |= two(b); }
inline void unset_bit(int &n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) {
  int res = 0;
  while (n && ++res)
    n -= n & (-n);
  return res;
}
inline int level(int n) {
  int ans = 0;
  while (n % 2 == 0) {
    n /= 2;
    ans++;
  }
  return ans;
}
map<int, int> col, row;
vector<int> vec[300005];
int main() {
  fast_io // REMOVE FOR interactive problems
      int h,
      w, m;
  cin >> h >> w >> m;
  while (m--) {
    int x, y;
    cin >> x >> y;
    col[x]++;
    row[y]++;
    vec[x].push_back(y);
  }
  multiset<int> mult, st;
  for (auto x : row)
    mult.insert(x.second);
  int ansa = 0;
  for (auto x : col) {
    int cl = x.first;
    int siz = x.second;
    for (auto k : vec[cl]) {
      int kr = row[k];
      mult.erase(mult.find(kr));
      st.insert(kr);
    }
    int ans = max(siz + (*st.rbegin()) - 1, siz + (*mult.rbegin()));
    ansa = max(ansa, ans);
    while (!st.empty()) {
      mult.insert(*st.begin());
      st.erase(st.begin());
    }
  }
  cout << ansa;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define lli long long int
#define double long double
#define all(x) x.begin(), x.end()
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define siz(x) x.size()
#define prs(x, y) x.find(y) != x.end()
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define forn(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
const int INF = 1 << 29;
typedef long long ll;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n >> b) & 1; }
inline void set_bit(int &n, int b) { n |= two(b); }
inline void unset_bit(int &n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) {
  int res = 0;
  while (n && ++res)
    n -= n & (-n);
  return res;
}
inline int level(int n) {
  int ans = 0;
  while (n % 2 == 0) {
    n /= 2;
    ans++;
  }
  return ans;
}
map<int, int> col, row;
vector<int> vec[300005];
int main() {
  fast_io // REMOVE FOR interactive problems
      int h,
      w, m;
  cin >> h >> w >> m;
  while (m--) {
    int x, y;
    cin >> x >> y;
    col[x]++;
    row[y]++;
    vec[x].push_back(y);
  }
  multiset<int> mult, st;
  for (auto x : row)
    mult.insert(x.second);
  int ansa = 0;
  for (auto x : col) {
    int cl = x.first;
    int siz = x.second;
    for (auto k : vec[cl]) {
      int kr = row[k];
      mult.erase(mult.find(kr));
      st.insert(kr);
    }
    if (!st.empty())
      ansa = max(ansa, siz + (*st.rbegin()) - 1);
    if (!mult.empty())
      ansa = max(ansa, siz + (*mult.rbegin()));
    while (!st.empty()) {
      mult.insert(*st.begin());
      st.erase(st.begin());
    }
  }
  cout << ansa;
}