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

#include <bits/stdc++.h>
#include <iostream>
#define ll long long
#define ld long double
#define IO                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
int n, q, v[300009], w[300009], h[300009], mxarr[2009];
vector<pair<int, int>> dp[300009];
int main() {
  IO cin >> n;
  dp[0].push_back({0, 0});
  for (int i = 1; i <= n; i++)
    h[i] = h[i / 2] + 1;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
    if (h[i] <= 11) {
      dp[i] = dp[i / 2];
      for (auto j : dp[i / 2]) {
        pair<int, int> p = {j.first + w[i], j.second + v[i]};
        if (p.first <= 1e5)
          dp[i].push_back(p);
      }
      sort(dp[i].begin(), dp[i].end());
    }
  }
  cin >> q;
  while (q--) {
    int node, L, ans = 0;
    cin >> node >> L;
    if (h[node] <= 11) {
      for (auto i : dp[node])
        if (i.first <= L)
          ans = max(ans, i.second);
      cout << ans << "\\n";
      continue;
    }
    vector<pair<int, int>> v1;
    v1.push_back({0, 0});
    while (h[node] > 11) {
      int sz = v1.size();
      for (int i = 0; i < sz; i++) {
        pair<int, int> p = {v1[i].first + w[node], v1[i].second + v[node]};
        if (p.first <= L)
          v1.push_back(p);
      }
      node = node / 2;
    }
    sort(v1.begin(), v1.end());
    int curmx = 0, id = 0;
    while (!v1.empty()) {
      int rem = L - v1.back().first;
      while (id + 1 < dp[node].size() && dp[node][id + 1].first <= rem)
        id++, curmx = max(curmx, dp[node][id].second);
      ans = max(ans, v1.back().second + curmx);
      v1.pop_back();
    }
    cout << ans << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#define ll long long
#define ld long double
#define IO                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
int n, q, v[300009], w[300009], h[300009], mxarr[2009];
int main() {
  IO cin >> n;
  for (int i = 1; i <= n; i++)
    h[i] = h[i / 2] + 1;
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];
  cin >> q;
  while (q--) {
    int node, L;
    cin >> node >> L;
    vector<pair<int, int>> v1, v2;
    v1.push_back({0, 0});
    v2.push_back({0, 0});
    int cnt = h[node];
    cnt /= 2;
    while (h[node] > cnt) {
      int sz = v1.size();
      for (int i = 0; i < sz; i++) {
        pair<int, int> p = {v1[i].first + w[node], v1[i].second + v[node]};
        if (p.first <= L)
          v1.push_back(p);
        //  cout<<p.first<<p.second<<endl;
      }
      node = node / 2;
    }
    // cout<<endl<<endl;
    while (node) {
      int sz = v2.size();
      for (int i = 0; i < sz; i++) {
        pair<int, int> p = {v2[i].first + w[node], v2[i].second + v[node]};
        if (p.first <= L)
          v2.push_back(p);
        //      cout<<p.first<<p.second<<endl;
      }
      node = node / 2;
    }
    sort(v2.begin(), v2.end());
    int sz = v2.size();
    mxarr[0] = v2[0].second;
    for (int i = 1; i < sz; i++)
      mxarr[i] = max(mxarr[i - 1], v2[i].second);
    int ans = 0;
    for (auto i : v1) {
      int rem = L - i.first;
      int low = 0, high = v2.size() - 1, id = 0;
      while (low <= high) {
        int mid = (low + high) / 2;
        if (v2[mid].first <= rem)
          id = mid, low = mid + 1;
        else
          high = mid - 1;
      }
      ans = max(ans, i.second + mxarr[id]);
    }
    cout << ans << "\\n";
  }
}