Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); ++i)
#define SIZE 200005
#define INF 1000000005LL
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
vector<int> x_dire = {0, 0, -1, 1};
vector<int> y_dire = {-1, 1, 0, 0};
int main() {

  ll h, w, m;
  cin >> h >> w >> m;
  map<ll, ll> datax;
  map<ll, ll> datay;
  if (m == 0) {
    cout << 0 << endl;
    return 0;
  }
  vector<set<int>> cntx(h);
  // vector<set<int>> cnty(h);
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    datax[a]++;
    datay[b]++;
    cntx[a].insert(b);
  }
  ll max_x = -1;
  ll max_y = -1;
  for (auto p = datax.begin(); p != datax.end(); p++) {
    max_x = max(max_x, p->second);
  }
  for (auto p = datay.begin(); p != datay.end(); p++) {
    max_y = max(max_y, p->second);
  }
  // cout<<max_x<<max_y<<endl;
  queue<ll> kouho_x;
  queue<ll> kouho_y;
  for (auto p = datax.begin(); p != datax.end(); p++) {
    if (p->second == max_x) {
      kouho_x.push(p->first);
    }
  }
  for (auto p = datay.begin(); p != datay.end(); p++) {
    if (p->second == max_y) {
      kouho_y.push(p->first);
    }
  }
  ll ans = -1;
  while (!kouho_x.empty()) {
    ll x = kouho_x.front();
    ll y;
    queue<ll> next = kouho_y;
    while (!kouho_y.empty()) {
      y = kouho_y.front();
      if (!cntx[x].count(y)) {
        ans = max_x + max_y;
        cout << ans << endl;
        return 0;
      }
      kouho_y.pop();
    }
    kouho_y = next;
    kouho_x.pop();
  }
  cout << max_x + max_y - 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); ++i)
#define SIZE 200005
#define INF 1000000005LL
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
vector<int> x_dire = {0, 0, -1, 1};
vector<int> y_dire = {-1, 1, 0, 0};
int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  map<ll, ll> datax;
  map<ll, ll> datay;
  // vector<ll> datax(300000);
  // vector<ll> datay(300000);
  vector<set<int>> cntx(w);
  // vector<set<int>> cnty(h);
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    datax[a]++;
    datay[b]++;
    cntx[a].insert(b);
    // cntx[b].insert(a);
  }
  ll max_x = -1;
  ll max_y = -1;
  for (auto p = datax.begin(); p != datax.end(); p++) {
    max_x = max(max_x, p->second);
  }
  for (auto p = datay.begin(); p != datay.end(); p++) {
    max_y = max(max_y, p->second);
  }
  // cout<<max_x<<max_y<<endl;
  queue<ll> kouho_x;
  queue<ll> kouho_y;
  for (auto p = datax.begin(); p != datax.end(); p++) {
    if (p->second == max_x) {
      kouho_x.push(p->first);
    }
  }
  for (auto p = datay.begin(); p != datay.end(); p++) {
    if (p->second == max_y) {
      kouho_y.push(p->first);
    }
  }
  ll ans = -1;
  while (!kouho_x.empty()) {
    ll x = kouho_x.front();
    ll y;
    while (!kouho_y.empty()) {
      y = kouho_y.front();
      if (!cntx[x].count(y)) {
        ans = max_x + max_y;
        cout << ans << endl;
        return 0;
      }
      kouho_y.pop();
    }
    kouho_x.pop();
  }
  cout << max_x + max_y - 1 << endl;
}