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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;

// →↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};

const double EPS = 1e-9;

struct State {
  int lower, size, rest;
  State(int lower, int size, int rest) : lower(lower), size(size), rest(rest){};
};

int main() {
  vector<int> v;
  vector<int> odd;
  for (int i = 1;; i++) {
    int c = i * (i + 1) * (i + 2) / 6;
    if (c > 1000000)
      break;
    v.push_back(c);
    if (c % 2 == 1)
      odd.push_back(c);
  }
  int M = 1000010;
  vector<int> answer(M, 1 << 30);
  vector<int> answer_odd(M, 1 << 30);
  answer[0] = 0;
  answer_odd[0] = 0;
  for (int i = 0; i < M; i++) {
    int nex;
    for (int j = 0; j < v.size() && (nex = i + v[j]) < M; j++) {
      answer[nex] = min(answer[i] + 1, answer[nex]);
    }
    for (int j = 0; j < odd.size() && (nex = i + odd[j]) < M; j++) {
      answer_odd[nex] = min(answer_odd[i] + 1, answer_odd[nex]);
    }
  }

  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    cout << answer[n] << " " << answer_odd[n] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;

// →↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};

const double EPS = 1e-9;

struct State {
  int lower, size, rest;
  State(int lower, int size, int rest) : lower(lower), size(size), rest(rest){};
};

int main() {
  vector<int> v;
  vector<int> odd;
  for (int i = 1;; i++) {
    int c = i * (i + 1) * (i + 2) / 6;
    if (c > 1000000)
      break;
    v.push_back(c);
    if (c % 2 == 1)
      odd.push_back(c);
  }
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    queue<State> que;
    que.push(State(0, 0, n));
    int ret;
    while (!que.empty()) {
      State s = que.front();
      que.pop();
      if (s.rest == 0) {
        ret = s.size;
        break;
      }
      for (int i = s.lower; v[i] <= s.rest; i++) {
        que.push(State(i, s.size + 1, s.rest - v[i]));
      }
    }
    int oret;
    que = queue<State>();
    que.push(State(0, 0, n));
    while (!que.empty()) {
      State s = que.front();
      que.pop();
      if (s.rest == 0) {
        oret = s.size;
        break;
      }
      for (int i = lower_bound(all(odd), s.rest) - odd.begin(); i >= 0; i--) {
        que.push(State(i, s.size + 1, s.rest - odd[i]));
      }
      // for(int i=s.lower;odd[i] <= s.rest;i++){
    }
    cout << ret << " " << oret << endl;
  }
  return 0;
}