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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll(c, d)));
#define re(c, b) for (ll c = 0; c < b; c++)
#define all(obj) (obj).begin(), (obj).end()
typedef int ll;
typedef long double ld;
using namespace std;
typedef pair<int, int> pi;

vector<vector<pi>> mp(400000, vector<pi>());
vvl item = VV(400000, 2, 0, ll); // W, V

int main(int argc, char const *argv[]) {
  ll n;
  std::cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d %d", &item[i][1], &item[i][0]);

  ll th = (1 << 12) - 1;
  for (int i = 1; i <= min(th, n); i++) {
    mp[i].push_back(pi(item[i][0], item[i][1])); // w, v
    ll par = i / 2;
    if (par == 0)
      continue;
    for (auto t : mp[par]) {
      mp[i].push_back(pi(t.first + item[i][0], t.second + item[i][1]));
      mp[i].push_back(pi(t.first, t.second));
    }
    sort(all(mp[i]), [](pi a, pi b) {
      if (a.first == b.first)
        return a.second > b.second;
      return a.first < b.first;
    });
    ll m = 0;
    for (int j = 0; j < mp[i].size(); j++) {
      m = max(m, mp[i][j].second);
      mp[i][j].second = m;
    }
  }

  for (int i = th + 1; i <= n; i++) {
    mp[i].push_back(pi(item[i][0], item[i][1])); // w, v
    ll par = i / 2;
    if (par > th) {
      for (auto t : mp[par]) {
        mp[i].push_back(pi(t.first + item[i][0], t.second + item[i][1]));
        mp[i].push_back(pi(t.first, t.second));
      }
    }
    sort(all(mp[i]), [](pi a, pi b) {
      if (a.first == b.first)
        return a.second > b.second;
      return a.first < b.first;
    });
    ll m = 0;
    for (int j = 0; j < mp[i].size(); j++) {
      m = max(m, mp[i][j].second);
      mp[i][j].second = m;
    }
  }

  ll q;
  std::cin >> q;

  for (int i = 0; i < q; i++) {
    ll u, l;
    scanf("%d %d", &u, &l);
    if (u <= th) {
      auto itr = upper_bound(all(mp[u]), pi(l, 1000000000));
      if (itr == mp[u].begin())
        std::cout << 0 << '\\n';
      else {
        itr--;
        std::cout << (*itr).second << '\\n';
      }
    } else {
      ll p = u;
      while (p > th)
        p /= 2;
      ll ans = 0;

      for (auto t : mp[u]) {
        ll w = t.first, v = t.second;
        if (w > l)
          continue;
        ans = max(ans, v);
        // l - w 以下のもの
        auto itr = upper_bound(all(mp[p]), pi(l - w, 100000000));
        if (itr == mp[p].begin())
          ans = max(ans, v);
        else {
          itr--;
          ans = max(ans, v + (*itr).second);
        }
      }
      auto itr = upper_bound(all(mp[p]), pi(l, 100000000));
      if (itr != mp[p].begin()) {
        itr--;
        ans = max(ans, (*itr).second);
      }
      std::cout << ans << '\\n';
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll(c, d)));
#define re(c, b) for (ll c = 0; c < b; c++)
#define all(obj) (obj).begin(), (obj).end()
typedef int ll;
typedef long double ld;
using namespace std;
typedef pair<int, int> pi;

vector<vector<pi>> mp(400000, vector<pi>());
vvl item = VV(400000, 2, 0, ll); // W, V

int main(int argc, char const *argv[]) {
  ll n;
  std::cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d %d", &item[i][1], &item[i][0]);

  ll th = (1 << 12) - 1;
  for (int i = 1; i <= min(th, n); i++) {
    mp[i].push_back(pi(item[i][0], item[i][1])); // w, v
    ll par = i / 2;
    if (par == 0)
      continue;
    for (auto t : mp[par]) {
      mp[i].push_back(pi(t.first + item[i][0], t.second + item[i][1]));
      mp[i].push_back(pi(t.first, t.second));
    }
    sort(all(mp[i]), [](pi a, pi b) {
      if (a.first == b.first)
        return a.second > b.second;
      return a.first < b.first;
    });
    ll m = 0;
    for (int j = 0; j < mp[i].size(); j++) {
      m = max(m, mp[i][j].second);
      mp[i][j].second = m;
    }
  }

  for (int i = th + 1; i <= n; i++) {
    mp[i].push_back(pi(item[i][0], item[i][1])); // w, v
    ll par = i / 2;
    if (par <= th)
      continue;
    for (auto t : mp[par]) {
      mp[i].push_back(pi(t.first + item[i][0], t.second + item[i][1]));
      mp[i].push_back(pi(t.first, t.second));
    }
    sort(all(mp[i]), [](pi a, pi b) {
      if (a.first == b.first)
        return a.second > b.second;
      return a.first < b.first;
    });
    ll m = 0;
    for (int j = 0; j < mp[i].size(); j++) {
      m = max(m, mp[i][j].second);
      mp[i][j].second = m;
    }
  }

  ll q;
  std::cin >> q;
  for (int i = 0; i < q; i++) {
    ll u, l;
    scanf("%d %d", &u, &l);
    if (u <= th) {
      auto itr = upper_bound(all(mp[u]), pi(l, 1000000000));
      if (itr == mp[u].begin())
        std::cout << 0 << '\\n';
      else {
        itr--;
        std::cout << (*itr).second << '\\n';
      }
    } else {
      /*
      ll p = u;
      while(p>th) p/=2;
      ll ans = 0;
      for(auto t:mp[u]){
        ll w = t.first, v = t.second;
        if(w>l) continue;
        // l - w 以下のもの
        auto itr = upper_bound(all(mp[p]), pi(l-w, 1000000000));

        if(itr==mp[p].begin()) ans = max(ans, v);
        else{
          itr--;
          ans = max(ans, v + (*itr).second);
        }
      }
      std::cout << ans << '\\n';
      */
      exit(1);
    }
  }
  return 0;
}