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


#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  int N;
  std::cin >> N;
  std::vector<int> V(N + 1), W(N + 1);
  for (int i = 1; i <= N; ++i)
    std::cin >> V[i] >> W[i];

  int Q;
  std::cin >> Q;
  std::vector<int> ans(Q);
  for (int q = 0; q < Q; q++) {
    int v, L;
    std::cin >> v >> L;

    using P = std::pair<int, int>;
    std::vector<P> l1, l2;

    auto gen = [&](std::vector<P> &l, int thr) {
      std::vector<P> tmp;
      l.emplace_back(0, 0);
      while (v >= thr) {
        int p1 = 0, p2 = 0;
        while (p1 < (int)l.size() || p2 < (int)l.size()) {
          P nxt;
          if (p1 < (int)l.size() && l[p1].first < l[p2].first + W[v])
            nxt = l[p1++];
          else
            nxt = {l[p2].first + W[v], l[p2++].second + V[v]};
          if (tmp.empty() || tmp.back().second < nxt.second)
            tmp.push_back(nxt);
        }
        v /= 2;
        l.swap(tmp);
        tmp.clear();
      }
    };

    constexpr int b = 9;
    gen(l1, 1 << b);
    gen(l2, 1);
    int p1 = 0, p2 = 0;
    while (p2 + 1 < (int)l2.size() && l1[p1].first + l2[p2 + 1].first <= L)
      p2++;
    ans[q] = l1[p1].second + l2[p2].second;
    while (++p1 < (int)l1.size() && l1[p1].first <= L) {
      while (l1[p1].first + l2[p2].first > L)
        p2--;
      ans[q] = std::max(ans[q], l1[p1].second + l2[p2].second);
    }
  }
  for (auto &&a : ans)
    std::cout << a << std::endl;
  return 0;
}


Error: 0

Fixed submission:


#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// std::numeric_limits<int>::max(), min()
// std::setprecision(X)
// std::islower(), std::isupper(), std::tolower, std::toupper
// std::accumulate(m.begin(), m.end(), 0LL);
// std::sort(begin(),end()) // 0, 1, 2, 3 ,4 (default, less);
// std::priority_queue<int, vector<int>, less<int>> que; // 4, 3, 2, 1 (defualt,
// less) : if ( std::regex_match(S, std::regex("(dream|dreamer|erase|eraser)*"))
// ) std::cout << "YES" << std::endl;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

using ll = long long;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  ll N;
  std::cin >> N;
  std::vector<int> V(N), W(N);
  rep(i, N) std::cin >> V[i] >> W[i];

  int Q;
  std::cin >> Q;
  std::vector<int> v(Q), L(Q);
  rep(i, Q) std::cin >> v[i] >> L[i];

  std::vector<int> ind(Q);
  rep(i, Q) ind[i] = i;
  std::sort(all(ind), [&L](int x, int y) { return L[x] > L[y]; });

  std::vector<std::vector<int>> Gdp(N, std::vector<int>(1e5 + 1, -1));
  std::vector<int> ans(Q);
  rep(t, Q) {
    int i = ind[t];
    int s = v[i];
    std::vector<int> vtx;
    vtx.push_back(s);
    while (s != 1) {
      s /= 2;
      vtx.push_back(s);
    }

    if (Gdp[s - 1][L[i]] != -1)
      ans[i] = Gdp[s - 1][L[i]];
    else {
      // vtx dp
      std::vector<std::vector<ll>> dp(sz(vtx) + 1,
                                      std::vector<ll>(L[i] + 1, 0));
      for (int m = 0; m < sz(vtx); m++)
        for (int n = 0; n <= L[i]; n++) {
          if (n - W[vtx[m] - 1] >= 0)
            dp[m + 1][n] =
                std::max(dp[m][n], dp[m][n - W[vtx[m] - 1]] + V[vtx[m] - 1]);
          else
            dp[m + 1][n] = dp[m][n];
          if (m >= 1)
            Gdp[vtx[m - 1] - 1][n] = dp[m - 1][n];
        }
      ans[i] = dp[sz(vtx)][L[i]];
    }
  }

  for (auto &&a : ans)
    std::cout << a << std::endl;
  return 0;
}