Problem p02648 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) FOR(i, 0, (n))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define LAR(a, b) ((a) = max((a), (b)))
#define SML(a, b) ((a) = min((a), (b)))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template <typename T> using pque = priority_queue<T, vector<T>, greater<T>>;
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#ifdef LOCAL_DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

constexpr int N = 524288;
ll v[N], w[N];
using vpl = vector<pair<ll, ll>>;
vpl x, y, zz;

void op(vpl &z, int a, int k) {
  z.clear();
  z.EB(0, 0);
  REP(i, k) {
    zz.clear();
    int b = a >> i;
    auto it = z.begin();
    for (auto [ww, vv] : z) {
      while (it != z.end() && it->first + w[b] < ww) {
        zz.EB(it->first + w[b], it->second + v[b]);
        it++;
      }
      zz.EB(ww, vv);
    }
    while (it != z.end()) {
      zz.EB(it->first + w[b], it->second + v[b]);
      it++;
    }
    swap(z, zz);
  }
  // REP(h, (1<<k)){
  // 	ll vv = 0, ww = 0;
  // 	REP(i, k){
  // 		if(h & (1<<i)){
  // 			vv += v[a>>i];
  // 			ww += w[a>>i];
  // 		}
  // 	}
  // 	z.EB(ww, vv);
  // }
  sort(ALL(z));
  DEBUG(" (%d, %d):\\n", a, k);
  for (auto [ww, vv] : z)
    DEBUG("    %lld %lld\\n", ww, vv);
}

int main() {
  int n;
  scanf("%d", &n);
  FOR(i, 1, n + 1) scanf("%lld%lld", v + i, w + i);
  int q;
  scanf("%d", &q);
  REP(iq, q) {
    int a;
    ll l;
    scanf("%d%lld", &a, &l);
    int k = 32 - __builtin_clz(a);
    DEBUG("Q: %d %lld %d\\n", a, l, k);
    op(x, a, k / 2);
    op(y, a >> (k / 2), k - k / 2);

    FOR(i, 1, (int)y.size())
    LAR(y[i].second, y[i - 1].second);

    ll ans = 0;
    int j = y.size() - 1;
    for (auto [ww, vv] : x) {
      if (ww > l)
        break;
      while (y[j].first + ww > l)
        j--;
      LAR(ans, vv + y[j].second);
    }

    printf("%lld\\n", ans);
  }
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) FOR(i, 0, (n))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define LAR(a, b) ((a) = max((a), (b)))
#define SML(a, b) ((a) = min((a), (b)))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template <typename T> using pque = priority_queue<T, vector<T>, greater<T>>;
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#ifdef LOCAL_DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

using vpl = vector<pair<ll, ll>>;
ll op(vpl &x, vpl &y, ll l) {
  ll ans = 0;
  int j = y.size() - 1;
  for (auto [ww, vv] : x) {
    if (ww > l)
      break;
    while (y[j].first + ww > l)
      j--;
    LAR(ans, vv + y[j].second);
  }
  return ans;
}

constexpr int N = 524288;
ll v[N], w[N];
vpl x[N], xu = {{0, 0}};

int main() {
  int n;
  scanf("%d", &n);
  FOR(i, 1, n + 1) scanf("%lld%lld", v + i, w + i);
  int q;
  scanf("%d", &q);
  REP(iq, q) {
    int a;
    ll l;
    scanf("%d%lld", &a, &l);
    int k = 32 - __builtin_clz(a);
    DEBUG("Q: %d %lld %d\\n", a, l, k);
    REP(i, k) {
      int b = a >> (k - i - 1);
      if (x[b].size())
        continue;
      if (i == 0 || i == 9) {
        x[b].EB(0, 0);
        x[b].EB(w[b], v[b]);
      } else {
        auto it = x[b / 2].begin();
        for (auto [ww, vv] : x[b / 2]) {
          while (it != x[b / 2].end() && it->first + w[b] < ww) {
            // DEBUG("  added (%lld+%lld, %lld+%lld)\\n",
            // 		it->first, w[b], it->second, v[b]);
            x[b].EB(it->first + w[b], it->second + v[b]);
            it++;
          }
          // DEBUG("  added (%lld, %lld)\\n", ww, vv);
          x[b].EB(ww, vv);
        }
        while (it != x[b / 2].end()) {
          // DEBUG("  added (%lld+%lld, %lld+%lld)\\n",
          // 		it->first, w[b], it->second, v[b]);
          x[b].EB(it->first + w[b], it->second + v[b]);
          it++;
        }
      }
      DEBUG(" %d: \\n", b);
      for (auto [ww, vv] : x[b])
        DEBUG("  (%lld, %lld)\\n", ww, vv);
      FOR(j, 1, (int)x[b].size()) LAR(x[b][j].second, x[b][j - 1].second);
    }
    ll ans;
    if (k <= 9) {
      ans = op(x[a], xu, l);
    } else {
      ans = op(x[a], x[a >> (k - 9)], l);
    }
    printf("%lld\\n", ans);
  }
}