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

#include <bits/stdc++.h>

using namespace std;

using ll = long long int;
using vi = vector<int>;
#define f(n) for (int i = 0; i < n; i++)
#define fj(n) for (int j = 0; j < n; j++)
#define fk(n) for (int k = 0; k < n; k++)
#define ci(n) cin >> n;
#define ic(n)                                                                  \\
  int n;                                                                       \\
  cin >> n;
#define lc(n)                                                                  \\
  int n;                                                                       \\
  cin >> n;
#define all(a) a.begin(), a.end()
#define pri(a, n)                                                              \\
  for (int i = 0; i < n; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << "\\n";
#define sc(a, n)                                                               \\
  int a[n] = {0};                                                              \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];
#define scl(a, n)                                                              \\
  ll a[n] = {0};                                                               \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];
#define pb push_back
#define eb emplace_back
// #define int long long
const int inf = 2e9, mxp = 10, maxn = (1 << 18);

struct el {
  int c, w;
  el(int _c = 0, int _w = 0) : c(_c), w(_w) {}
};
bool operator<(el a, el b) { return a.w < b.w; }
vector<el> els[(1 << mxp) + 10];
vector<int> pr[maxn];
int c[maxn], w[maxn], n;
void DPFS(int v, int d) {
  // for(el e : els[v / 2]) t.pb(e);
  // cout << v << '\\n';
  if (v > n)
    return;
  vector<el> t = els[v / 2];
  vector<el> t2;
  for (el e : els[v / 2])
    t2.eb(e.c + c[v - 1], e.w + w[v - 1]);

  merge(all(t), all(t2), back_inserter(els[v]));
  // cout << v << '\\n';
  // for(el e : els[v]) cout << e.w << ' ' << e.c << '\\n';
  // cout << '\\n';
  pr[v].resize(els[v].size(), 0);
  // cout << "DONE " << v << '\\n';
  pr[v][0] = els[v][0].c;
  for (int i = 1; i < els[v].size(); i++)
    pr[v][i] = max(pr[v][i - 1], els[v][i].c);

  if (d < mxp - 1) {
    DPFS(2 * v, d + 1);
    DPFS(2 * v + 1, d + 1);
  }
}
void solve(int ci) {
  cin >> n;
  f(n) cin >> c[i] >> w[i];
  els[0].eb(0, 0);
  DPFS(1, 0);
  // cout << "DONE";
  int d[n + 1];
  d[1] = 0;
  for (int i = 2; i <= n; i++) {
    d[i] = d[i - 1];
    if (1 << (d[i] + 1) <= i)
      d[i]++;
  }
  ic(q) fk(q) {
    ic(v) ic(l) int ans = 0;
    if (d[v] < mxp) {
      for (el e : els[v])
        if (e.w <= l)
          ans = max(ans, e.c);
      cout << ans << '\\n';
      continue;
    }
    vector<el> cp;
    for (; d[v] >= mxp; v = v / 2)
      cp.eb(c[v - 1], w[v - 1]);
    vector<el> cls;
    int cn = cp.size();
    f(1 << cn) {
      int cc = 0, cw = 0;
      fj(cn) {
        if ((i >> j) & 1)
          cc += cp[j].c, cw += cp[j].w;
      }
      cls.eb(cc, cw);
    }
    sort(all(cls));
    int lp = els[v].size() - 1;
    f(cls.size()) {
      while (lp - 1 >= 0 && els[v][lp].w + cls[i].w > l)
        lp--;
      if (els[v][lp].w + cls[i].w <= l)
        ans = max(ans, pr[v][lp] + cls[i].c);
    }
    cout << ans << '\\n';
  }
}

signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t = 1;
  // cin >> t;
  f(t) solve(i + 1);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long int;
using vi = vector<int>;
#define f(n) for (int i = 0; i < n; i++)
#define fj(n) for (int j = 0; j < n; j++)
#define fk(n) for (int k = 0; k < n; k++)
#define ci(n) cin >> n;
#define ic(n)                                                                  \\
  int n;                                                                       \\
  cin >> n;
#define lc(n)                                                                  \\
  int n;                                                                       \\
  cin >> n;
#define all(a) a.begin(), a.end()
#define pri(a, n)                                                              \\
  for (int i = 0; i < n; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << "\\n";
#define sc(a, n)                                                               \\
  int a[n] = {0};                                                              \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];
#define scl(a, n)                                                              \\
  ll a[n] = {0};                                                               \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];
#define pb push_back
#define eb emplace_back
// #define int long long
const int inf = 2e9, mxp = 10, maxn = (1 << 18);

struct el {
  int c, w;
  el(int _c = 0, int _w = 0) : c(_c), w(_w) {}
};
bool operator<(el a, el b) { return a.w < b.w; }
vector<el> els[(1 << 9) + 10];
vector<int> pr[maxn];
int c[maxn], w[maxn], n;
void DPFS(int v, int d) {
  // for(el e : els[v / 2]) t.pb(e);
  // cout << v << '\\n';
  if (v > n)
    return;
  vector<el> t = els[v / 2];
  vector<el> t2;
  for (el e : els[v / 2])
    t2.eb(e.c + c[v - 1], e.w + w[v - 1]);

  merge(all(t), all(t2), back_inserter(els[v]));
  // cout << v << '\\n';
  // for(el e : els[v]) cout << e.w << ' ' << e.c << '\\n';
  // cout << '\\n';
  pr[v].resize(els[v].size(), 0);
  // cout << "DONE " << v << '\\n';
  pr[v][0] = els[v][0].c;
  for (int i = 1; i < els[v].size(); i++)
    pr[v][i] = max(pr[v][i - 1], els[v][i].c);

  if (d < mxp - 1) {
    DPFS(2 * v, d + 1);
    DPFS(2 * v + 1, d + 1);
  }
}
void solve(int ci) {
  cin >> n;
  f(n) cin >> c[i] >> w[i];
  els[0].eb(0, 0);
  DPFS(1, 0);
  // cout << "DONE";
  int d[n + 1];
  d[1] = 0;
  for (int i = 2; i <= n; i++) {
    d[i] = d[i - 1];
    if (1 << (d[i] + 1) <= i)
      d[i]++;
  }
  ic(q) f(q) {
    ic(v) ic(l) int ans = 0;
    if (d[v] < mxp) {
      for (el e : els[v])
        if (e.w <= l)
          ans = max(ans, e.c);
      cout << ans << '\\n';
      continue;
    }
    vector<el> cp;
    for (; d[v] >= mxp; v = v / 2)
      cp.eb(c[v - 1], w[v - 1]);
    vector<el> cls;
    int cn = cp.size();
    f(1 << cn) {
      int cc = 0, cw = 0;
      fj(cn) {
        if ((i >> j) & 1)
          cc += cp[j].c, cw += cp[j].w;
      }
      cls.eb(cc, cw);
    }
    sort(all(cls));
    int lp = els[v].size() - 1;
    f(cls.size()) {
      while (lp - 1 > 0 && els[v][lp].w + cls[i].w > l)
        lp--;
      if (els[v][lp].w + cls[i].w <= l)
        ans = max(ans, pr[v][lp] + cls[i].c);
    }
    cout << ans << '\\n';
  }
}

signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t = 1;
  // cin >> t;
  f(t) solve(i + 1);
}