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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
typedef pair<lint, lint> pl;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
// 問題文および制約はちゃんと確認しよう！
// サイズは10^5じゃなくて2×10^5とかかもしれないし、重要な制約・条件を見落としているかも
// とりあえずサンプルを読んでから解法を考えよう？
ostream &operator<<(ostream &os, const pl &a) {
  os << "(" << a.fi << "," << a.se << ")";
  return os;
}
ostream &operator<<(ostream &os, const vector<pl> &a) {
  os << "[";
  rep(i, a.size()) {
    os << a[i];
    if (i < a.size() - 1)
      os << ",";
  }
  os << "]";
  return os;
}
#define D 10
vector<pl> dp[300100];
lint V[300100], W[300100];
int main() {
  int n, bo = (1 << D) - 1;
  cin >> n;
  rep(i, n) { cin >> V[i] >> W[i]; }
  dp[0].pb(mp(0, 0));
  dp[0].pb(mp(W[0], V[0]));
  REP(i, 1, min(n, bo)) {
    vector<pl> v;
    int r = (i - 1) / 2;
    rep(j, dp[r].size()) {
      v.pb(dp[r][j]);
      v.pb(mp(dp[r][j].fi + W[i], dp[r][j].se + V[i]));
    }
    sort(All(v));
    // cout<<i<<' '<<v<<endl;
    dp[i].pb(v[0]);
    lint ma = 0;
    rep(j, v.size() - 1) {
      if (v[j + 1].se > ma) {
        dp[i].pb(v[j + 1]);
        ma = v[j + 1].se;
      }
    }
    rep(j, dp[i].size() - 1) {
      assert(dp[i][j].fi <= dp[i][j + 1].fi && dp[i][j].se <= dp[i][j + 1].se);
    }
    // cout<<i<<' '<<dp[i]<<endl;
  }
  // rep(i,n) cout<<i<<' '<<dp[i]<<endl;
  int q, a, b;
  cin >> q;
  rep(i, q) {
    cin >> a >> b;
    a--;
    vector<pl> v;
    v.pb(mp(0, 0));
    while (a >= bo) {
      /*rep(j,dp[a].size()){
              v.pb(dp[a][j]);v.pb(mp(dp[a][j].fi+W[i],dp[a][j].se+V[i]));
      }*/
      int len = v.size();
      rep(j, len) v.pb(mp(v[j].fi + W[a], v[j].se + V[a]));
      a--;
      a /= 2;
    }
    lint ret = 0;
    rep(j, v.size()) {
      lint w = b - v[j].fi;
      if (w < 0LL)
        continue;
      int it = lower_bound(All(dp[a]), mp(w + 1, -1LL)) - dp[a].begin();
      it--;
      ret = max(ret, v[j].se + dp[a][it].se);
    }
    cout << ret << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
typedef pair<lint, lint> pl;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
// 問題文および制約はちゃんと確認しよう！
// サイズは10^5じゃなくて2×10^5とかかもしれないし、重要な制約・条件を見落としているかも
// とりあえずサンプルを読んでから解法を考えよう？
ostream &operator<<(ostream &os, const pl &a) {
  os << "(" << a.fi << "," << a.se << ")";
  return os;
}
ostream &operator<<(ostream &os, const vector<pl> &a) {
  os << "[";
  rep(i, a.size()) {
    os << a[i];
    if (i < a.size() - 1)
      os << ",";
  }
  os << "]";
  return os;
}
#define D 9
vector<pl> dp[100100];
lint V[100100], W[100100];
int main() {
  int n, bo = (1 << D) - 1;
  cin >> n;
  rep(i, n) { cin >> V[i] >> W[i]; }
  dp[0].pb(mp(0, 0));
  dp[0].pb(mp(W[0], V[0]));
  REP(i, 1, min(n, bo)) {
    vector<pl> v;
    int r = (i - 1) / 2;
    rep(j, dp[r].size()) {
      v.pb(dp[r][j]);
      v.pb(mp(dp[r][j].fi + W[i], dp[r][j].se + V[i]));
    }
    sort(All(v));
    // cout<<i<<' '<<v<<endl;
    dp[i].pb(v[0]);
    lint ma = 0;
    rep(j, v.size() - 1) {
      if (v[j + 1].se > ma) {
        dp[i].pb(v[j + 1]);
        ma = v[j + 1].se;
      }
    }
    // cout<<i<<' '<<dp[i]<<endl;
  }
  // rep(i,n) cout<<i<<' '<<dp[i]<<endl;
  int q, a, b;
  cin >> q;
  rep(i, q) {
    cin >> a >> b;
    a--;
    vector<pl> v;
    v.pb(mp(0, 0));
    while (a >= bo) {
      rep(j, dp[a].size()) {
        v.pb(dp[a][j]);
        v.pb(mp(dp[a][j].fi + W[i], dp[a][j].se + V[i]));
      }
      a--;
      a /= 2;
    }
    lint ret = 0;
    rep(j, v.size()) {
      lint w = b - v[j].fi;
      if (w < 0LL)
        continue;
      int it = lower_bound(All(dp[a]), mp(w + 1, -1LL)) - dp[a].begin();
      it--;
      ret = max(ret, v[j].se + dp[a][it].se);
    }
    cout << ret << endl;
  }
}