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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define chmax(a, b) a = max(a, b);
#define chmin(a, b) a = min(a, b);
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<int> v(n + 1), w(n + 1);
  rep(i, n) cin >> v[i + 1] >> w[i + 1];
  int q;
  cin >> q;
  rep(_, q) {
    int s, l;
    cin >> s >> l;
    vector<vector<P>> items(2);
    for (; s; s /= 2) {
      items[items[0].size() < 9 ? 0 : 1].emplace_back(w[s], v[s]);
    }
    vector<vector<P>> d(2);
    rep(t, 2) {
      vector<P> d1{{0, 0}};
      for (auto p : items[t]) {
        vector<P> d2;
        for (auto q : d1) {
          d2.emplace_back(p.first + q.first, p.second + q.second);
        }
        vector<P> nd;
        int i1 = 0, i2 = 0;
        while (i2 < d2.size()) {
          P p;
          if (i1 < d1.size() && d1[i1] < d2[i2]) {
            p = d1[i1++];
          } else {
            p = d2[i2++];
          }
          if (nd.empty()) {
            nd.push_back(p);
          } else if (nd.back().first == p.first) {
            nd.back().second = p.second;
          } else if (nd.back().second < p.second) {
            nd.push_back(p);
          }
        }
        d1 = move(nd);
      }
      d[t] = move(d1);
    }
    int ans = 0;
    int j = d[1].size() - 1;
    rep(i, d[0].size()) {
      while (j >= 0 && d[0][i].first + d[1][j].first > l)
        j--;
      if (j < 0)
        break;
      chmax(ans, d[0][i].second + d[1][j].second);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define chmax(a, b) a = max(a, b);
#define chmin(a, b) a = min(a, b);
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<int> v(n + 1), w(n + 1);
  rep(i, n) cin >> v[i + 1] >> w[i + 1];
  int q;
  cin >> q;
  rep(_, q) {
    int s, l;
    cin >> s >> l;
    vector<vector<P>> items(2);
    for (; s; s /= 2) {
      items[items[0].size() < 9 ? 0 : 1].emplace_back(w[s], v[s]);
    }
    vector<vector<P>> d(2);
    rep(t, 2) {
      vector<P> d1{{0, 0}};
      for (auto p : items[t]) {
        vector<P> d2;
        for (auto q : d1) {
          d2.emplace_back(p.first + q.first, p.second + q.second);
        }
        vector<P> nd;
        int i1 = 0, i2 = 0;
        while (1) {
          if (d1[i1] < d2[i2]) {
            nd.push_back(d1[i1++]);
            if (i1 == d1.size()) {
              nd.insert(nd.end(), d2.begin() + i2, d2.end());
              break;
            }
          } else {
            nd.push_back(d2[i2++]);
            if (i2 == d2.size()) {
              nd.insert(nd.end(), d1.begin() + i1, d1.end());
              break;
            }
          }
        }
        d1 = move(nd);
      }
      rep(i, d1.size() - 1) {
        if (d1[i].first != d1[i + 1].first &&
            d1[i].second >= d1[i + 1].second) {
          d1[i + 1] = d1[i];
        }
      }
      d1.erase(unique(d1.begin(), d1.end()), d1.end());
      d[t] = move(d1);
    }
    int ans = 0;
    int j = d[1].size() - 1;
    rep(i, d[0].size()) {
      while (j >= 0 && d[0][i].first + d[1][j].first > l)
        j--;
      if (j < 0)
        break;
      chmax(ans, d[0][i].second + d[1][j].second);
    }
    cout << ans << endl;
  }
}