Problem p02648 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, m, n) for (int(i) = (int)(m); i < (int)(n); ++i)
#define rep2(i, m, n) for (int(i) = (int)(n)-1; i >= (int)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll)1e9 + 7;
// constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;
typedef vector<ll> Array;
typedef vector<Array> Matrix;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

struct Edge {
  ll to, rev;
  long double cap;
  Edge(ll _to, long double _cap, ll _rev) {
    to = _to;
    cap = _cap;
    rev = _rev;
  }
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;

void add_edge(Graph &G, ll from, ll to, long double cap, bool revFlag,
              long double revCap) {
  G[from].push_back(Edge(to, cap, (ll)G[to].size()));
  if (revFlag)
    G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

void solve() {
  int n;
  cin >> n;
  vec<ll> v(n), w(n);
  REP(i, n)
  cin >> v[i] >> w[i];
  ll max_w = 101010;

  vvec<P> dp(1024);
  dp[0] = vec<P>({{0, 0}, {w[0], v[0]}});
  REP(i, min(n, 1024)) {
    vec<P> dp1 = dp[(i + 1) / 2 - 1];
    vec<P> dp2;
    for (auto [ww, vv] : dp1) {
      if (ww + w[i] > max_w)
        break;
      dp2.push_back({ww + w[i], vv + v[i]});
    }
    merge(all(dp1), all(dp2), back_inserter(dp[i]));
  }

  auto calc = [&](int p, int l, vec<int> o) {
    vec<P> wv(1, {0, 0});
    for (auto i : o) {
      vec<P> wv1 = wv;
      vec<P> wv2;
      for (auto [ww, vv] : wv1) {
        if (ww + w[i] > l)
          break;
        wv2.push_back({ww + w[i], vv + v[i]});
      }
      wv.clear();
      merge(all(wv1), all(wv2), back_inserter(wv));
    }

    ll ans = 0;
    ll ma = 0;
    int t = 0;
    REP2(i, dp[p].size()) {
      if (dp[p][i].first > l)
        continue;
      while (t < wv.size() and dp[p][i].first + wv[t].first <= l) {
        chmax(ma, wv[t].second);
        t++;
      }
      chmax(ans, dp[p][i].second + ma);
    }
    cout << ans << en;
  };

  ll q;
  cin >> q;
  while (q--) {
    ll v, l;
    cin >> v >> l;
    if (v <= 1024) {
      v--;
      ll ans = 0;
      for (auto [ww, vv] : dp[v]) {
        if (ww > l)
          break;
        chmax(ans, vv);
      }
      cout << ans << en;
    } else {
      vec<int> o;
      while (v) {
        if (v <= 1024) {
          calc(v - 1, l, o);
          break;
        }
        o.push_back(v - 1);
        v /= 2;
      }
    }
  }
}

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

  solve();
  // ll t;cin>>t;REP(i,t) solve();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, m, n) for (int(i) = (int)(m); i < (int)(n); ++i)
#define rep2(i, m, n) for (int(i) = (int)(n)-1; i >= (int)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll)1e9 + 7;
// constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;
typedef vector<ll> Array;
typedef vector<Array> Matrix;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

struct Edge {
  ll to, rev;
  long double cap;
  Edge(ll _to, long double _cap, ll _rev) {
    to = _to;
    cap = _cap;
    rev = _rev;
  }
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;

void add_edge(Graph &G, ll from, ll to, long double cap, bool revFlag,
              long double revCap) {
  G[from].push_back(Edge(to, cap, (ll)G[to].size()));
  if (revFlag)
    G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

void solve() {
  int n;
  cin >> n;
  vec<ll> v(n), w(n);
  REP(i, n)
  cin >> v[i] >> w[i];
  ll max_w = 101010;

  vvec<P> dp(1024);
  dp[0] = vec<P>({{0, 0}, {w[0], v[0]}});
  int m = min(n, 1024);
  rep(i, 1, m) {
    vec<P> dp1 = dp[(i + 1) / 2 - 1];
    vec<P> dp2;
    for (auto [ww, vv] : dp1) {
      if (ww + w[i] > max_w)
        break;
      dp2.push_back({ww + w[i], vv + v[i]});
    }
    merge(all(dp1), all(dp2), back_inserter(dp[i]));
  }

  auto calc = [&](int p, int l, vec<int> o) {
    vec<P> wv(1, {0, 0});
    for (auto i : o) {
      vec<P> wv1 = wv;
      vec<P> wv2;
      for (auto [ww, vv] : wv1) {
        if (ww + w[i] > l)
          break;
        wv2.push_back({ww + w[i], vv + v[i]});
      }
      wv.clear();
      merge(all(wv1), all(wv2), back_inserter(wv));
    }

    ll ans = 0;
    ll ma = 0;
    int t = 0;
    REP2(i, dp[p].size()) {
      if (dp[p][i].first > l)
        continue;
      while (t < wv.size() and dp[p][i].first + wv[t].first <= l) {
        chmax(ma, wv[t].second);
        t++;
      }
      chmax(ans, dp[p][i].second + ma);
    }
    cout << ans << en;
  };

  ll q;
  cin >> q;
  while (q--) {
    ll v, l;
    cin >> v >> l;
    if (v <= 1024) {
      v--;
      ll ans = 0;
      for (auto [ww, vv] : dp[v]) {
        if (ww > l)
          break;
        chmax(ans, vv);
      }
      cout << ans << en;
    } else {
      vec<int> o;
      while (v) {
        if (v <= 1024) {
          calc(v - 1, l, o);
          break;
        }
        o.push_back(v - 1);
        v /= 2;
      }
    }
  }
}

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

  solve();
  // ll t;cin>>t;REP(i,t) solve();

  return 0;
}