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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
using ll = long long;
const ll mod = (ll)1e9 + 7;

struct sec_ord {
  bool operator()(const pair<int, int> &x, const pair<int, int> &y) const {
    return x.second < y.second;
  }
};

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  // vector<vector<bool>> board(h, vector<bool>(w));
  set<pair<int, int>> pos;
  // rep(i, h) rep(j, w) board[i][j] = false;
  map<int, int> xnum, ynum;
  rep(i, m) {
    int y, x;
    cin >> y >> x;
    y--, x--;
    pos.insert(make_pair(y, x));
    xnum[x]++;
    ynum[y]++;
  }
  priority_queue<pair<int, int>, vector<pair<int, int>>, sec_ord> quex, quey;
  for (auto p : xnum)
    quex.push(p);
  for (auto p : ynum)
    quey.push(p);

  vector<pair<int, int>> xtop, ytop;
  int xmx = quex.top().second, ymx = quey.top().second;
  while (!quex.empty()) {
    if (quex.top().second < xmx)
      break;
    xtop.push_back(quex.top());
    quex.pop();
  }
  while (!quey.empty()) {
    if (quey.top().second < ymx)
      break;
    ytop.push_back(quey.top());
    quey.pop();
  }
  // for (auto p : xtop) cout << "x " << p.first << " : " << p.second << endl;
  // for (auto p : ytop) cout << "y " << p.first << " : " << p.second << endl;

  bool minus = true;
  rep(i, ytop.size()) rep(j, xtop.size()) {
    if (pos.find(make_pair(ytop[i].first, xtop[j].first)) == pos.end()) {
      // cout << i << " " << j << "is empty." << endl;
      minus = false;
      break;
    }
  }
  int mxnum = xmx + ymx - (minus ? 1 : 0);
  cout << mxnum << endl;
  // cout << xmx << " + " << ymx << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, n) for (int i = (a); i < (n); ++i)
using namespace std;
using ll = long long;
using uint = unsigned int;
const ll mod = (ll)1e9 + 7;

struct sec_ord {
  bool operator()(const pair<int, int> &x, const pair<int, int> &y) const {
    return x.second < y.second;
  }
};

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<vector<bool>> board(h, vector<bool>(w));
  rep(i, h) rep(j, w) board[i][j] = false;
  map<int, int> xnum, ynum;
  rep(i, m) {
    int y, x;
    cin >> y >> x;
    y--, x--;
    board[y][x] = true;
    xnum[x]++;
    ynum[y]++;
  }
  priority_queue<pair<int, int>, vector<pair<int, int>>, sec_ord> quex, quey;
  for (auto p : xnum)
    quex.push(p);
  for (auto p : ynum)
    quey.push(p);

  vector<pair<int, int>> xtop, ytop;
  int xmx = quex.top().second, ymx = quey.top().second;
  while (!quex.empty()) {
    if (quex.top().second < xmx)
      break;
    xtop.push_back(quex.top());
    quex.pop();
  }
  while (!quey.empty()) {
    if (quey.top().second < ymx)
      break;
    ytop.push_back(quey.top());
    quey.pop();
  }
  // for (auto p : xtop) cout << "x " << p.first << " : " << p.second << endl;
  // for (auto p : ytop) cout << "y " << p.first << " : " << p.second << endl;

  bool minus = true;
  rep(i, (int)ytop.size()) rep(j, (int)xtop.size()) {
    if (!board[ytop[i].first][xtop[j].first]) {
      // cout << i << " " << j << "is empty." << endl;
      minus = false;
      break;
    }
  }
  int mxnum = xmx + ymx - (minus ? 1 : 0);
  cout << mxnum << endl;
  // cout << xmx << " + " << ymx << endl;
  return 0;
}