Problem p02648 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int maxD = 1 << 9, maxQ = 100009, maxN = (1 << 18) + 9;
int maxL, N, Q, v[maxN], w[maxN], id[20], ans[maxQ];
vector<int> dp;
vector<pair<int, int>> qr[maxQ];

void df0(int nod) {
  if (nod >= maxD) {
    if (!qr[nod].empty()) {
      int nr = 1;
      id[0] = nod;
      while (id[nr - 1] / 2 >= maxD)
        id[nr] = id[nr - 1] / 2, nr++;
      for (auto q : qr[nod]) {
        int L = q.first, pos = q.second;
        for (int msk = 0; msk < (1 << nr); msk++) {
          int tV = 0, tW = 0;
          for (int i = 0; i < nr; i++)
            if (msk & (1 << i))
              tV += v[id[i]], tW += w[id[i]];
          if (tW <= L && dp[L - tW] + tV > ans[pos])
            ans[pos] = dp[L - tW] + tV;
        }
      }
    }
    if (nod * 2 <= N)
      df0(nod << 1);
    if (nod * 2 < N)
      df0(nod << 1 | 1);
    return;
  }
  vector<int> old = dp;
  for (int i = w[nod]; i <= maxL; i++)
    if (old[i - w[nod]] + v[nod] > dp[i])
      dp[i] = old[i - w[nod]] + v[nod];
  /*    printf ("%d: ", nod);
      for (int i=0; i<=maxL; i++)
          printf ("(%d, %d) ", i, dp[i]);
      printf ("\n");*/
  for (auto q : qr[nod]) {
    //        printf ("%d -> (%d, %d)\n", q.second, q.first, dp[q.first]);
    ans[q.second] = dp[q.first];
  }
  if (nod * 2 <= N)
    df0(nod << 1);
  if (nod * 2 < N)
    df0(nod << 1 | 1);
  dp = old;
}

int main() {
  // freopen ("input", "r", stdin);
  // freopen ("output", "w", stdout);

  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%d %d", &v[i], &w[i]);
  scanf("%d", &Q);
  for (int i = 1; i <= Q; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    if (y > maxL)
      maxL = y;
    qr[x].push_back({y, i});
  }
  dp = vector<int>(maxL + 2, 0);
  df0(1);
  for (int i = 1; i <= Q; i++)
    printf("%d\n", ans[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int maxD = 1 << 9, maxQ = 100009, maxN = (1 << 18) + 9;
int maxL, N, Q, v[maxN], w[maxN], id[20], ans[maxQ];
vector<int> dp;
vector<pair<int, int>> qr[maxN];

void df0(int nod) {
  if (nod >= maxD) {
    if (!qr[nod].empty()) {
      int nr = 1;
      id[0] = nod;
      while (id[nr - 1] / 2 >= maxD)
        id[nr] = id[nr - 1] / 2, nr++;
      for (auto q : qr[nod]) {
        int L = q.first, pos = q.second;
        for (int msk = 0; msk < (1 << nr); msk++) {
          int tV = 0, tW = 0;
          for (int i = 0; i < nr; i++)
            if (msk & (1 << i))
              tV += v[id[i]], tW += w[id[i]];
          if (tW <= L && dp[L - tW] + tV > ans[pos])
            ans[pos] = dp[L - tW] + tV;
        }
      }
    }
    if (nod * 2 <= N)
      df0(nod << 1);
    if (nod * 2 < N)
      df0(nod << 1 | 1);
    return;
  }
  vector<int> old = dp;
  for (int i = w[nod]; i <= maxL; i++)
    if (old[i - w[nod]] + v[nod] > dp[i])
      dp[i] = old[i - w[nod]] + v[nod];
  /*    printf ("%d: ", nod);
      for (int i=0; i<=maxL; i++)
          printf ("(%d, %d) ", i, dp[i]);
      printf ("\n");*/
  for (auto q : qr[nod]) {
    //        printf ("%d -> (%d, %d)\n", q.second, q.first, dp[q.first]);
    ans[q.second] = dp[q.first];
  }
  if (nod * 2 <= N)
    df0(nod << 1);
  if (nod * 2 < N)
    df0(nod << 1 | 1);
  dp = old;
}

int main() {
  // freopen ("input", "r", stdin);
  // freopen ("output", "w", stdout);

  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%d %d", &v[i], &w[i]);
  scanf("%d", &Q);
  for (int i = 1; i <= Q; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    if (y > maxL)
      maxL = y;
    qr[x].push_back({y, i});
  }
  dp = vector<int>(maxL + 2, 0);
  df0(1);
  for (int i = 1; i <= Q; i++)
    printf("%d\n", ans[i]);
  return 0;
}