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

#include <bits/stdc++.h>
#define REP(i, x, y) for (ll i = x; i <= y; i++)
#define BIT(t) (1ll << t)
#define PER(i, y, x) for (ll i = y; i >= x; i--)
#define vll vector<ll>
#define vvll vector<vector<ll>>
#define pll pair<ll, ll>
#define SIZE(v) ll(v.size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
using namespace std;
typedef long long ll;
//        ios::sync_with_stdio(false);
//        cin.tie(nullptr);

ll f(ll n, ll halnum, vector<map<ll, ll>> &vw, vll &v, vll &w, ll nod, ll cw) {
  if (cw < 0) {
    return -1e18;
  }
  if (cw == 0) {
    return 0;
  }
  if (nod <= halnum) {
    auto tmp = vw[nod].upper_bound(cw);
    tmp--;
    return tmp->second;
  }
  return max(f(n, halnum, vw, v, w, nod / 2, cw),
             f(n, halnum, vw, v, w, nod / 2, cw - w[nod]) + v[nod]);
}

int main() {
  ll n;
  cin >> n;
  ll dep = 1;
  while (pow(2, dep) - 1 < n) {
    dep++;
  }
  vll w(n + 1), v(n + 1);
  REP(i, 1, n) { cin >> v[i] >> w[i]; }
  ll q;
  cin >> q;
  vll qv(q + 1), ql(q + 1);
  REP(i, 1, q) { cin >> qv[i] >> ql[i]; }
  ll hal = (dep + 1) / 2;
  ll halnum = pow(2, hal) - 1;
  vector<map<ll, ll>> vw(halnum + 1);
  vw[0].insert({0, 0});
  REP(i, 1, halnum) {
    ll bef = i / 2;
    for (auto x : vw[bef]) {
      vw[i].insert(x);
    }

    for (auto x : vw[bef]) {
      ll befw = x.first;
      ll befv = x.second;
      ll curw = befw + w[i];
      ll curv = befv + v[i];
      if (vw[i].find(curw) == vw[i].end()) {
        vw[i][curw] = curv;
      } else {
        vw[i][curw] = max(vw[i][curw], curv);
      }
    }
    //     REP(i,1,halnum){
    //   cout << i << " ";
    //   for(auto x:vw[i]){
    //     cout << "("<<x.first << ", " << x.second << "), " ;
    //   }
    //   cout << endl;
    // }
    vector<ll> era;
    auto cur = vw[i].begin();
    ll befwei = -1;
    ll befval = -1;
    while (cur != vw[i].end()) {
      ll curwei = cur->first;
      ll curval = cur->second;
      if (befval >= curval) {
        // vw[i].erase(cur);
        era.push_back(curwei);
        // cout << befval << " !!! " << curwei << endl;
      } else {
        befval = curval;
        befwei = curwei;
      }
      cur++;
    }
    for (auto cur : era) {
      vw[i].erase(cur);
    }
  }
  // REP(i,1,halnum){
  //   cout << i << " ";
  //   for(auto x:vw[i]){
  //     cout << "("<<x.first << ", " << x.second << "), " ;
  //   }
  //   cout << endl;
  // }
  vll ans(q + 2);
  REP(i, 1, q) { ans[i] = f(n, halnum, vw, v, w, qv[i], ql[i]); }
  REP(i, 1, q) { cout << ans[i] << endl; }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, x, y) for (ll i = x; i <= y; i++)
#define BIT(t) (1ll << t)
#define PER(i, y, x) for (ll i = y; i >= x; i--)
#define vll vector<ll>
#define vvll vector<vector<ll>>
#define pll pair<ll, ll>
#define SIZE(v) ll(v.size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
using namespace std;
typedef long long ll;
//        ios::sync_with_stdio(false);
//        cin.tie(nullptr);

ll f(ll n, ll halnum, vector<map<ll, ll>> &vw, vll &v, vll &w, ll nod, ll cw) {
  if (cw < 0) {
    return -1e18;
  }
  if (cw == 0) {
    return 0;
  }
  if (nod <= halnum) {
    auto tmp = vw[nod].upper_bound(cw);
    tmp--;
    return tmp->second;
  }
  return max(f(n, halnum, vw, v, w, nod / 2, cw),
             f(n, halnum, vw, v, w, nod / 2, cw - w[nod]) + v[nod]);
}

int main() {
  ll n;
  cin >> n;
  ll dep = 1;
  while (pow(2, dep) - 1 < n) {
    dep++;
  }
  vll w(n + 1), v(n + 1);
  REP(i, 1, n) { cin >> v[i] >> w[i]; }
  ll q;
  cin >> q;
  vll qv(n + 1), ql(n + 1);
  REP(i, 1, q) { cin >> qv[i] >> ql[i]; }
  ll hal = (dep + 1) / 2;
  ll halnum = pow(2, hal) - 1;
  vector<map<ll, ll>> vw(halnum + 1);
  vw[0].insert({0, 0});
  REP(i, 1, halnum) {
    ll bef = i / 2;
    for (auto x : vw[bef]) {
      vw[i].insert(x);
    }

    for (auto x : vw[bef]) {
      ll befw = x.first;
      ll befv = x.second;
      ll curw = befw + w[i];
      ll curv = befv + v[i];
      if (vw[i].find(curw) == vw[i].end()) {
        vw[i][curw] = curv;
      } else {
        vw[i][curw] = max(vw[i][curw], curv);
      }
    }
    //     REP(i,1,halnum){
    //   cout << i << " ";
    //   for(auto x:vw[i]){
    //     cout << "("<<x.first << ", " << x.second << "), " ;
    //   }
    //   cout << endl;
    // }
    vector<ll> era;
    auto cur = vw[i].begin();
    ll befwei = -1;
    ll befval = -1;
    while (cur != vw[i].end()) {
      ll curwei = cur->first;
      ll curval = cur->second;
      if (befval >= curval) {
        // vw[i].erase(cur);
        era.push_back(curwei);
        // cout << befval << " !!! " << curwei << endl;
      } else {
        befval = curval;
        befwei = curwei;
      }
      cur++;
    }
    for (auto cur : era) {
      vw[i].erase(cur);
    }
  }
  // REP(i,1,halnum){
  //   cout << i << " ";
  //   for(auto x:vw[i]){
  //     cout << "("<<x.first << ", " << x.second << "), " ;
  //   }
  //   cout << endl;
  // }
  vll ans(q + 2);
  REP(i, 1, q) { ans[i] = f(n, halnum, vw, v, w, qv[i], ql[i]); }
  REP(i, 1, q) { cout << ans[i] << endl; }
}