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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const ll LINF = 1LL << 60;
const int MOD = 1'000'000'007;

template <typename T> void print(const T &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      cout << ' ';
    cout << v[i];
  }
  cout << endl;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  set<P> st;
  vector<P> row(h), line(w);
  // indexを付与
  for (int i = 0; i < h; i++) {
    row[i].second = i;
  }
  for (int i = 0; i < w; i++) {
    line[i].second = i;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    st.insert(make_pair(a, b));
    ++row[a].first;
    ++line[b].first;
  }
  sort(row.begin(), row.end());
  reverse(row.begin(), row.end());
  sort(line.begin(), line.end());
  reverse(line.begin(), line.end());
  int row_maxi = row[0].first;
  int line_maxi = line[0].first;
  int maxi = 0;
  for (auto r : row) {
    if (r.first != row_maxi) {
      break;
    }
    for (auto l : line) {
      if (l.first != line_maxi) {
        break;
      }
      if (st.count(make_pair(r.second, l.second))) {
        maxi = max(maxi, r.first + l.first - 1);
      } else {
        maxi = max(maxi, r.first + l.first);
        cout << maxi << endl;
        return 0;
      }
    }
  }
  cout << maxi << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const ll LINF = 1LL << 60;
const int MOD = 1'000'000'007;

template <typename T> void print(const T &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      cout << ' ';
    cout << v[i];
  }
  cout << endl;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<vector<bool>> grid(h, vector<bool>(w, false));
  vector<P> row(h), line(w);
  // indexを付与
  for (int i = 0; i < h; i++) {
    row[i].second = i;
  }
  for (int i = 0; i < w; i++) {
    line[i].second = i;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    grid[a][b] = true;
    ++row[a].first;
    ++line[b].first;
  }
  sort(row.begin(), row.end());
  reverse(row.begin(), row.end());
  sort(line.begin(), line.end());
  reverse(line.begin(), line.end());
  int row_maxi = row[0].first;
  int line_maxi = line[0].first;
  int maxi = 0;
  for (auto r : row) {
    if (r.first != row_maxi) {
      break;
    }
    for (auto l : line) {
      if (l.first != line_maxi) {
        break;
      }
      if (grid[r.second][l.second]) {
        maxi = max(maxi, r.first + l.first - 1);
      } else {
        maxi = max(maxi, r.first + l.first);
        cout << maxi << endl;
        return 0;
      }
    }
  }
  cout << maxi << endl;
  return 0;
}