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

#include <bits/stdc++.h>
using namespace std;

struct dat {
  long long n, l;
};

long long n, q, lim = 100000;
vector<long long> v, w, res;
vector<vector<long long>> dp;
vector<vector<dat>> query;

void solve();
void dfs(int now, int dep);

int main() {
  cin >> n;
  v.resize(n);
  w.resize(n);
  for (int i = 0; i < n; ++i)
    cin >> v[i] >> w[i];
  cin >> q;
  query.resize(n + 1);
  for (int i = 0; i < q; ++i) {
    long long vn, li;
    cin >> vn >> li;
    query[vn].push_back({i, li});
  }
  dp.assign(10, vector<long long>(lim + 1, 0));
  solve();
  for (int i = 0; i < q; ++i)
    cout << res[i] << endl;
  return 0;
}

void solve() {
  res.assign(q, 0);
  dfs(1, 0);
}

void dfs(int now, int dep) {
  if (now > n)
    return;
  if (dep == 9)
    for (int i = 1; i <= lim; ++i)
      dp[dep][i] = max(dp[dep][i], dp[dep][i - 1]);
  if (dep < 9) {
    dp[dep + 1] = dp[dep];
    for (int i = w[now - 1]; i <= lim; ++i)
      dp[dep + 1][i] =
          max(dp[dep + 1][i], dp[dep][i - w[now - 1]] + v[now - 1]);
    vector<long long> vm = dp[dep + 1];
    for (int i = 1; i <= lim; ++i)
      vm[i] = max(vm[i - 1], vm[i]);
    for (auto qu : query[now])
      res[qu.n] = vm[qu.l];
  } else {
    using P = pair<long long, long long>;
    vector<P> lst;
    lst.emplace_back(0, 0);
    long long d = dep, x = now;
    while (x >= (1 << 9)) {
      int len = lst.size();
      for (int i = 0; i < len; ++i)
        lst.emplace_back(lst[i].first + w[x - 1], lst[i].second + v[x - 1]);
      x >>= 1;
    }
    for (auto qu : query[now]) {
      long long nres = dp[9][qu.l], nl = qu.l;
      for (auto p : lst)
        if (p.first <= nl)
          nres = max(nres, dp[9][nl - p.first] + p.second);
      res[qu.n] = nres;
    }
  }
  dfs(now * 2, dep + 1);
  dfs(now * 2 + 1, dep + 1);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

struct dat {
  long long n, l;
};

long long n, q, lim = 100000;
vector<long long> v, w, res;
vector<map<long long, long long>> dp;
vector<vector<dat>> query;

void solve();
void dfs(int now, int dep);

int main() {
  cin >> n;
  v.resize(n);
  w.resize(n);
  for (int i = 0; i < n; ++i)
    cin >> v[i] >> w[i];
  cin >> q;
  query.resize(n + 1);
  for (int i = 0; i < q; ++i) {
    long long vn, li;
    cin >> vn >> li;
    query[vn].push_back({i, li});
  }
  dp.resize(20);
  solve();
  for (int i = 0; i < q; ++i)
    cout << res[i] << endl;
  return 0;
}

void solve() {
  res.assign(q, 0);
  dp[0][0] = 0;
  dfs(1, 0);
}

void dfs(int now, int dep) {
  if (now > n)
    return;
  dp[dep + 1] = dp[dep];
  for (auto p : dp[dep]) {
    long long nw = p.first, nv = p.second;
    if (nw + w[now - 1] > lim)
      continue;
    dp[dep + 1][nw + w[now - 1]] =
        max(dp[dep + 1][nw + w[now - 1]], nv + v[now - 1]);
  }
  if (query[now].size()) {
    using P = pair<long long, long long>;
    vector<P> vm;
    for (auto p : dp[dep + 1])
      vm.push_back(p);
    vm.push_back(P(-1, 0));
    sort(vm.begin(), vm.end());
    for (int i = 1; i < vm.size(); ++i)
      vm[i].second = max(vm[i].second, vm[i - 1].second);
    for (auto qu : query[now]) {
      auto it = lower_bound(vm.begin(), vm.end(), P(qu.l + 1, -1));
      res[qu.n] = (*(--it)).second;
    }
  }
  dfs(now * 2, dep + 1);
  dfs(now * 2 + 1, dep + 1);
}