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

#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

const int SZ = 1 << 12;
vector<pair<int, int>> D[1 << 18], T;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  TEST(freopen("input.txt", "r", stdin));
  TEST(freopen("output.txt", "w", stdout));
  TEST(freopen("debug.txt", "w", stderr));
  int N, v, w;
  cin >> N >> v >> w;
  D[1].emplace_back(0, 0);
  D[1].emplace_back(w, v);
  for (int i = 2; i <= N; i++) {
    int j = 1, k = 0, jsz, ksz;
    cin >> v >> w;
    T = D[i / 2];
    for (auto &[W, V] : T) {
      W += w;
      V += v;
    }
    while (T.size() && T.back().first > 100000)
      T.pop_back();
    D[i].emplace_back(0, 0);
    if (i >= 2 * SZ && i < 4 * SZ) {
      jsz = ksz = 1;
    } else {
      jsz = D[i / 2].size();
      ksz = T.size();
    }
    while (j < jsz && k < ksz) {
      if (D[i / 2][j] < T[k]) {
        if (D[i].back().first == D[i / 2][j].first)
          D[i].pop_back();
        if (D[i].back().second < D[i / 2][j].second)
          D[i].push_back(D[i / 2][j]);
        j++;
      } else {
        if (D[i].back().first == T[k].first)
          D[i].pop_back();
        if (D[i].back().second < T[k].second)
          D[i].push_back(T[k]);
        k++;
      }
    }
    while (j < jsz) {
      if (D[i].back().first == D[i / 2][j].first)
        D[i].pop_back();
      if (D[i].back().second < D[i / 2][j].second)
        D[i].push_back(D[i / 2][j]);
      j++;
    }
    while (k < ksz) {
      if (D[i].back().first == T[k].first)
        D[i].pop_back();
      if (D[i].back().second < T[k].second)
        D[i].push_back(T[k]);
      k++;
    }
  }
  for (cin >> N; N--;) {
    int v, L;
    cin >> v >> L;
    if (v < 2 * SZ) {
      w = upper_bound(D[v].begin(), D[v].end(), make_pair(L, 0x7fffffff)) -
          D[v].begin() - 1;
      cout << D[v][w].second << '\\n';
    } else {
      int p = v, j, ans = 0;
      while (p >= 2 * SZ)
        p >>= 1;
      for (auto [W, V] : D[v]) {
        if (W > L)
          break;
        j = upper_bound(D[p].begin(), D[p].end(),
                        make_pair(L - W, 0x7fffffff)) -
            D[p].begin() - 1;
        ans = max(ans, V + D[p][j].second);
      }
      cout << ans << '\\n';
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

const int SZ = 1 << 12;
vector<pair<int, int>> D[1 << 18], T;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  TEST(freopen("input.txt", "r", stdin));
  TEST(freopen("output.txt", "w", stdout));
  TEST(freopen("debug.txt", "w", stderr));
  int N, v, w;
  cin >> N >> v >> w;
  D[1].emplace_back(0, 0);
  D[1].emplace_back(w, v);
  for (int i = 2; i <= N; i++) {
    int j = 0, k = 0, jsz, ksz;
    cin >> v >> w;
    T = D[i / 2];
    for (auto &[W, V] : T) {
      W += w;
      V += v;
    }
    while (T.size() && T.back().first > 100000)
      T.pop_back();
    D[i].emplace_back(0, 0);
    if (i >= 2 * SZ && i / 2 < 2 * SZ) {
      jsz = ksz = 1;
    } else {
      jsz = D[i / 2].size();
      ksz = T.size();
    }
    while (j < jsz && k < ksz) {
      if (D[i / 2][j] < T[k]) {
        if (D[i].back().first == D[i / 2][j].second)
          D[i].pop_back();
        if (D[i].back().second < D[i / 2][j].second)
          D[i].push_back(D[i / 2][j]);
        j++;
      } else {
        if (D[i].back().first == T[k].second)
          D[i].pop_back();
        if (D[i].back().second < T[k].second)
          D[i].push_back(T[k]);
        k++;
      }
    }
    while (j < jsz) {
      if (D[i].back().first == D[i / 2][j].second)
        D[i].pop_back();
      if (D[i].back().second < D[i / 2][j].second)
        D[i].push_back(D[i / 2][j]);
      j++;
    }
    while (k < ksz) {
      if (D[i].back().first == T[k].second)
        D[i].pop_back();
      if (D[i].back().second < T[k].second)
        D[i].push_back(T[k]);
      k++;
    }
  }
  for (cin >> N; N--;) {
    int v, L;
    cin >> v >> L;
    if (v < 2 * SZ) {
      w = upper_bound(D[v].begin(), D[v].end(), make_pair(L, 0x7fffffff)) -
          D[v].begin() - 1;
      cout << D[v][w].second << '\\n';
    } else {
      int p = v, j, ans = 0;
      while (p > 2 * SZ)
        p >>= 1;
      for (auto [W, V] : D[v]) {
        if (W > L)
          break;
        j = upper_bound(D[p].begin(), D[p].end(),
                        make_pair(L - W, 0x7fffffff)) -
            D[p].begin() - 1;
        ans = max(ans, V + D[p][j].second);
      }
      cout << ans << '\\n';
    }
  }
  return 0;
}