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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<ll> v(n), w(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  vector<vector<pair<ll, ll>>> s(512);
  s[0].emplace_back(0, 0);
  for (int i = 0; i < min(n, 512); i++) {
    s[i] = s[(i - 1) / 2];
    for (int j = s[i].size() - 1; j >= 0; j--) {
      s[i].emplace_back(s[i][j].first + w[i], s[i][j].second + v[i]);
    }
    sort(s[i].begin(), s[i].end());
    for (int j = 1; j < s[i].size(); j++) {
      s[i][j].second = max(s[i][j].second, s[i][j - 1].second);
    }
  }
  int q;
  cin >> q;
  while (q--) {
    ll vx, l;
    cin >> vx >> l;
    vx--;
    if (vx == 0) {
      if (w[0] <= l) {
        cout << v[0] << '\\n';
      } else {
        cout << 0 << '\\n';
      }
      continue;
    }
    ll ans = 0;
    vector<pair<ll, ll>> t(1);
    while (vx >= 512) {
      for (int i = t.size() - 1; i >= 0; i--) {
        t.emplace_back(t[i].first + w[vx], t[i].second + v[vx]);
      }
      vx--;
      vx /= 2;
    }
    sort(t.begin(), t.end());
    for (int i = 1; i < t.size(); i++) {
      t[i].second = max(t[i].second, t[i - 1].second);
    }
    int j = s[vx].size() - 1;
    for (int i = 0; i < t.size(); i++) {
      int wei = t[i].first, val = t[i].second;
      while (j > 0 && wei + s[vx][j].first > l) {
        j--;
      }
      if (wei + s[vx][j].first > l) {
        break;
      }
      ans = max(ans, val + s[vx][j].second);
    }
    cout << ans << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> v(n), w(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  int q;
  cin >> q;
  vector<vector<pair<int, int>>> l(n);
  vector<int> ans(q);
  for (int i = 0; i < q; i++) {
    int vx, lx;
    cin >> vx >> lx;
    vx--;
    l[vx].emplace_back(lx, i);
  }
  vector<pair<int, int>> s(2), t(1);
  s[1] = {w[0], v[0]};
  vector<int> dep(n);
  function<void(int)> dfs = [&](int vv) {
    debug(vv, s, t);
    for (auto p : l[vv]) {
      int lx, id;
      tie(lx, id) = p;
      int j = s.size() - 1;
      for (int i = 0; i < t.size(); i++) {
        int wei = t[i].first, val = t[i].second;
        while (j > 0 && wei + s[j].first > lx) {
          j--;
        }
        if (wei + s[j].first > lx) {
          break;
        }
        ans[id] = max(ans[id], val + s[j].second);
      }
    }
    vector<pair<int, int>> ss = s, tt = t;
    for (int to = 2 * vv + 1; to <= 2 * vv + 2; to++) {
      if (n <= to) {
        break;
      }
      dep[to] = dep[vv] + 1;
      int wei = w[to], val = v[to];
      if (dep[to] % 2 == 0) {
        for (int i = s.size() - 1; i >= 0; i--) {
          s.emplace_back(s[i].first + wei, s[i].second + val);
        }
        sort(s.begin(), s.end());
        for (int i = 1; i < s.size(); i++) {
          s[i].second = max(s[i].second, s[i - 1].second);
        }
      } else {
        for (int i = t.size() - 1; i >= 0; i--) {
          t.emplace_back(t[i].first + wei, t[i].second + val);
        }
        sort(t.begin(), t.end());
        for (int i = 1; i < t.size(); i++) {
          t[i].second = max(t[i].second, t[i - 1].second);
        }
      }
      dfs(to);
      s = ss, t = tt;
    }
  };
  dfs(0);
  for (int i : ans) {
    cout << i << '\\n';
  }
  return 0;
}