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

// D.

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<LL, LL> LLLL;

vector<LL> v;
vector<LL> w;

const int MAX_W = 100000;
const int THRESHOLD = 1 << 9;

vector<LLLL> calc(int node) {
  int init_node = node;
  vector<LLLL> dp;
  dp.emplace_back(LLLL(0, 0));
  for (LL i = 0; i < 9; ++i) {
    vector<LLLL> next = dp;
    for (auto kv : dp) {
      if (kv.first + w[node] > MAX_W)
        continue;
      next.emplace_back(LLLL(kv.first + w[node], kv.second + v[node]));
    }
    dp = next;
    node /= 2;
    if (node <= 0 || (init_node >= THRESHOLD && node < THRESHOLD)) {
      break;
    }
  }
  return dp;
}

int main(int argc, char *argv[]) {
  LL n, q, x, y;
  cin >> n;
  v.resize(n + 1);
  w.resize(n + 1);
  for (LL i = 1; i <= n; ++i) {
    cin >> v[i] >> w[i];
  }
  vector<vector<LL>> lower(THRESHOLD);
  if (n >= THRESHOLD) {
    vector<vector<LLLL>> dp(THRESHOLD);
    dp[0].emplace_back(LLLL(0, 0));
    for (int i = 1; i < THRESHOLD; ++i) {
      vector<LLLL> next = dp[i / 2];
      for (auto kv : dp[i / 2]) {
        if (kv.first + w[i] > MAX_W)
          continue;
        next.emplace_back(LLLL(kv.first + w[i], kv.second + v[i]));
      }
      dp[i] = next;
      if (i >= THRESHOLD / 2) {
        next.emplace_back(LLLL(MAX_W + 1, 0));
        sort(next.begin(), next.end());
        LL mxv = 0, prev = 0;
        lower[i].resize(MAX_W + 1);
        for (auto kv : next) {
          for (LL j = prev; j < kv.first; ++j) {
            lower[i][j] = mxv;
          }
          prev = kv.first;
          mxv = max(mxv, kv.second);
        }
      }
    }
  }
  cin >> q;
  for (LL tt = 0; tt < q; ++tt) {
    cin >> x >> y;
    LL ans = 0;
    vector<LLLL> r = calc(x);
    if (x < THRESHOLD) {
      for (auto kv : r) {
        if (kv.first <= y) {
          ans = max(ans, kv.second);
        }
      }
    } else {
      while (x >= THRESHOLD) {
        x /= 2;
      }
      for (auto kv : r) {
        if (kv.first <= y) {
          ans = max(ans, kv.second + lower[x][y - kv.first]);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

// D.

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<LL, LL> LLLL;

vector<LL> v;
vector<LL> w;

const int THRESHOLD = 1 << 9;

map<LL, LL> calc(int node) {
  int init_node = node;
  vector<LLLL> dp;
  dp.emplace_back(LLLL(0, 0));
  for (LL i = 0; i < 9; ++i) {
    vector<LLLL> next = dp;
    for (auto kv : dp) {
      if (kv.first + w[node] > 100000)
        continue;
      next.emplace_back(LLLL(kv.first + w[node], kv.second + v[node]));
    }
    dp = next;
    node /= 2;
    if (node <= 0 || (init_node >= THRESHOLD && node < THRESHOLD)) {
      break;
    }
  }
  sort(dp.begin(), dp.end());
  map<LL, LL> r;
  LL mxv = 0;
  for (auto kv : dp) {
    mxv = max(mxv, kv.second);
    r[kv.first] = mxv;
  }
  return r;
}

int main(int argc, char *argv[]) {
  LL n, q, x, y;
  cin >> n;
  v.resize(n + 1);
  w.resize(n + 1);
  for (LL i = 1; i <= n; ++i) {
    cin >> v[i] >> w[i];
  }
  vector<vector<LL>> lower(THRESHOLD);
  if (n >= THRESHOLD) {
    vector<vector<LLLL>> dp(THRESHOLD);
    dp[0].emplace_back(LLLL(0, 0));
    for (int i = 1; i < THRESHOLD; ++i) {
      vector<LLLL> next = dp[i / 2];
      for (auto kv : dp[i / 2]) {
        next.emplace_back(LLLL(kv.first + w[i], kv.second + v[i]));
      }
      dp[i] = next;
      if (i >= THRESHOLD / 2) {
        next.emplace_back(LLLL(100001, 0));
        sort(next.begin(), next.end());
        LL mxv = 0, prev = -1;
        lower[i].resize(100001);
        for (auto kv : next) {
          for (LL j = prev + 1; j < kv.first; ++j) {
            lower[i][j] = mxv;
          }
          prev = kv.first;
          mxv = kv.second;
        }
      }
    }
  }
  cin >> q;
  for (LL tt = 0; tt < q; ++tt) {
    cin >> x >> y;
    LL ans = 0;
    map<LL, LL> m = calc(x);
    if (x < THRESHOLD) {
      auto mv = m.lower_bound(y);
      if (mv == m.end() || mv->first > y) {
        --mv;
      }
      ans = mv->second;
    } else {
      while (x >= THRESHOLD) {
        x /= 2;
      }
      for (auto kv : m) {
        LL rw = y - kv.first;
        if (rw < 0) {
          break;
        }
        ans = max(ans, kv.second + lower[x][rw]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}