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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int LMAX = 100000;
int n, maxDepth;
vector<int> w, v;
vector<vector<pair<int, int>>> query;
vector<int> ans;

void solve(int pos, int depth, vector<int> &dp, vector<pair<int, int>> &half) {
  if (pos >= n)
    return;

  vector<int> nextDp;
  if (depth < maxDepth / 2) {
    nextDp = dp;
    for (int i = LMAX; i >= w[pos]; --i)
      nextDp[i] = max(nextDp[i], nextDp[i - w[pos]] + v[pos]);
    dp.swap(nextDp);
  } else {
    for (int i = half.size() - 1; i >= 0; --i)
      half.push_back(
          make_pair(half[i].first + w[pos], half[i].second + v[pos]));
  }

  for (const auto &q : query[pos]) {
    int l = q.first;
    for (const auto &p : half) {
      if (p.first <= l) {
        int v = dp[l - p.first] + p.second;
        ans[q.second] = max(ans[q.second], v);
      }
    }
  }

  solve(2 * pos + 1, depth + 1, dp, half);
  solve(2 * pos + 2, depth + 1, dp, half);

  if (depth < maxDepth / 2)
    dp.swap(nextDp);
  else
    half.resize(half.size() / 2);
}

int main() {
  cin >> n;
  w.resize(n);
  v.resize(n);
  for (int i = 0; i < n; ++i)
    cin >> v[i] >> w[i];

  maxDepth = 1;
  while ((1 << maxDepth) <= n)
    ++maxDepth;

  int q;
  cin >> q;
  query.resize(n);
  for (int i = 0; i < q; ++i) {
    int v, l;
    cin >> v >> l;
    --v;
    query[v].push_back(make_pair(l, i));
  }

  vector<int> dp(LMAX + 1, 0);
  vector<pair<int, int>> half = {{0, 0}};
  ans.resize(q);
  solve(0, 0, dp, half);
  for (int i = 0; i < q; ++i)
    cout << ans[i] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n;
vector<int> v, w;
vector<vector<pair<int, int>>> query;
vector<int> ans;

void solve(int pos, bool isOdd, vector<vector<pair<int, int>>> &dpPrev) {
  if (pos >= n)
    return;

  int k = isOdd ? 0 : 1;
  vector<vector<pair<int, int>>> dp(2);
  dp[k] = dpPrev[k];
  dp[k ^ 1] = move(dpPrev[k ^ 1]);

  for (const auto &p : dpPrev[k]) {
    int w2 = p.first + w[pos];
    int v2 = p.second + v[pos];
    dp[k].push_back(make_pair(w2, v2));
  }
  sort(dp[k].begin(), dp[k].end());

  for (int i = 1; i < (int)dp[k].size(); ++i) {
    if (dp[k][i - 1].first == dp[k][i].first)
      dp[k][i - 1] = dp[k][i];
    else if (dp[k][i - 1].second > dp[k][i].second)
      dp[k][i] = dp[k][i - 1];
  }
  dp[k].erase(unique(dp[k].begin(), dp[k].end()), dp[k].end());

  for (const auto &q : query[pos]) {
    int l = q.first;
    auto it = dp[1].rbegin();
    for (const auto &p : dp[0]) {
      while (it != dp[1].rend() && p.first + it->first > l) {
        ++it;
      }
      if (it == dp[1].rend())
        break;

      int v = p.second + it->second;
      ans[q.second] = max(ans[q.second], v);
    }
  }

  solve(2 * pos + 1, !isOdd, dp);
  solve(2 * pos + 2, !isOdd, dp);
  dpPrev[k ^ 1] = move(dp[k ^ 1]);
}

int main() {
  cin >> n;
  v.resize(n);
  w.resize(n);
  for (int i = 0; i < n; ++i)
    cin >> v[i] >> w[i];

  int q;
  cin >> q;
  query.resize(n);
  for (int i = 0; i < q; ++i) {
    int v, l;
    cin >> v >> l;
    --v;
    query[v].push_back(make_pair(l, i));
  }

  vector<vector<pair<int, int>>> dp(2, {{0, 0}});
  ans.resize(q);
  solve(0, false, dp);
  for (int i = 0; i < q; ++i)
    cout << ans[i] << endl;

  return 0;
}