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

#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define Test(tt) cout << "Case #" << tt << ": "

using namespace std;

const int MAXN = (1 << 18);

int value[MAXN + 1], weight[MAXN + 1];
vector<pair<int, int>> qry[MAXN + 1];

vector<pair<ll, ll>> sums[20][2];
ll sol[MAXN + 1];

ll pref_mx[1 << 10];

void dfs(int nod, int lvl, int n) {

  if (nod > n)
    return;

  if (sums[lvl - 1][0].size() > sums[lvl - 1][1].size()) {
    swap(sums[lvl - 1][0], sums[lvl - 1][1]);
  }

  int sza = (int)sums[lvl - 1][0].size(), szb = (int)sums[lvl - 1][0].size();
  int a = 0, b = 0;
  while (a < sza || b < szb) {
    if (b == szb || (a < sza && sums[lvl - 1][0][a].second + weight[nod] <=
                                    sums[lvl - 1][0][b].second)) {
      sums[lvl][0].push_back({sums[lvl - 1][0][a].first + value[nod],
                              sums[lvl - 1][0][a].second + weight[nod]});
      a++;
    } else {
      sums[lvl][0].push_back(sums[lvl - 1][0][b]);
      b++;
    }
  }
  sums[lvl][1] = sums[lvl - 1][1];

  pref_mx[0] = sums[lvl][1][0].first;
  for (int i = 1; i < (int)sums[lvl][1].size(); i++) {
    pref_mx[i] = max(pref_mx[i - 1], sums[lvl][1][i].first);
  }

  for (auto it : qry[nod]) {
    int j = (int)sums[lvl][1].size() - 1;
    for (int i = 0; i < (int)sums[lvl][0].size(); i++) {
      while (j >= 0 &&
             sums[lvl][0][i].second + sums[lvl][1][j].second > it.first) {
        j--;
      }
      if (j >= 0) {
        sol[it.second] =
            max(sol[it.second], sums[lvl][0][i].first + pref_mx[j]);
      }
    }
  }

  /*for(int i = 0; i < 2; i++) {
          for(auto it : sums[lvl][i]) {
                  cerr << it.first << " " << it.second << "  ";
          }
          cerr << "\\n";
  }
  cerr << "\\n\\n";*/

  dfs(2 * nod, lvl + 1, n);
  dfs(2 * nod + 1, lvl + 1, n);

  sums[lvl][0].clear();
  sums[lvl][1].clear();
}

int main() {
#ifdef HOME
  ifstream cin("B.in");
  ofstream cout("B.out");
#endif
  int i, n, q;
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);

  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> value[i] >> weight[i];
  }
  cin >> q;
  for (i = 1; i <= q; i++) {
    int nod, l;
    cin >> nod >> l;
    qry[nod].push_back({l, i});
  }

  sums[0][0].push_back({0, 0});
  sums[0][1].push_back({0, 0});

  dfs(1, 1, n);

  for (i = 1; i <= q; i++) {
    cout << sol[i] << "\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define Test(tt) cout << "Case #" << tt << ": "

using namespace std;

const int MAXN = (int)1e5;

int value[MAXN + 1], weight[MAXN + 1];
vector<pair<int, int>> qry[MAXN + 1];

vector<pair<ll, ll>> sums[20][2];
ll sol[MAXN + 1];

ll pref_mx[1 << 10];

void dfs(int nod, int lvl, int n) {

  if (nod > n)
    return;

  if (sums[lvl - 1][0].size() > sums[lvl - 1][1].size()) {
    swap(sums[lvl - 1][0], sums[lvl - 1][1]);
  }

  int sza = (int)sums[lvl - 1][0].size(), szb = (int)sums[lvl - 1][0].size();
  int a = 0, b = 0;
  while (a < sza || b < szb) {
    if (b == szb || (a < sza && sums[lvl - 1][0][a].second + weight[nod] <=
                                    sums[lvl - 1][0][b].second)) {
      sums[lvl][0].push_back({sums[lvl - 1][0][a].first + value[nod],
                              sums[lvl - 1][0][a].second + weight[nod]});
      a++;
    } else {
      sums[lvl][0].push_back(sums[lvl - 1][0][b]);
      b++;
    }
  }
  sums[lvl][1] = sums[lvl - 1][1];

  pref_mx[0] = sums[lvl][1][0].first;
  for (int i = 1; i < (int)sums[lvl][1].size(); i++) {
    pref_mx[i] = max(pref_mx[i - 1], sums[lvl][1][i].first);
  }

  for (auto it : qry[nod]) {
    int j = (int)sums[lvl][1].size() - 1;
    for (int i = 0; i < (int)sums[lvl][0].size(); i++) {
      while (j >= 0 &&
             sums[lvl][0][i].second + sums[lvl][1][j].second > it.first) {
        j--;
      }
      if (j >= 0) {
        sol[it.second] =
            max(sol[it.second], sums[lvl][0][i].first + pref_mx[j]);
      }
    }
  }

  /*for(int i = 0; i < 2; i++) {
          for(auto it : sums[lvl][i]) {
                  cerr << it.first << " " << it.second << "  ";
          }
          cerr << "\\n";
  }
  cerr << "\\n\\n";*/

  dfs(2 * nod, lvl + 1, n);
  dfs(2 * nod + 1, lvl + 1, n);

  sums[lvl][0].clear();
  sums[lvl][1].clear();
}

int main() {
#ifdef HOME
  ifstream cin("B.in");
  ofstream cout("B.out");
#endif
  int i, n, q;
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);

  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> value[i] >> weight[i];
  }
  cin >> q;
  for (i = 1; i <= q; i++) {
    int nod, l;
    cin >> nod >> l;
    qry[nod].push_back({l, i});
  }

  sums[0][0].push_back({0, 0});
  sums[0][1].push_back({0, 0});

  dfs(1, 1, n);

  for (i = 1; i <= q; i++) {
    cout << sol[i] << "\\n";
  }

  return 0;
}