Problem p02580 (C++)

Buggy submission (status: Runtime Error):
/// mAAria...
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(v) v.begin(), v.end()
#define allr(v) v.begin(), v.end()
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define pii pair<int, int>
#define pi 3.1415926535897932384626433832
#define mod 1000000007
// #define mod 998244353
#define inf 1000000000
#define pr1(a) cout << a << endl
#define pr2(a, b) cout << a << " " << b << endl
#define pr3(a, b, c) cout << a << " " << b << " " << c << endl

const long double PI = acos(-1);

int powm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

bool power_of_two(int n) { return ((n & (n - 1)) == 0); }

void solve() {

  int r, c, ans = 0;
  cin >> r >> c;
  int m;
  cin >> m;

  vector<int> adj[r + 1];
  vector<int> row(r + 1, 0);
  vector<int> col(c + 1, 0);

  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    row[x]++;
    col[y]++;
  }

  set<pii> col_cnt;

  for (int j = 1; j <= c; j++) {
    col_cnt.insert({col[j], j});
  }

  for (int i = 1; i <= r; i++) {

    for (auto j : adj[i]) {
      if (col_cnt.find({col[j], j}) != col_cnt.end())
        col_cnt.erase(col_cnt.find({col[j], j}));

      col[j]--;
      col_cnt.insert({col[j], j});
    }

    auto it = col_cnt.end();
    if (it != col_cnt.begin())
      it--;

    ans = max(ans, row[i] + (*it).ff);

    for (auto j : adj[i]) {
      col_cnt.erase(col_cnt.find({col[j], j}));
      col[j]++;
      col_cnt.insert({col[j], j});
    }
  }
  cout << ans << endl;
}

signed main() {
  IOS;
  int t = 1;
  cin >> t;

  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}


Fixed submission:
/// mAAria...
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(v) v.begin(), v.end()
#define allr(v) v.begin(), v.end()
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define pii pair<int, int>
#define pi 3.1415926535897932384626433832
#define mod 1000000007
// #define mod 998244353
#define inf 1000000000
#define pr1(a) cout << a << endl
#define pr2(a, b) cout << a << " " << b << endl
#define pr3(a, b, c) cout << a << " " << b << " " << c << endl

const long double PI = acos(-1);

int powm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

bool power_of_two(int n) { return ((n & (n - 1)) == 0); }

void solve() {

  int r, c, ans = 0;
  cin >> r >> c;
  int m;
  cin >> m;

  vector<int> adj[r + 1];
  vector<int> row(r + 1, 0);
  vector<int> col(c + 1, 0);

  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    row[x]++;
    col[y]++;
  }

  set<pii> col_cnt;

  for (int j = 1; j <= c; j++) {
    col_cnt.insert({col[j], j});
  }

  for (int i = 1; i <= r; i++) {

    for (auto j : adj[i]) {
      if (col_cnt.find({col[j], j}) != col_cnt.end())
        col_cnt.erase(col_cnt.find({col[j], j}));

      col[j]--;
      col_cnt.insert({col[j], j});
    }

    auto it = col_cnt.end();
    if (it != col_cnt.begin())
      it--;

    ans = max(ans, row[i] + (*it).ff);

    for (auto j : adj[i]) {
      col_cnt.erase(col_cnt.find({col[j], j}));
      col[j]++;
      col_cnt.insert({col[j], j});
    }
  }
  cout << ans << endl;
}

signed main() {
  IOS;
  int t = 1;
  // cin >> t;

  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}