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

#include <algorithm>
#include <chrono>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
#define fastInp                                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  ios_base::sync_with_stdio(0);

const ll SIZE = 1e6, INF = 1e9 * 1e9 + 10, MOD = 998244353, B = 9;

ll dpt[SIZE];
vector<pair<ll, ll>> vec;
vector<vector<pair<ll, ll>>> vls;

int main() {
  fastInp;

  ll n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    ll x, y;
    cin >> x >> y;

    vec.push_back({x, y});
  }
  ll q;
  cin >> q;
  dpt[0] = 0;

  vls.resize(n);
  vls[0].push_back({0, 0});
  vls[0].push_back(vec[0]);
  for (int i = 1; i < n; i++) {
    dpt[i] = dpt[(i - 1) / 2] + 1;
    if (dpt[i] <= B) {
      vector<pair<ll, ll>> nw;
      for (auto cur : vls[(i - 1) / 2]) {
        nw.push_back(cur);
        nw.push_back({cur.first + vec[i].first, cur.second + vec[i].second});
      }
      vls[i] = nw;
      sort(vls[i].begin(), vls[i].end());

      nw.clear();

      nw.push_back(vls[i][0]);
      for (int j = 1; j < vls[i].size(); j++) {
        while (nw.size() > 0 && nw.back().second >= vls[i][j].second)
          nw.pop_back();
        nw.push_back(vls[i][j]);
      }
    }
  }

  while (q--) {
    ll v, l;
    cin >> v >> l;
    v--;
    ll ans = 0;
    if (dpt[v] <= B) {
      for (auto cur : vls[v]) {
        if (cur.second <= l)
          ans = max(ans, cur.first);
      }
      cout << ans << "\\n";
    } else {
      vector<pair<ll, ll>> nw;
      ll u = v;
      nw.push_back({0, 0});
      nw.push_back(vec[u]);
      u = (u - 1) / 2;
      while (dpt[u] > B) {
        vector<pair<ll, ll>> nw2;
        for (auto cur : nw) {
          nw2.push_back(cur);
          nw2.push_back({cur.first + vec[u].first, cur.second + vec[u].second});
        }
        nw = nw2;

        nw2.clear();
        nw2.push_back(nw[0]);
        sort(nw.begin(), nw.end());
        for (int j = 1; j < nw.size(); j++) {
          while (nw2.size() > 0 && nw2.back().second >= nw[j].second)
            nw2.pop_back();
          nw2.push_back(nw[j]);
        }

        nw = nw2;

        u = (u - 1) / 2;
      }

      ll i = vls[u].size() - 1, i2 = 0, ans = 0;
      while (i2 < nw.size() && i >= 0) {
        while (i2 < nw.size() && i >= 0 &&
               (nw[i2].second + vls[u][i].second > l)) {
          i--;
        }
        if (i == -1)
          break;

        ans = max(ans, nw[i2].first + vls[u][i].first);
        i2++;
      }

      cout << ans << "\\n";
    }
  }
  return 0;
}

// 192 / 216 - 8 / 9
// 192 / 8

// 54 / 72 - 3 / 4
// 54 / 6


Error: 0

Fixed submission:

#include <algorithm>
#include <chrono>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
#define fastInp                                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  ios_base::sync_with_stdio(0);

const ll SIZE = 1e6, INF = 1e9 * 1e9 + 10, MOD = 998244353;

ll dpt[SIZE];
vector<pair<ll, ll>> vec;
vector<vector<pair<ll, ll>>> vls;

int main() {
  fastInp;

  ll n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    ll x, y;
    cin >> x >> y;

    vec.push_back({x, y});
  }
  ll q;
  cin >> q;
  dpt[0] = 0;

  vls.resize(n);
  vls[0].push_back({0, 0});
  vls[0].push_back(vec[0]);
  for (int i = 1; i < n; i++) {
    dpt[i] = dpt[(i - 1) / 2] + 1;
    if (dpt[i] <= 9) {
      vector<pair<ll, ll>> nw;
      for (auto cur : vls[(i - 1) / 2]) {
        nw.push_back(cur);
        nw.push_back({cur.first + vec[i].first, cur.second + vec[i].second});
      }
      vls[i] = nw;
      sort(vls[i].begin(), vls[i].end());

      nw.clear();

      nw.push_back(vls[i][0]);
      for (int j = 1; j < vls[i].size(); j++) {
        while (nw.size() > 0 && nw.back().second >= vls[i][j].second)
          nw.pop_back();
        nw.push_back(vls[i][j]);
      }
    }
  }

  while (q--) {
    ll v, l;
    cin >> v >> l;
    v--;
    ll ans = 0;
    if (dpt[v] <= 9) {
      for (auto cur : vls[v]) {
        if (cur.second <= l)
          ans = max(ans, cur.first);
      }
      cout << ans << "\\n";
    } else {
      vector<pair<ll, ll>> nw;
      ll u = v;
      while (dpt[u] > 9) {
        vector<pair<ll, ll>> nw2;
        for (auto cur : nw) {
          nw2.push_back(cur);
          nw2.push_back({cur.first + vec[u].first, cur.second + vec[u].second});
        }
        nw = nw2;

        nw2.clear();
        nw2.push_back(nw[0]);
        for (int j = 1; j < nw.size(); j++) {
          while (nw2.size() > 0 && nw2.back().second >= nw[j].second)
            nw2.pop_back();
          nw2.push_back(nw[j]);
        }

        nw = nw2;

        u = (u - 1) / 2;
      }

      ll i = vls[u].size() - 1, i2 = 0, ans = 0;
      while (i2 < nw.size() && i >= 0) {
        while (i2 < nw.size() && i >= 0 &&
               (nw[i2].second + vls[u][i].second > l)) {
          i--;
        }
        if (i == -1)
          break;

        ans = max(ans, nw[i2].first + vls[u][i].first);
        i2++;
      }

      cout << ans << "\\n";
    }
  }
  return 0;
}

// 192 / 216 - 8 / 9
// 192 / 8

// 54 / 72 - 3 / 4
// 54 / 6