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

#include <bits/stdc++.h>
using namespace std;
#define ft first
#define sd second
#define ll long long
#define pb push_back
#define mkp make_pair
#define pll pair<ll, ll>
#define vll vector<int>
#define mll map<ll, ll>
#define pqg priority_queue<ll>
#define pqs priority_queue<ll, vll, greater<ll>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define MAXN 10000001
void online_judge() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
}

void ISO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

set<pair<int, int>> s;
int main() {

  ISO();
  online_judge();

  int h, w, m;
  cin >> h >> w >> m;
  vector<int> x(w, 0), y(h, 0);
  map<pair<int, int>, int> bomb;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    x[b]++;
    y[a]++;
    bomb[make_pair(b, a)]++;
  }
  int maxX = 0, maxY = 0;
  for (int i = 0; i < w; i++) {
    maxX = max(maxX, x[i]);
  }
  for (int i = 0; i < h; i++) {
    maxY = max(maxY, y[i]);
  }
  vector<int> maxXs, maxYs;
  for (int i = 0; i < w; i++) {
    if (x[i] == maxX) {
      maxXs.push_back(i);
    }
  }
  for (int i = 0; i < h; i++) {
    if (y[i] == maxY) {
      maxYs.push_back(i);
    }
  }
  for (int xx : maxXs) {
    for (int yy : maxYs) {
      if (!bomb.count(make_pair(xx, yy))) {
        cout << maxX + maxY << endl;
        return 0;
      }
    }
  }
  cout << maxX + maxY - 1 << endl;

  // cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl;
  return 0;
}

Error: -6

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ft first
#define sd second
#define ll long long
#define pb push_back
#define mkp make_pair
#define pll pair<ll, ll>
#define vll vector<int>
#define mll map<ll, ll>
#define pqg priority_queue<ll>
#define pqs priority_queue<ll, vll, greater<ll>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define MAXN 10000001
void online_judge() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
}

void ISO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

set<pair<int, int>> s;
int main() {

  ISO();
  online_judge();

  int h, w;
  cin >> h >> w;
  int m;
  cin >> m;
  vector<pair<int, int>> v;

  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    v.push_back({x, y});
    s.insert({x, y});
  }
  vector<int> fr(h, 0);
  vector<int> fc(w, 0);
  int fr_max = INT_MIN;
  int fc_max = INT_MIN;
  for (auto x : v) {
    fr[x.first]++;
    fc[x.second]++;
    fr_max = max(fr_max, fr[x.first]);
    fc_max = max(fc_max, fc[x.second]);
  }
  // sort(fr.begin(),fr.end());
  // sort(fc.begin(),fc.end());
  // cerr << fr_max <<" "<< fc_max <<"\\n";
  vector<int> x_max;
  vector<int> y_max;
  int ans = fr_max + fc_max - 1;
  // cerr << ans <<"\\n";
  for (int i = 0; i < w; ++i) {
    if (fr[i] == fr_max) {
      x_max.push_back(i);
    }
  }
  for (int i = 0; i < h; ++i) {
    if (fc[i] == fc_max) {
      y_max.push_back(i);
    }
  }
  bool flag = false;
  for (auto x : x_max) {
    for (auto y : y_max) {

      if (s.find({x, y}) == s.end()) {
        // cerr << x <<" "<< y <<"\\n";
        ans++;
        flag = true;
        break;
      }
    }
    if (flag)
      break;
  }
  cout << ans << "\\n";

  cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl;
  return 0;
}