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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define rep(i, n) for (lint i = 0; i < n; i++)
#define repr(i, n) for (lint i = n - 1; i >= 0; i--)
#define repi(i, ini, n) for (lint i = ini; i < n; i++)
#define repir(i, ini, n) for (lint i = n - 1; i >= ini; i--)
#define repb(i, start, end) for (lint i = start; i <= end; i++)
#define repbr(i, start, end) for (lint i = end; i >= start; i--)

#define bit(n) (1LL << (n))

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rg(v, ini, end) v.begin() + ini, v.begin() + end
#define rrg(v, ini, end) v.rbegin() + ini, v.rbegin() + end

#define ret return 0;

namespace {

using lint = long long;
using ulint = unsigned long long;
using ld = long double;
struct xy {
  lint x, y;
  xy() : x(0), y(0) {}
  xy(lint _x, lint _y) : x(_x), y(_y) {}
  xy operator+(const xy &p) const { return xy(x + p.x, y + p.y); }
  bool operator<(xy p) const {
    if (y == p.y)
      return x < p.x;
    return y < p.y;
  }
};
struct xyd {
  ld x, y;
  xyd() : x(0), y(0) {}
  xyd(long double _x, long double _y) : x(_x), y(_y) {}
};
using vec = vector<lint>;
using vecd = vector<ld>;
using vecs = vector<string>;
using vecp = vector<xy>;
template <class T> using vect = vector<T>;
class vec2 : public vector<vector<lint>> {
public:
  vec2() {}
  vec2(lint n) : vector(n) {}
  vec2(lint h, lint w) : vector(h, vector<lint>(w)) {}
  vec2(lint h, lint w, lint v) : vector(h, vector<lint>(w, v)) {}
};

template <class T> using priq = priority_queue<T>;
template <class T> using rpriq = priority_queue<T, vector<T>, greater<T>>;
template <class Key, class Val> using hashmap = unordered_map<Key, Val>;
template <class Key> using hashset = unordered_set<Key>;

mt19937 mtrand((random_device())());

const double pi = 3.141592653589793238462;
const lint intmax = 9223372036854775807;
const lint inf = 1100100100100100100LL;

template <class T> constexpr auto msum(T arg0) { return arg0; }

template <class T, class... Types> constexpr auto msum(T arg0, Types... args) {
  static_assert(sizeof...(args) > 0, "arg err");

  return arg0 + msum(args...);
}

template <class It> constexpr typename It::value_type mmax(It begin, It end) {
  return *max_element(begin, end);
}

template <class It> constexpr typename It::value_type mmin(It begin, It end) {
  return *min_element(begin, end);
}

template <class T> constexpr auto mmax(T arg) { return arg; }

template <class T, class... Types> constexpr auto mmax(T arg0, Types... args) {
  using promote_t = decltype(msum(arg0, args...));

  return max(static_cast<promote_t>(arg0),
             static_cast<promote_t>(mmax(args...)));
}

template <class T> constexpr auto mmin(T arg) { return arg; }

template <class T, class... Types> constexpr auto mmin(T arg0, Types... args) {
  using promote_t = decltype(msum(arg0, args...));

  return min(static_cast<promote_t>(arg0),
             static_cast<promote_t>(mmin(args...)));
}

template <class T> struct Mplus {
  T operator()(const T &a, const T &b) { return a + b; }

  const static T id = T(0);
};

template <class It, class T = typename It::value_type, class Monoid = Mplus<T>>
T acc(It begin, It end, Monoid f = Mplus<T>()) {
  return accumulate(begin, end, Monoid::id, f);
}

constexpr auto clamp(lint val, lint left, lint right) {
  return mmax(mmin(val, right), left);
}

constexpr lint div2(lint p, lint q) { return (p + q - 1) / q; }

#if (__cplusplus < 201703L)

lint gcd(lint a, lint b) {
  while (1) {
    if (a < b)
      swap(a, b);
    if (!b)
      break;
    a %= b;
  }
  return a;
}

lint lcm(lint a, lint b) { return a / gcd(a, b) * b; }

#endif

constexpr lint powInt(lint a, lint b) {
  if (b == 0) {
    return 1;
  }
  if (b == 1) {
    return a;
  }
  lint tmp = powInt(a, b / 2);
  return (b % 2 == 1 ? a * tmp * tmp : tmp * tmp);
}

template <class T> T sgn(T val) {
  if (val == T(0))
    return T(0);
  if (val < 0)
    return T(-1);
  if (val > 0)
    return T(1);
}

template <class T> constexpr auto modK_belowN(T k, T MOD, T n) {
  return (n + MOD - k - 1) / MOD;
}

template <class It, class T> bool exist(It begin, It end, const T &val) {
  return find(begin, end, val) != end;
}

template <class It, class Pr> bool exist_if(It begin, It end, Pr pred) {
  return find_if(begin, end, pred) != end;
}

template <class T> bool between(T val, T l, T r) {
  return (val >= l) && (val <= r);
}

lint extGCD(lint a, lint b, lint &x, lint &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  lint d = extGCD(b, a % b, y, x);
  y -= a / b * x;
  return d;
}

template <class It> auto carr(It begin, It end) {
  vec c;
  c.push_back(1);
  auto before = *begin;
  for (auto it = begin + 1; it != end; it++) {
    if (before != *it) {
      c.push_back(0);
      before = *it;
    }
    c.back()++;
  }
  return c;
}

template <class T> struct nval {
  lint n;
  T val;
  nval() : n(0){};
  nval(lint _n, T _val) : n(_n), val(_val){};
};

template <class It> auto carr2(It begin, It end) {
  using T = nval<remove_reference_t<decltype(*begin)>>;

  vect<T> c;
  c.push_back(T(1, *begin));
  auto before = *begin;
  for (auto it = begin + 1; it != end; it++) {
    if (before != *it) {
      c.push_back(T(0, *it));
      before = *it;
    }
    c.back().n++;
  }
  return c;
}

template <class It, class... T> void sort2(It begin, It end, T... p) {
  using val_t = remove_reference_t<decltype(*begin)>;

  sort(begin, end, [p...](const val_t &a, const val_t &b) {
    bool neq[] = {(a.*p != b.*p)...};
    bool sg[] = {(a.*p < b.*p)...};

    rep(i, sizeof...(p)) {
      if (neq[i]) {
        return sg[i];
      }
    }
    return false;
  });
}

template <class It, class It2> auto spacel(It i, It2 end) {
  if (i + 1 == end) {
    return '\\n';
  } else {
    return ' ';
  }
}

bool match(string s, string r) { return regex_match(s, regex(r)); }

ostream &setp(ostream &ost) {
  cout << setprecision(60) << fixed;
  return ost;
}

#ifdef _LOCAL
auto &dbg = cout;
#else
struct dummy_cout {
  template <class T> dummy_cout &operator<<(T &&op) { return *this; }

  using endl_t = basic_ostream<char, char_traits<char>>;

  dummy_cout &operator<<(endl_t &(*)(endl_t &)) { return *this; }
};

dummy_cout dbg;
#endif

const ulint mod = 1000000007;
// const ulint mod = 998244353;
const ld eps = 0.0000001;
}; // namespace

struct Q {
  lint i, v, l;
};

lint n;
lint v[300000], w[300000];
vect<Q> qq[300000];
bool qe[300000];
lint ans[100010];

lint dp[20][200010];

bool dfs_qe(lint pos) {
  if (pos >= n)
    return false;
  qe[pos] |= dfs_qe(pos * 2 + 1) | dfs_qe(pos * 2 + 2);
  return qe[pos];
}

void dfs_q(lint pos, lint d) {
  const lint db = 9;
  if (d <= db) {
    repb(i, 0, 100000) { dp[d][i] = 0; }
    repb(i, 0, 100000) {
      if (i >= w[pos])
        dp[d][i] = mmax(dp[d - 1][i], dp[d - 1][i - w[pos]] + v[pos]);
      else
        dp[d][i] = dp[d - 1][i];
    }
    rep(i, qq[pos].size()) { ans[qq[pos][i].i] = dp[d][qq[pos][i].l]; }
  } else if (qq[pos].size() > 0) {
    rep(qqi, qq[pos].size()) {
      rep(ptn, bit(d - db)) {
        lint wt = 0, vt = 0, pos2 = pos;
        rep(i, d - db) {
          if (ptn & bit(i)) {
            wt += w[pos2];
            vt += v[pos2];
          }
          pos2 = (pos2 - 1) / 2;
        }

        if (0 <= qq[pos][qqi].l - wt)
          ans[qq[pos][qqi].i] =
              mmax(ans[qq[pos][qqi].i], dp[db][qq[pos][qqi].l - wt] + vt);
      }
    }
  }

  if (pos * 2 + 1 < n && qe[pos * 2 + 1])
    dfs_q(pos * 2 + 1, d + 1);
  if (pos * 2 + 2 < n && qe[pos * 2 + 2])
    dfs_q(pos * 2 + 2, d + 1);
}

int main() {
  cin >> n;
  rep(i, n) cin >> v[i] >> w[i];

  lint q;
  cin >> q;
  rep(i, q) {
    lint qv, ql;
    cin >> qv >> ql;
    qv--;
    qq[qv].push_back(Q{i, qv, ql});
    qe[qv] = true;
  }

  dfs_qe(0);
  dfs_q(0, 1);

  rep(i, q) { cout << ans[i] << endl; }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define rep(i, n) for (lint i = 0; i < n; i++)
#define repr(i, n) for (lint i = n - 1; i >= 0; i--)
#define repi(i, ini, n) for (lint i = ini; i < n; i++)
#define repir(i, ini, n) for (lint i = n - 1; i >= ini; i--)
#define repb(i, start, end) for (lint i = start; i <= end; i++)
#define repbr(i, start, end) for (lint i = end; i >= start; i--)

#define bit(n) (1LL << (n))

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rg(v, ini, end) v.begin() + ini, v.begin() + end
#define rrg(v, ini, end) v.rbegin() + ini, v.rbegin() + end

#define ret return 0;

namespace {

using lint = long long;
using ulint = unsigned long long;
using ld = long double;
struct xy {
  lint x, y;
  xy() : x(0), y(0) {}
  xy(lint _x, lint _y) : x(_x), y(_y) {}
  xy operator+(const xy &p) const { return xy(x + p.x, y + p.y); }
  bool operator<(xy p) const {
    if (y == p.y)
      return x < p.x;
    return y < p.y;
  }
};
struct xyd {
  ld x, y;
  xyd() : x(0), y(0) {}
  xyd(long double _x, long double _y) : x(_x), y(_y) {}
};
using vec = vector<lint>;
using vecd = vector<ld>;
using vecs = vector<string>;
using vecp = vector<xy>;
template <class T> using vect = vector<T>;
class vec2 : public vector<vector<lint>> {
public:
  vec2() {}
  vec2(lint n) : vector(n) {}
  vec2(lint h, lint w) : vector(h, vector<lint>(w)) {}
  vec2(lint h, lint w, lint v) : vector(h, vector<lint>(w, v)) {}
};

template <class T> using priq = priority_queue<T>;
template <class T> using rpriq = priority_queue<T, vector<T>, greater<T>>;
template <class Key, class Val> using hashmap = unordered_map<Key, Val>;
template <class Key> using hashset = unordered_set<Key>;

mt19937 mtrand((random_device())());

const double pi = 3.141592653589793238462;
const lint intmax = 9223372036854775807;
const lint inf = 1100100100100100100LL;

template <class T> constexpr auto msum(T arg0) { return arg0; }

template <class T, class... Types> constexpr auto msum(T arg0, Types... args) {
  static_assert(sizeof...(args) > 0, "arg err");

  return arg0 + msum(args...);
}

template <class It> constexpr typename It::value_type mmax(It begin, It end) {
  return *max_element(begin, end);
}

template <class It> constexpr typename It::value_type mmin(It begin, It end) {
  return *min_element(begin, end);
}

template <class T> constexpr auto mmax(T arg) { return arg; }

template <class T, class... Types> constexpr auto mmax(T arg0, Types... args) {
  using promote_t = decltype(msum(arg0, args...));

  return max(static_cast<promote_t>(arg0),
             static_cast<promote_t>(mmax(args...)));
}

template <class T> constexpr auto mmin(T arg) { return arg; }

template <class T, class... Types> constexpr auto mmin(T arg0, Types... args) {
  using promote_t = decltype(msum(arg0, args...));

  return min(static_cast<promote_t>(arg0),
             static_cast<promote_t>(mmin(args...)));
}

template <class T> struct Mplus {
  T operator()(const T &a, const T &b) { return a + b; }

  const static T id = T(0);
};

template <class It, class T = typename It::value_type, class Monoid = Mplus<T>>
T acc(It begin, It end, Monoid f = Mplus<T>()) {
  return accumulate(begin, end, Monoid::id, f);
}

constexpr auto clamp(lint val, lint left, lint right) {
  return mmax(mmin(val, right), left);
}

constexpr lint div2(lint p, lint q) { return (p + q - 1) / q; }

#if (__cplusplus < 201703L)

lint gcd(lint a, lint b) {
  while (1) {
    if (a < b)
      swap(a, b);
    if (!b)
      break;
    a %= b;
  }
  return a;
}

lint lcm(lint a, lint b) { return a / gcd(a, b) * b; }

#endif

constexpr lint powInt(lint a, lint b) {
  if (b == 0) {
    return 1;
  }
  if (b == 1) {
    return a;
  }
  lint tmp = powInt(a, b / 2);
  return (b % 2 == 1 ? a * tmp * tmp : tmp * tmp);
}

template <class T> T sgn(T val) {
  if (val == T(0))
    return T(0);
  if (val < 0)
    return T(-1);
  if (val > 0)
    return T(1);
}

template <class T> constexpr auto modK_belowN(T k, T MOD, T n) {
  return (n + MOD - k - 1) / MOD;
}

template <class It, class T> bool exist(It begin, It end, const T &val) {
  return find(begin, end, val) != end;
}

template <class It, class Pr> bool exist_if(It begin, It end, Pr pred) {
  return find_if(begin, end, pred) != end;
}

template <class T> bool between(T val, T l, T r) {
  return (val >= l) && (val <= r);
}

lint extGCD(lint a, lint b, lint &x, lint &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  lint d = extGCD(b, a % b, y, x);
  y -= a / b * x;
  return d;
}

template <class It> auto carr(It begin, It end) {
  vec c;
  c.push_back(1);
  auto before = *begin;
  for (auto it = begin + 1; it != end; it++) {
    if (before != *it) {
      c.push_back(0);
      before = *it;
    }
    c.back()++;
  }
  return c;
}

template <class T> struct nval {
  lint n;
  T val;
  nval() : n(0){};
  nval(lint _n, T _val) : n(_n), val(_val){};
};

template <class It> auto carr2(It begin, It end) {
  using T = nval<remove_reference_t<decltype(*begin)>>;

  vect<T> c;
  c.push_back(T(1, *begin));
  auto before = *begin;
  for (auto it = begin + 1; it != end; it++) {
    if (before != *it) {
      c.push_back(T(0, *it));
      before = *it;
    }
    c.back().n++;
  }
  return c;
}

template <class It, class... T> void sort2(It begin, It end, T... p) {
  using val_t = remove_reference_t<decltype(*begin)>;

  sort(begin, end, [p...](const val_t &a, const val_t &b) {
    bool neq[] = {(a.*p != b.*p)...};
    bool sg[] = {(a.*p < b.*p)...};

    rep(i, sizeof...(p)) {
      if (neq[i]) {
        return sg[i];
      }
    }
    return false;
  });
}

template <class It, class It2> auto spacel(It i, It2 end) {
  if (i + 1 == end) {
    return '\\n';
  } else {
    return ' ';
  }
}

bool match(string s, string r) { return regex_match(s, regex(r)); }

ostream &setp(ostream &ost) {
  cout << setprecision(60) << fixed;
  return ost;
}

#ifdef _LOCAL
auto &dbg = cout;
#else
struct dummy_cout {
  template <class T> dummy_cout &operator<<(T &&op) { return *this; }

  using endl_t = basic_ostream<char, char_traits<char>>;

  dummy_cout &operator<<(endl_t &(*)(endl_t &)) { return *this; }
};

dummy_cout dbg;
#endif

const ulint mod = 1000000007;
// const ulint mod = 998244353;
const ld eps = 0.0000001;
}; // namespace

struct Q {
  lint i, v, l;
};

lint n;
lint v[300000], w[300000];
vect<Q> qq[300000];
bool qe[300000];
lint ans[100010];

lint dp[20][200010];

bool dfs_qe(lint pos) {
  if (pos >= n)
    return false;
  qe[pos] |= dfs_qe(pos * 2 + 1) | dfs_qe(pos * 2 + 2);
  return qe[pos];
}

void dfs_q(lint pos, lint d) {
  const lint db = 9;
  if (d <= db) {
    repb(i, 0, 100000) { dp[d][i] = 0; }
    repb(i, 0, 100000) {
      if (i >= w[pos])
        dp[d][i] = mmax(dp[d - 1][i], dp[d - 1][i - w[pos]] + v[pos]);
      else
        dp[d][i] = dp[d - 1][i];
    }
    rep(i, qq[pos].size()) { ans[qq[pos][i].i] = dp[d][qq[pos][i].l]; }
  } else if (qq[pos].size()) {
    rep(qqi, qq[pos].size()) {
      rep(ptn, bit(d - db)) {
        lint wt = 0, vt = 0, pos2 = pos;
        rep(i, d - db) {
          if (ptn & bit(i)) {
            wt += w[pos2];
            vt += v[pos2];
          }
          pos2 = (pos2 - 1) / 2;
        }

        if (wt <= qq[pos][qqi].l)
          ans[qq[pos][qqi].i] =
              mmax(ans[qq[pos][qqi].i], dp[db][qq[pos][qqi].l - wt] + vt);
      }
    }
  }

  if (qe[pos * 2 + 1])
    dfs_q(pos * 2 + 1, d + 1);
  if (qe[pos * 2 + 2])
    dfs_q(pos * 2 + 2, d + 1);
}

int main() {
  cin >> n;
  rep(i, n) cin >> v[i] >> w[i];

  lint q;
  cin >> q;
  rep(i, q) {
    lint qv, ql;
    cin >> qv >> ql;
    qv--;
    qq[qv].push_back(Q{i, qv, ql});
    qe[qv] = true;
  }

  dfs_qe(0);
  dfs_q(0, 1);

  rep(i, q) { cout << ans[i] << endl; }

  return 0;
}