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

#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define mt make_tuple
#define MOD 1000000007
#define fo(i, a, b) for (i = a; i < b; i++)
#define foe(i, a, b) for (i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<long long int>
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<long long int, long long int>>
#define boost                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
using namespace std;
const int inf = 1e9 + 5;
const ll inf64 = 1e18 + 5;

const int MAX = 1 << 18;
const int N = 1100;
const int L = 10;

int lvl[MAX];
vector<pii> pre[N];
vector<pii> a;
void rec(int x, int l, vector<pii> v) {
  if (x >= a.size())
    return;
  v.pb({a[x].f, a[x].s});
  if (l == L) {
    for (int i = 0; i < (1 << L); i++) {
      int ww = 0, vv = 0;
      for (int j = 0; j < L; j++) {
        if ((1 << j) & i) {
          ww += v[j].s;
          vv += v[j].f;
        }
      }
      pre[x].pb({ww, vv});
    }
    sort(all(pre[x]), [&](const pii p1, const pii p2) {
      if (p1.f == p2.f)
        return p1.s < p2.s;
      return p1.f < p2.f;
    });
    for (int i = 1; i < pre[x].size(); i++)
      pre[x][i].s = max(pre[x][i].s, pre[x][i - 1].s);
    return;
  }
  rec(2 * x, l + 1, v);
  rec(2 * x + 1, l + 1, v);
}
int main() {
  boost;
  int n, q, x, y, i;
  cin >> n;
  a.pb({0, 0});

  lvl[1] = 1;
  foe(i, 2, n) lvl[i] = lvl[i / 2] + 1;

  foe(i, 1, n) {
    cin >> x >> y;
    a.pb({x, y});
  }
  rec(1, 1, {});
  cin >> q;
  while (q--) {
    int val, W;
    cin >> val >> W;
    vector<int> v;
    int ans = 0;
    if (lvl[val] < L) {
      do {
        v.pb(val);
        val /= 2;
      } while (val);

      int x = v.size();
      for (int i = 0; i < (1 << x); i++) {
        int ww = 0, vv = 0;
        for (int j = 0; j < x; j++) {
          if ((1 << j) & i) {
            ww += a[v[j]].s;
            vv += a[v[j]].f;
          }
        }
        if (ww <= W)
          ans = max(ans, vv);
      }
    } else {
      while (lvl[val] > L) {
        v.pb(val);
        val /= 2;
      }
      int node = val;
      int x = v.size();
      for (int i = 0; i < (1 << x); i++) {
        int ww = 0, vv = 0;
        for (int j = 0; j < x; j++) {
          if ((1 << j) & i) {
            ww += a[v[j]].s;
            vv += a[v[j]].f;
          }
        }
        if (ww <= W) {
          pii p = {W - ww, inf};
          ans = max(ans, vv);
          int ind = upper_bound(all(pre[node]), p) - pre[node].begin();
          --ind;
          if (ind >= 0)
            ans = max(ans, vv + pre[node][ind].s);
        }
      }
    }
    cout << ans << '\\n';
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define mt make_tuple
#define MOD 1000000007
#define fo(i, a, b) for (i = a; i < b; i++)
#define foe(i, a, b) for (i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<long long int>
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<long long int, long long int>>
#define boost                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
using namespace std;
const int inf = 1e9 + 5;
const ll inf64 = 1e18 + 5;

const int MAX = 1 << 18;
const int N = 550;
const int L = 10;

int lvl[MAX];
vector<pii> pre[N];
vector<pii> a;
void rec(int x, int l, vector<pii> v) {
  if (x >= a.size())
    return;
  v.pb({a[x].f, a[x].s});
  if (l == L) {
    for (int i = 0; i < (1 << L); i++) {
      int ww = 0, vv = 0;
      for (int j = 0; j < L; j++) {
        if ((1 << j) & i) {
          ww += v[j].s;
          vv += v[j].f;
        }
      }
      pre[x].pb({ww, vv});
    }
    sort(all(pre[x]), [&](const pii p1, const pii p2) {
      if (p1.f == p2.f)
        return p1.s < p2.s;
      return p1.f < p2.f;
    });
    for (int i = 1; i < pre[x].size(); i++)
      pre[x][i].s = max(pre[x][i].s, pre[x][i - 1].s);
    return;
  }
  rec(2 * x, l + 1, v);
  rec(2 * x + 1, l + 1, v);
}
int main() {
  boost;
  int n, q, x, y, i;
  cin >> n;
  a.pb({0, 0});

  lvl[1] = 1;
  foe(i, 2, n) lvl[i] = lvl[i / 2] + 1;

  foe(i, 1, n) {
    cin >> x >> y;
    a.pb({x, y});
  }
  rec(1, 1, {});
  cin >> q;
  while (q--) {
    int val, W;
    cin >> val >> W;
    vector<int> v;
    int ans = 0;
    if (lvl[val] < L) {
      do {
        v.pb(val);
        val /= 2;
      } while (val);

      int x = v.size();
      for (int i = 0; i < (1 << x); i++) {
        int ww = 0, vv = 0;
        for (int j = 0; j < x; j++) {
          if ((1 << j) & i) {
            ww += a[v[j]].s;
            vv += a[v[j]].f;
          }
        }
        if (ww <= W)
          ans = max(ans, vv);
      }
    } else {
      while (lvl[val] > L) {
        v.pb(val);
        val /= 2;
      }
      int node = val;
      int x = v.size();
      for (int i = 0; i < (1 << x); i++) {
        int ww = 0, vv = 0;
        for (int j = 0; j < x; j++) {
          if ((1 << j) & i) {
            ww += a[v[j]].s;
            vv += a[v[j]].f;
          }
        }
        if (ww <= W) {
          pii p = {W - ww, inf};
          ans = max(ans, vv);
          int ind = upper_bound(all(pre[node]), p) - pre[node].begin();
          --ind;
          if (ind >= 0)
            ans = max(ans, vv + pre[node][ind].s);
        }
      }
    }
    cout << ans << '\\n';
  }
}