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

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

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD 1000000007
#define P pair<ll, ll>

ll n, v[300000], w[300000];
vector<ll> dat[300000];
vector<P> r[300000];

bool solve(ll now, ll index, ll key) { // 条件判定
  if (r[now][index].first <= key)
    return true; // ここに条件を入れる
  else
    return false;
}

ll nibutan(ll now, ll key = 0) { // ちゃろー！

  ll ng = r[now].size(); // 最大値ならs.size()
  ll ok = -1;            // 最大値なら0

  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    if (solve(now, mid, key))
      ok = mid;
    else
      ng = mid;
  }
  return ok; // 満たさない最大ならng
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  REP(i, n) { cin >> v[i] >> w[i]; }
  REP(i, min(n, 1023ll)) {
    if (i == 0) {
      r[i].push_back(P(0, 0));
      r[i].push_back(P(w[i], v[i]));
    } else {
      ll p = (i - 1) / 2;
      REP(j, r[p].size()) {
        P now = r[p][j];
        r[i].push_back(P(now.first, now.second));
        if (now.first + w[i] <= 100010)
          r[i].push_back(P(now.first + w[i], now.second + v[i]));
      }
      sort(ALL(r[i]));
    }
    REP(j, r[i].size()) {
      dat[i].resize(r[i].size() + 1);
      dat[i][j] = max(dat[i][j], r[i][j].second);
      dat[i][j + 1] = dat[i][j];
      // cout << i << " " << j << " " << r[i][j].second << endl;
    }
  }
  ll q;
  cin >> q;
  REP(qq, q) {
    ll a, l;
    cin >> a >> l;
    a--;
    ll ans = 0;
    if (a < 1023) {
      ll pos = nibutan(a, l);
      // cout << a << " "<<  pos << " " << dat[a][pos] << " !" << endl;
      ans = max(ans, dat[a][pos]);
    } else {
      vector<P> s;
      ll now = a;
      while (1) {
        s.push_back(P(w[now], v[now]));
        now = (now - 1) / 2;
        if (now < 1023)
          break;
      }
      ll ns = s.size();
      REP(i, 1 << ns) {
        ll nl = 0, nv = 0;
        REP(j, ns) {
          if (i & (1 << j)) {
            nl += s[j].first;
            nv += s[j].second;
          }
        }
        if (nl > l)
          continue;
        ll pos = nibutan(now, l - nl);
        ans = max(ans, dat[now][pos] + nv);
      }
    }
    cout << ans << "\\n";
  }
}


Error: 0

Fixed submission:

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

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD 1000000007
#define P pair<ll, ll>

ll n, v[300000], w[300000], dep[300000];
vector<ll> g[300000], dat[300000];
vector<P> r[300000];

bool solve(ll now, ll index, ll key) { // 条件判定
  if (r[now][index].first <= key)
    return true; // ここに条件を入れる
  else
    return false;
}

ll nibutan(ll now, ll key = 0) { // ちゃろー！

  ll ng = r[now].size(); // 最大値ならs.size()
  ll ok = -1;            // 最大値なら0

  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    if (solve(now, mid, key))
      ok = mid;
    else
      ng = mid;
  }
  return ok; // 満たさない最大ならng
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  REP(i, n) {
    if (i > 0) {
      g[(i - 1) / 2].push_back(i);
      g[i].push_back((i - 1) / 2);
    }
    cin >> v[i] >> w[i];
  }
  REP(i, min(n, 1023ll)) {
    if (i == 0) {
      r[i].push_back(P(0, 0));
      r[i].push_back(P(w[i], v[i]));
    } else {
      ll p = (i - 1) / 2;
      REP(j, r[p].size()) {
        P now = r[p][j];
        r[i].push_back(P(now.first, now.second));
        if (now.first + w[i] <= 100010)
          r[i].push_back(P(now.first + w[i], now.second + v[i]));
      }
      sort(ALL(r[i]));
    }
    REP(j, r[i].size()) {
      dat[i].resize(r[i].size() + 1);
      dat[i][j] = max(dat[i][j], r[i][j].second);
      dat[i][j + 1] = dat[i][j];
      // cout << i << " " << j << " " << r[i][j].second << endl;
    }
  }
  ll q;
  cin >> q;
  REP(qq, q) {
    ll a, l;
    cin >> a >> l;
    a--;
    ll ans = 0;
    if (a < 1023) {
      ll pos = nibutan(a, l);
      // cout << a << " "<<  pos << " " << dat[a][pos] << " !" << endl;
      ans = max(ans, dat[a][pos]);
    } else {
      vector<P> s;
      ll now = a;
      while (1) {
        s.push_back(P(w[a], v[a]));
        now = (now - 1) / 2;
        if (dep[now] <= 9)
          break;
      }
      ll ns = s.size();
      REP(i, 1 << ns) {
        ll nl = 0, nv = 0;
        REP(j, ns) {
          if (i & (1 << j)) {
            nl += w[i];
            nv += v[i];
          }
        }
        if (nl > l)
          continue;
        ll pos = nibutan(now, l - nl);
        ans = max(ans, dat[now][pos] + nv);
      }
    }
    cout << ans << "\\n";
  }
}