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

#include <bits/stdc++.h>
using namespace std;
#define pi 2 * asin(1.0)
#define MAX 10000000
#define MOD 1000000007

vector<long> fac(MAX);
vector<long> finv(MAX);
vector<long> inv(MAX);

// template functions
/////////////////////////
void Cominit() {
  fac.at(0) = fac.at(1) = 1;
  finv.at(0) = finv.at(1) = 1;
  inv.at(1) = 1;
  for (long i = 2; i < MAX; i++) {
    fac.at(i) = (fac.at(i - 1) * i) % MOD;
    inv.at(i) = MOD - inv.at(MOD % i) * (MOD / i) % MOD;
    finv.at(i) = finv.at(i - 1) * inv.at(i) % MOD;
  }
}

long facComb(long N, long M) { // nCm%
  if (N < M)
    return 0;
  if (N < 0 || M < 0)
    return 0;
  return fac.at(N) * (finv.at(M) * finv.at(N - M) % MOD) % MOD;
}

long Comb(long N, long M) { // nCm
  long ans = 1;
  for (long i = N; i > N - M; i--) {
    ans = ans * i;
  }
  for (long i = M; i > 0; i--) {
    ans = ans / i;
  }
  return ans;
}

long Perm(long N, long M) { // nPm
  long ans = 1;
  for (long i = N; i > N - M; i--) {
    ans = ans * i;
  }
  return ans;
}

long GCM(long A, long B) {
  long W, r;
  if (A < B) {

    W = B;
    B = A;
    A = W;
  }
  while (A % B != 0) {
    r = A % B;
    A = B;
    B = r;
  }
  return B;
}
long LCM(long A, long B) { return (A * B) / GCM(A, B); }

long upDiv(long a, long b) { return (a + b - 1) / b; }

/////////////////////////

// Answer

int main() {

  typedef pair<long, long> P;
  long H, W, M, ans, flag = 0;
  cin >> H >> W >> M;
  vector<P> h(H + 1);
  vector<P> w(W + 1);
  vector<P> m(M);

  // vector<vector<long>> A(H+1,vector<long>(W+1,0));

  for (long i = 0; i < H + 1; i++) {
    h.at(i).first = 0;
    h.at(i).second = i;
  }
  for (long i = 0; i < W + 1; i++) {
    w.at(i).first = 0;
    w.at(i).second = i;
  }

  for (long i = 0; i < M; i++) {
    cin >> m.at(i).first >> m.at(i).second;
    h.at(m.at(i).first).first++;
    w.at(m.at(i).second).first++;
    // A.at(m.at(i).first).at(m.at(i).second)=1;
  }
  sort(h.begin(), h.end());
  reverse(h.begin(), h.end());
  sort(w.begin(), w.end());
  reverse(w.begin(), w.end());
  sort(m.begin(), m.end());

  long a = 0, b = 0;
  while (a < H + 1 && h.at(a).first == h.at(0).first) {
    a++;
    if (a == H + 1) {
      break;
    }
  }
  while (b < W + 1 && w.at(b).first == w.at(0).first) {
    b++;
    if (b == W + 1) {
      break;
    }
  }

  if (a * b > M) {
    ans = h.at(0).first + w.at(0).first;
  } else {
    vector<P> hw(a * b);
    for (long i = 0; i < a; i++) {
      for (long j = 0; j < b; j++) {
        hw.at(i * b + j).first = h.at(i).second;
        hw.at(i * b + j).second = w.at(j).second;
      }
    }
    sort(hw.begin(), hw.end());
    sort(m.begin(), m.end());
    long t = 0;
    for (long i = 0; i < M; i++) {
      if ((m.at(i).first == hw.at(t).first) &&
          (m.at(i).second == hw.at(t).second)) {
        t++;
        if (t == a * b) {
          break;
        }
      }
    }

    if (t < a * b) {
      ans = h.at(0).first + w.at(0).first;
    } else {
      ans = h.at(0).first + w.at(0).first - 1;
    }
  }
  cout << ans << endl;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pi 2 * asin(1.0)
#define MAX 10000000
#define MOD 1000000007

vector<long> fac(MAX);
vector<long> finv(MAX);
vector<long> inv(MAX);

// template functions
/////////////////////////
void Cominit() {
  fac.at(0) = fac.at(1) = 1;
  finv.at(0) = finv.at(1) = 1;
  inv.at(1) = 1;
  for (long i = 2; i < MAX; i++) {
    fac.at(i) = (fac.at(i - 1) * i) % MOD;
    inv.at(i) = MOD - inv.at(MOD % i) * (MOD / i) % MOD;
    finv.at(i) = finv.at(i - 1) * inv.at(i) % MOD;
  }
}

long facComb(long N, long M) { // nCm%
  if (N < M)
    return 0;
  if (N < 0 || M < 0)
    return 0;
  return fac.at(N) * (finv.at(M) * finv.at(N - M) % MOD) % MOD;
}

long Comb(long N, long M) { // nCm
  long ans = 1;
  for (long i = N; i > N - M; i--) {
    ans = ans * i;
  }
  for (long i = M; i > 0; i--) {
    ans = ans / i;
  }
  return ans;
}

long Perm(long N, long M) { // nPm
  long ans = 1;
  for (long i = N; i > N - M; i--) {
    ans = ans * i;
  }
  return ans;
}

long GCM(long A, long B) {
  long W, r;
  if (A < B) {

    W = B;
    B = A;
    A = W;
  }
  while (A % B != 0) {
    r = A % B;
    A = B;
    B = r;
  }
  return B;
}
long LCM(long A, long B) { return (A * B) / GCM(A, B); }

long upDiv(long a, long b) { return (a + b - 1) / b; }

/////////////////////////

// Answer

int main() {

  typedef pair<long, long> P;
  long H, W, M, ans, flag = 0;
  cin >> H >> W >> M;
  vector<P> h(H + 1);
  vector<P> w(W + 1);
  vector<P> m(M);

  // vector<vector<long>> A(H+1,vector<long>(W+1,0));

  for (long i = 0; i < H + 1; i++) {
    h.at(i).first = 0;
    h.at(i).second = i;
  }
  for (long i = 0; i < W + 1; i++) {
    w.at(i).first = 0;
    w.at(i).second = i;
  }

  for (long i = 0; i < M; i++) {
    cin >> m.at(i).first >> m.at(i).second;
    h.at(m.at(i).first).first++;
    w.at(m.at(i).second).first++;
    // A.at(m.at(i).first).at(m.at(i).second)=1;
  }
  sort(h.begin(), h.end());
  reverse(h.begin(), h.end());
  sort(w.begin(), w.end());
  reverse(w.begin(), w.end());
  sort(m.begin(), m.end());

  long a = 0, b = 0;
  while (a < H + 1 && h.at(a).first == h.at(0).first) {
    a++;
    if (a == H + 1) {
      break;
    }
  }
  while (b < W + 1 && w.at(b).first == w.at(0).first) {
    b++;
    if (b == W + 1) {
      break;
    }
  }

  if (a * b > M) {
    ans = h.at(0).first + w.at(0).first;
  } else {
    vector<P> hw(a * b);
    for (long i = 0; i < a; i++) {
      for (long j = 0; j < b; j++) {
        hw.at(i * b + j).first = h.at(i).second;
        hw.at(i * b + j).second = w.at(j).second;
      }
    }
    sort(hw.begin(), hw.end());
    sort(m.begin(), m.end());
    long t = 0;
    for (long i = 0; i < M; i++) {
      if ((m.at(i).first == hw.at(t).first) &&
          (m.at(i).second == hw.at(t).second)) {

        t++;
      }
    }

    if (t < a * b) {
      ans = h.at(0).first + w.at(0).first;
    } else {
      ans = h.at(0).first + w.at(0).first - 1;
    }
  }
  cout << ans << endl;
}