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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repA(i, a, n) for (ll i = a; i < (n); ++i)
#define repD(i, a, n) for (ll i = a; i > (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (long long)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
// #define mp make_pair
#define pb push_back
void __print(int x) { cout << x; }
void __print(long x) { cout << x; }
void __print(long long x) { cout << x; }
void __print(unsigned x) { cout << x; }
void __print(unsigned long x) { cout << x; }
void __print(unsigned long long x) { cout << x; }
void __print(float x) { cout << x; }
void __print(double x) { cout << x; }
void __print(long double x) { cout << x; }
void __print(char x) { cout << '\\'' << x << '\\''; }
void __print(const char *x) { cout << '\\"' << x << '\\"'; }
void __print(const string &x) { cout << '\\"' << x << '\\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cout << '{';
  __print(x.first);
  cout << ',';
  __print(x.second);
  cout << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cout << '{';
  for (auto &i : x)
    cout << (f++ ? "," : ""), __print(i);
  cout << "}";
}
void _print() { cout << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cout << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define dbg(x...)                                                              \\
  cout << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define dbg(x...)
#endif
const long double PI = 3.141592653589793238462643383;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long int> vll;
typedef vector<double> vd;
typedef vector<pii> vii;
long long Ceil(long long a, long long b) {
  if (a % b == 0)
    return a / b;
  else
    return (a / b) + 1;
}
ll p = 1000000007;
float E = 0.00000000000001;
ll power(ll x, ll y) {
  if (y == 0) {
    return 1;
  }
  if (y % 2 == 0) {
    ll q = power(x, y / 2);
    q = q % p;
    return (q * q) % p;
  }
  ll q = power(x, y / 2);
  q = q % p;
  return ((x % p) * ((q * q) % p)) % p;
}
bool isPowerOfTwo(long long x) {
  /* First x in the below expression is for the case when x is 0 */
  return x && (!(x & (x - 1)));
}

long long int gcd(long long int a, long long int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
long long int mod(long long int a, long long int b) {
  long long int ret = a % b;
  return ret >= 0 ? ret : ret + b;
}

int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--) {
    int h, w, m;
    cin >> h >> w >> m;
    set<pair<int, int>> s;
    vector<int> row(h, 0), col(w, 0);
    unordered_set<int> rs, cs;
    for (int i = 0; i < m; i++) {
      pair<int, int> p;
      cin >> p.first >> p.second;
      p.first--;
      p.second--;
      row[p.first]++;
      col[p.second]++;
      s.insert(p);
    }
    int mx_rows = *max_element(row.begin(), row.end()),
        mx_cols = *max_element(col.begin(), col.end());
    // int cntr = 0, cntc = 0;
    for (int i = 0; i < h; i++) {
      if (row[i] == mx_rows)
        rs.insert(i);
    }
    for (int j = 0; j < w; j++) {
      if (col[j] == mx_cols)
        cs.insert(j);
    }
    // long long total = mx_rows*mx_cols;
    long long total = (long long)(rs.size()) * (long long)(cs.size());
    long long cnt = 0;
    for (auto p : s) {
      if (rs.find(p.first) != rs.end() && cs.find(p.second) != cs.end())
        cnt++;
    }
    if (cnt < total)
      cout << mx_rows + mx_cols << endl;
    else
      cout << mx_rows + mx_cols - 1 << endl;
  }
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repA(i, a, n) for (ll i = a; i < (n); ++i)
#define repD(i, a, n) for (ll i = a; i > (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (long long)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
// #define mp make_pair
#define pb push_back
void __print(int x) { cout << x; }
void __print(long x) { cout << x; }
void __print(long long x) { cout << x; }
void __print(unsigned x) { cout << x; }
void __print(unsigned long x) { cout << x; }
void __print(unsigned long long x) { cout << x; }
void __print(float x) { cout << x; }
void __print(double x) { cout << x; }
void __print(long double x) { cout << x; }
void __print(char x) { cout << '\\'' << x << '\\''; }
void __print(const char *x) { cout << '\\"' << x << '\\"'; }
void __print(const string &x) { cout << '\\"' << x << '\\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cout << '{';
  __print(x.first);
  cout << ',';
  __print(x.second);
  cout << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cout << '{';
  for (auto &i : x)
    cout << (f++ ? "," : ""), __print(i);
  cout << "}";
}
void _print() { cout << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cout << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define dbg(x...)                                                              \\
  cout << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define dbg(x...)
#endif
const long double PI = 3.141592653589793238462643383;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long int> vll;
typedef vector<double> vd;
typedef vector<pii> vii;
long long Ceil(long long a, long long b) {
  if (a % b == 0)
    return a / b;
  else
    return (a / b) + 1;
}
ll p = 1000000007;
float E = 0.00000000000001;
ll power(ll x, ll y) {
  if (y == 0) {
    return 1;
  }
  if (y % 2 == 0) {
    ll q = power(x, y / 2);
    q = q % p;
    return (q * q) % p;
  }
  ll q = power(x, y / 2);
  q = q % p;
  return ((x % p) * ((q * q) % p)) % p;
}
bool isPowerOfTwo(long long x) {
  /* First x in the below expression is for the case when x is 0 */
  return x && (!(x & (x - 1)));
}

long long int gcd(long long int a, long long int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
long long int mod(long long int a, long long int b) {
  long long int ret = a % b;
  return ret >= 0 ? ret : ret + b;
}
bool proper(int i, int j, int n, int m) {
  return i >= 0 && j >= 0 && i < n && j < m;
}
void dfs_visit(int i, int j, vector<string> &vec, vector<vector<int>> &visited,
               vector<vector<int>> &ids, int id) {
  if (!proper(i, j, vec.size(), vec[0].size()) || vec[i][j] == '#' ||
      visited[i][j])
    return;
  visited[i][j] = 1;
  ids[i][j] = id;
  dfs_visit(i + 1, j, vec, visited, ids, id);
  dfs_visit(i - 1, j, vec, visited, ids, id);
  dfs_visit(i, j + 1, vec, visited, ids, id);
  dfs_visit(i, j - 1, vec, visited, ids, id);
}
int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--) {
    int h, w, m;
    cin >> h >> w >> m;
    vector<vector<int>> vec(h, vector<int>(w, 0));
    for (int i = 0; i < m; i++) {
      pair<int, int> p;
      cin >> p.first >> p.second;
      vec[p.first - 1][p.second - 1] = 1;
    }
    vector<int> row(h, 0), col(w, 0);
    for (int i = 0; i < h; i++) {
      int count = 0;
      for (int j = 0; j < w; j++) {
        if (vec[i][j])
          count++;
      }
      row[i] = count;
    }
    for (int j = 0; j < w; j++) {
      int count = 0;
      for (int i = 0; i < h; i++) {
        if (vec[i][j])
          count++;
      }
      col[j] = count;
    }
    int ans = 0;
    dbg(row);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (!vec[i][j])
          ans = max(ans, row[i] + col[j]);
        else
          ans = max(ans, row[i] + col[j] - 1);
      }
    }
    cout << ans;
  }
  return 0;
}