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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define ALL(obj) (obj).begin(), (obj).end()
#define SPEED                                                                  \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false);

template <class T> using PQ = priority_queue<T>;
template <class T> using PQR = priority_queue<T, vector<T>, greater<T>>;

constexpr long long MOD = (long long)1e9 + 7;
constexpr long long MOD2 = 998244353;
constexpr long long HIGHINF = (long long)1e18;
constexpr long long LOWINF = (long long)1e15;
constexpr long double PI = 3.1415926535897932384626433L;

template <class T> void chmax(T &a, const T b) { a = max(a, b); }
template <class T> void chmin(T &a, const T b) { a = min(a, b); }
void YN(bool flg) { cout << (flg ? "YES" : "NO") << endl; }
void Yn(bool flg) { cout << (flg ? "Yes" : "No") << endl; }
void yn(bool flg) { cout << (flg ? "yes" : "no") << endl; }

int main() {
  SPEED
  int N;
  cin >> N;
  vector<int> V(N + 1), W(N + 1);
  for (int i = 1; i <= N; ++i)
    cin >> V[i] >> W[i];

  // O(Q)
  int nl = 1 << 9, nr = 1 << 9, L_max = 100001;
  vector<int> msb(nl, 0);
  for (int i = 1; i < nl; ++i)
    msb[i] = 31 - __builtin_clz(i);
  vector<int> bit_w_dp(nl, 0);
  vector<int> bit_v_dp(nl, 0);
  vector<vector<int>> dp(nl, vector<int>(L_max, 0));

  // 前計算
  for (int n = 1; n < min(nl, N + 1); ++n) {
    int k = 2 * n;
    vector<int> l;
    while (k /= 2)
      l.push_back(k);
    int sz = l.size();
    // bit全探索
    for (int i = 1; i < (1 << sz); ++i) {
      bit_w_dp[i] = bit_w_dp[i ^ (1 << msb[i])] + W[l[msb[i]]];
      bit_v_dp[i] = bit_v_dp[i ^ (1 << msb[i])] + V[l[msb[i]]];
      if (bit_w_dp[i] <= L_max)
        chmax(dp[n][bit_w_dp[i]], bit_v_dp[i]);
    }
    // 累積max
    for (int i = 1; i < L_max; ++i) {
      chmax(dp[n][i], dp[n][i - 1]);
    }
  }

  int Q;
  cin >> Q;
  string ans_str = "";

  // クエリ
  while (Q--) {
    int p, L;
    cin >> p >> L;
    vector<int> r;
    int k = 2 * p, l = 0;
    while (k /= 2) {
      if (k < nl)
        chmax(l, k);
      else
        r.push_back(k);
    }
    int sz = r.size(), ans = dp[l][L];
    // bit全探索
    for (int i = 1; i < (1 << sz); ++i) {
      bit_w_dp[i] = bit_w_dp[i ^ (1 << msb[i])] + W[r[msb[i]]];
      bit_v_dp[i] = bit_v_dp[i ^ (1 << msb[i])] + V[r[msb[i]]];
      if (bit_w_dp[i] <= L)
        chmax(ans, dp[l][L - bit_w_dp[i]] + bit_v_dp[i]);
    }
    ans_str += to_string(ans) + "\\n";
  }
  cout << ans_str;

  return 0;
}


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;
using ll = long long;

#define ALL(obj) (obj).begin(), (obj).end()
#define SPEED                                                                  \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false);

template <class T> using PQ = priority_queue<T>;
template <class T> using PQR = priority_queue<T, vector<T>, greater<T>>;

constexpr long long MOD = (long long)1e9 + 7;
constexpr long long MOD2 = 998244353;
constexpr long long HIGHINF = (long long)1e18;
constexpr long long LOWINF = (long long)1e15;
constexpr long double PI = 3.1415926535897932384626433L;

template <class T> void chmax(T &a, const T b) { a = max(a, b); }
template <class T> void chmin(T &a, const T b) { a = min(a, b); }
void YN(bool flg) { cout << (flg ? "YES" : "NO") << endl; }
void Yn(bool flg) { cout << (flg ? "Yes" : "No") << endl; }
void yn(bool flg) { cout << (flg ? "yes" : "no") << endl; }

int main() {
  SPEED
  int N;
  cin >> N;
  vector<int> V(N + 1), W(N + 1);
  for (int i = 1; i <= N; ++i)
    cin >> V[i] >> W[i];

  // O(Q)
  int nl = 1 << 9, nr = 1 << 9, L_max = 100001;
  vector<int> msb(nl, 0);
  for (int i = 1; i < nl; ++i)
    msb[i] = 31 - __builtin_clz(i);
  vector<int> bit_w_dp(nl, 0);
  vector<int> bit_v_dp(nl, 0);
  vector<vector<int>> dp(nl, vector<int>(L_max, 0));

  // 前計算
  for (int n = 1; n < min(nl, N + 1); ++n) {
    int k = 2 * n;
    vector<int> l;
    while (k /= 2)
      l.push_back(k);
    int sz = l.size();
    // bit全探索
    for (int i = 1; i < (1 << sz); ++i) {
      bit_w_dp[i] = bit_w_dp[i ^ (1 << msb[i])] + W[l[msb[i]]];
      bit_v_dp[i] = bit_v_dp[i ^ (1 << msb[i])] + V[l[msb[i]]];
      chmax(dp[n][bit_w_dp[i]], bit_v_dp[i]);
    }
    // 累積max
    for (int i = 1; i < L_max; ++i) {
      chmax(dp[n][i], dp[n][i - 1]);
    }
  }

  int Q;
  cin >> Q;
  string ans_str = "";

  // クエリ
  while (Q--) {
    int p, L;
    cin >> p >> L;
    vector<int> r;
    int k = 2 * p, l = 0;
    while (k /= 2) {
      if (k < nl)
        chmax(l, k);
      else
        r.push_back(k);
    }
    int sz = r.size(), ans = dp[l][L];
    // bit全探索
    for (int i = 1; i < (1 << sz); ++i) {
      bit_w_dp[i] = bit_w_dp[i ^ (1 << msb[i])] + W[r[msb[i]]];
      bit_v_dp[i] = bit_v_dp[i ^ (1 << msb[i])] + V[r[msb[i]]];
      if (bit_w_dp[i] <= L)
        chmax(ans, dp[l][L - bit_w_dp[i]] + bit_v_dp[i]);
    }
    ans_str += to_string(ans) + "\\n";
  }
  cout << ans_str;

  return 0;
}