Problem p02648 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;

using VP = vector<pair<int, int>>;

void merge(const VP &v1, const VP &v2, VP &result) {
  auto it1 = v1.begin(), it2 = v2.begin();
  int vmax = 0;
  while (it1 != v1.end() || it2 != v2.end()) {
    if (it1 == v1.end()) {
      result.push_back(*it2);
      it2++;
    } else if (it2 == v2.end()) {
      result.push_back(*it1);
      it1++;
    } else if (it1->first == it2->first) {
      result.push_back(max(*it1, *it2));
      it1++;
      it2++;
    } else if (it1->first < it2->first) {
      result.push_back(*it1);
      it1++;
    } else {
      result.push_back(*it2);
      it2++;
    }
    vmax = max(vmax, result.back().second);
    result.back().second = vmax;
  }
}

vector<pair<int, int>> buf(1000);

void update(const VP &prev, VP &result, int v, int w) {
  buf.clear();
  for (auto p : prev) {
    int j = p.first + w;
    if (j <= 1e5) {
      buf.push_back({j, p.second + v});
    } else
      break;
  }
  merge(prev, buf, result);
}

int main() {
  int n;
  cin >> n;
  int v[n + 1], w[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];

  const int S = 2;
  vector<pair<int, int>> dp[S];
  dp[1].push_back({0, 0});
  dp[1].push_back({w[1], v[1]});
  for (int i = 2; i <= min(n, S - 1); i++) {
    update(dp[i / 2], dp[i], v[i], w[i]);
  }
  // for (int i = 1; i <= min(n, S-1); i++) {
  //     cerr << "i = " << i << " size = " << dp[i].size() << endl;
  //     for (auto x : dp[i]) cerr << x.first << " " << x.second << endl;
  // }

  int q;
  cin >> q;
  while (q--) {
    int i, l;
    cin >> i >> l;
    vector<pair<int, int>> temp, save;
    temp.push_back({0, 0});
    while (i >= S) {
      swap(save, temp);
      temp.clear();
      update(save, temp, v[i], w[i]);
      i /= 2;
    }

    // cerr << "q = " << i << " " << "size = " << temp.size() << endl;
    // for (auto x : temp) cerr << x.first << " " << x.second << endl;
    int ans = 0;
    auto it1 = dp[i].begin();
    auto it2 = temp.rbegin();
    while (it1 != dp[i].end() && it2 != temp.rend()) {
      while (it2 != temp.rend() && it1->first + it2->first > l)
        it2++;
      if (it2 != temp.rend()) {
        ans = max(ans, it1->second + it2->second);
      }
      it1++;
    }
    cout << ans << endl;
  }
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

using VP = vector<pair<int, int>>;

void merge(const VP &v1, const VP &v2, VP &result) {
  auto it1 = v1.begin(), it2 = v2.begin();
  int vmax = 0;
  while (it1 != v1.end() || it2 != v2.end()) {
    if (it1 == v1.end()) {
      result.push_back(*it2);
      it2++;
    } else if (it2 == v2.end()) {
      result.push_back(*it1);
      it1++;
    } else if (it1->first == it2->first) {
      result.push_back(max(*it1, *it2));
      it1++;
      it2++;
    } else if (it1->first < it2->first) {
      result.push_back(*it1);
      it1++;
    } else {
      result.push_back(*it2);
      it2++;
    }
    vmax = max(vmax, result.back().second);
    result.back().second = vmax;
  }
}

vector<pair<int, int>> buf(1000);

void update(const VP &prev, VP &result, int v, int w) {
  buf.clear();
  for (auto p : prev) {
    int j = p.first + w;
    if (j <= 1e5) {
      buf.push_back({j, p.second + v});
    } else
      break;
  }
  merge(prev, buf, result);
}

int main() {
  int n;
  cin >> n;
  int v[n + 1], w[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];

  const int S = 1024;
  vector<pair<int, int>> dp[S];
  dp[1].push_back({0, 0});
  dp[1].push_back({w[1], v[1]});
  for (int i = 2; i <= min(n, S - 1); i++) {
    update(dp[i / 2], dp[i], v[i], w[i]);
  }
  // for (int i = 1; i <= min(n, S-1); i++) {
  //     cerr << "i = " << i << " size = " << dp[i].size() << endl;
  //     for (auto x : dp[i]) cerr << x.first << " " << x.second << endl;
  // }

  int q;
  cin >> q;
  while (q--) {
    int i, l;
    cin >> i >> l;
    vector<pair<int, int>> temp, save;
    temp.push_back({0, 0});
    while (i >= S) {
      swap(save, temp);
      temp.clear();
      update(save, temp, v[i], w[i]);
      i /= 2;
    }

    // cerr << "q = " << i << " " << "size = " << temp.size() << endl;
    // for (auto x : temp) cerr << x.first << " " << x.second << endl;
    int ans = 0;
    auto it1 = dp[i].begin();
    auto it2 = temp.rbegin();
    while (it1 != dp[i].end() && it2 != temp.rend()) {
      while (it2 != temp.rend() && it1->first + it2->first > l)
        it2++;
      if (it2 != temp.rend()) {
        ans = max(ans, it1->second + it2->second);
      }
      it1++;
    }
    cout << ans << endl;
  }
}