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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
////////////////////////////////////////////////
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define REPvec(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define REPe(e, mp) for (auto &e : mp)
#define all(x) x.begin(), x.end()
#define MOD 1000000007 // 1e9+7
using namespace std;
using ll = long long int;
using vecint = vector<int>;
using vecll = vector<ll>;
using vec2int = vector<vector<int>>;
using P = pair<int, int>;
////////////////////////////////////////////////
struct Sieve {
  // Eratosthenes sieve in [0,i)
  int n;
  vector<int> mpfac; // minimum prime factor
  vector<int> primes;
  ////////////////////////////////////////////////
  Sieve(int _n) : mpfac(_n) {
    n = _n;
    for (int i = 0; i < n; ++i) {
      mpfac[i] = i;
    }
    for (int i = 2; i * i < n; ++i) {
      if (mpfac[i] != i)
        continue;
      for (int j = i * i; j < n; j += i) {
        if (mpfac[j] == j)
          mpfac[j] = i;
      }
    }
    for (int i = 2; i < n; ++i) {
      if (mpfac[i] == i) {
        primes.push_back(i);
      }
    }
  }
  ////////////////////////////////////////////////
  map<int, int> factorize(int m) {
    map<int, int> res;
    while (m > 1) {
      res[mpfac[m]]++;
      m /= mpfac[m];
    }
    return res;
  }
  ////////////////////////////////////////////////
  long long fm(int m) {
    map<int, int> res;
    while (m > 1) {
      res[mpfac[m]]++;
      m /= mpfac[m];
    }
    long long r = 1LL;
    for (auto itr = res.begin(); itr != res.end(); ++itr) {
      r *= (ll)(itr->second + 1);
    }
    return r;
  }
};
////////////////////////////////////////////////
// std::vector<int> sieve(int n) {
//   std::vector<int> res(n);
//   std::iota(res.begin(), res.end(), 0);
//   for (int i = 2; i * i < n; ++i) {
//     if (res[i] < i) continue;
//     for (int j = i * i; j < n; j += i)
//       if (res[j] == j) res[j] = i;
//   }
//   return res;
// }
// ////////////////////////////////////////////////
// ll fac(int n, const std::vector<int>& min_factor) {
//   map<int, int> res;
//   while (n > 1) {
//     res[min_factor[n]]++;
//     n /= min_factor[n];
//     // 割った後の値についても素因数を知っているので順次求まる
//   }
//   ll r = 1LL;
//   REPvec(itr, res) { r *= (itr->second + 1LL); }
//   return r;
// }
////////////////////////////////////////////////
int main() {
  ////////////////////////////////////////////////
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  ////////////////////////////////////////////////
  int n;
  cin >> n;
  Sieve S(n + 1);
  ll ans = 0;
  REP(i, n + 1) { ans += (ll)i * S.fm(i); }
  cout << ans << endl;
}


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


Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
////////////////////////////////////////////////
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define REPvec(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define REPe(e, mp) for (auto &e : mp)
#define all(x) x.begin(), x.end()
#define MOD 1000000007 // 1e9+7
using namespace std;
using ll = long long int;
using vecint = vector<int>;
using vecll = vector<ll>;
using vec2int = vector<vector<int>>;
using P = pair<int, int>;
////////////////////////////////////////////////
int main() {
  ////////////////////////////////////////////////
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  ////////////////////////////////////////////////
  ll n, m, k;
  cin >> n >> m >> k;
  vecll a(n, 0);
  vecll b(m, 0);
  REPe(e, a) cin >> e;
  REPe(e, b) cin >> e;
  vecll suma(n, 0);
  vecll sumb(m, 0);
  suma[0] = a[0];
  sumb[0] = b[0];
  REP(i, n - 1) suma[i + 1] = suma[i] + a[i + 1];
  REP(i, m - 1) sumb[i + 1] = sumb[i] + b[i + 1];
  auto itr1 = upper_bound(all(sumb), k);
  ll ans = itr1 - sumb.begin();
  REP(i, n) {
    ll tota = suma[i];
    ll totb = k - tota;
    if (totb < 0)
      continue;
    auto itr = upper_bound(all(sumb), totb);
    // if (itr == sumb.begin()) {
    //   ans = max(ans, (ll)i + 1LL);
    //   continue;
    // }
    // cout << tota << " " << totb << " " << *itr << " " << itr - sumb.begin()
    //  << endl;
    ans = max(ans, (ll)i + 1LL + (ll)(itr - sumb.begin()));
    // cout << ans << endl;
  }
  cout << ans << endl;
}