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

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author cherrypi59
 */

#pragma GCC optimize("O2")
#pragma GCC optimize("tree-vectorize")
#pragma GCC target("sse4")

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FFOR(i, a, b) FOR(i, a, b + 1)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FOR(i, 1, n + 1)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define LEN(x) (int)(x).size()
#define DUMP(x) cerr << __LINE__ << ' ' << #x << "=" << (x) << endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair

using namespace std;
using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vector<T>>;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr lint ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; }

class DKnapsackQueriesOnATree {
public:
  static constexpr int MXV = ten(5);
  int N;
  vc<int> V, W, par, depth;
  vvc<int> dp;

  void pre_calc() {
    FOR(j, W[0], MXV + 1) dp[0][j] = V[0];
    RREP(i, min(N - 1, 510)) {
      REP(j, MXV + 1) {
        dp[i][j] = dp[par[i]][j];
        if (j)
          chmax(dp[i][j], dp[i][j - 1]);
        if (j >= W[i])
          chmax(dp[i][j], dp[par[i]][j - W[i]] + V[i]);
      }
    }
  }

  int query(int v, int w) {
    if (depth[v] <= 9) {
      return dp[v][w];
    }
    int cur = v;
    vc<pii> tmp;
    while (depth[cur] > 9) {
      tmp.eb(W[cur], V[cur]);
      cur = par[cur];
    }
    function<int(int, int, int)> dfs = [&](int idx, int curw, int curv) {
      if (idx == LEN(tmp)) {
        return curv + dp[cur][w - curw];
      }
      int res = dfs(idx + 1, curw, curv);
      if (curw + tmp[idx].first <= w)
        chmax(res, dfs(idx + 1, curw + tmp[idx].first, curv + tmp[idx].second));
      return res;
    };
    return dfs(0, 0, 0);
  }

  void solve(std::istream &in, std::ostream &out) {
    ios_base::sync_with_stdio(false);
    in.tie(nullptr), out.tie(nullptr);
#ifdef DEBUG
    int64_t seed = chrono::duration_cast<chrono::milliseconds>(
                       chrono::system_clock::now().time_since_epoch())
                       .count();
    mt19937_64 rnd(seed);
    N = 2000;
#else
    in >> N;
#endif
    V.resize(N);
    W.resize(N);
    par.resize(N);
    depth.resize(N, 1);
    REP(i, N) {
#ifdef DEBUG
      V[i] = rnd() % 100 + 1;
      W[i] = rnd() % 100 + 1;
#else
      in >> V[i] >> W[i];
#endif
      if (i) {
        par[i] = (i - 1) / 2;
        depth[i] = depth[par[i]] + 1;
      }
    }
    dp.resize(511, vc<int>(MXV + 1, 0));
    pre_calc();
    int Q;
    in >> Q;
    while (Q--) {
      int v, l;
      in >> v >> l;
      v--;
      int res = query(v, l);
      out << res << '\\n';
    }
  }
};

int main() {
  DKnapsackQueriesOnATree solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


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


Fixed submission:

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author cherrypi59
 */

#pragma GCC optimize("O2")
#pragma GCC optimize("tree-vectorize")
#pragma GCC target("sse4")

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FFOR(i, a, b) FOR(i, a, b + 1)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FOR(i, 1, n + 1)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define LEN(x) (int)(x).size()
#define DUMP(x) cerr << __LINE__ << ' ' << #x << "=" << (x) << endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair

using namespace std;
using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vector<T>>;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr lint ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; }

class DKnapsackQueriesOnATree {
public:
  static constexpr int MXV = ten(5);
  int N;
  vc<int> V, W, par, depth;
  vvc<int> G;
  vvc<lint> dp;

  void pre_calc() {
    dp[0][W[0]] = V[0];
    RREP(i, min(N - 1, 510)) {
      REP(j, MXV + 1) {
        dp[i][j] = dp[par[i]][j];
        if (j)
          chmax(dp[i][j], dp[i][j - 1]);
        if (j >= W[i])
          chmax(dp[i][j], dp[par[i]][j - W[i]] + V[i]);
      }
    }
  }

  lint query(int v, int w) {
    if (depth[v] <= 9) {
      return dp[v][w];
    }
    int cur = v;
    vc<pll> tmp;
    while (depth[cur] > 9) {
      tmp.eb(W[cur], V[cur]);
      cur = par[cur];
    }
    // curはvの先祖でdepth[cur]=9
    function<lint(int, int, lint)> dfs = [&](int idx, int curw, lint curv) {
      if (idx == LEN(tmp)) {
        return curv + dp[cur][w - curw];
      }
      lint res = dfs(idx + 1, curw, curv);
      if (curw + tmp[idx].first)
        chmax(res, dfs(idx + 1, curw + tmp[idx].first, curv + tmp[idx].second));
      return res;
    };
    return dfs(0, 0, 0);
  }

  void solve(std::istream &in, std::ostream &out) {
    ios_base::sync_with_stdio(false);
    in.tie(nullptr), out.tie(nullptr);
    in >> N;
    G.resize(N);
    V.resize(N);
    W.resize(N);
    par.resize(N);
    depth.resize(N, 1);
    REP(i, N) {
      in >> V[i] >> W[i];
      if (i) {
        par[i] = (i - 1) / 2;
        depth[i] = depth[par[i]] + 1;
      }
      if (2 * i + 1 < N)
        G[i].pb(2 * i + 1);
      if (2 * i + 2 < N)
        G[i].pb(2 * i + 2);
    }
    dp.resize(511, vc<lint>(MXV + 1, 0));
    pre_calc();
    int Q;
    in >> Q;
    while (Q--) {
      int v, l;
      in >> v >> l;
      v--;
      out << query(v, l) << '\\n';
    }
  }
};

int main() {
  DKnapsackQueriesOnATree solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}