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

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<int, int>;
#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
constexpr int MOD = 1000000007;
constexpr int INF = 2147483647;
void yes(bool expr) { cout << (expr ? "Yes" : "No") << "\\n"; }
struct Node;
vector<Node> F;
int N;
int maxd = 9;
lint calc = 0;
struct Node {
  int idx;
  int v;
  int w;
  int d = 0;
  vector<int> dp;
  void calcd() {
    int tmp = 2;
    while (idx >= tmp) {
      tmp *= 2;
      d++;
    }
  }
  void precalc(vector<int> dpt) {
    if (d > maxd)
      return;
    IREP(i, 100001) if (i - w >= 0) dpt[i] = max(dpt[i], dpt[i - w] + v);
    FOR(i, 1, 100001) dpt[i] = max(dpt[i], dpt[i - 1]);
    calc += 200000;
    dp = dpt;
    if (idx * 2 - 1 < N)
      F[idx * 2 - 1].precalc(dp);
    if (idx * 2 < N)
      F[idx * 2].precalc(dp);
  }
};
int solve(int x, int L) {
  if (F[x - 1].d <= maxd) {
    return F[x - 1].dp[L];
  }
  return max(
      solve(x / 2, L),
      (L - F[x - 1].w >= 0 ? solve(x / 2, L - F[x - 1].w) + F[x - 1].v : 0));
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> N;
  REP(i, N) {
    F.emplace_back();
    F[i].idx = i + 1;
    cin >> F[i].v >> F[i].w;
    // F[i].v = 1; F[i].w = 1;
    F[i].calcd();
  }
  vector<int> dp(100001);
  F[0].precalc(dp);

  // cout << calc << endl;

  int Q;
  cin >> Q;
  REP(q, Q) {
    int v, L;
    cin >> v >> L;
    // v = 249999; L = 100000;

    int ans = solve(v, L);

    cout << ans << "\\n";
  }

  // cout << calc << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<int, int>;
#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
constexpr int MOD = 1000000007;
constexpr int INF = 2147483647;
void yes(bool expr) { cout << (expr ? "Yes" : "No") << "\\n"; }
struct Node;
vector<Node> F;
int N;
int maxd = 9;
lint calc = 0;
struct Node {
  int idx;
  int v;
  int w;
  int d = 0;
  vector<int> dp;
  void calcd() {
    int tmp = 2;
    while (idx >= tmp) {
      tmp *= 2;
      d++;
    }
  }
  void precalc(vector<int> dpt) {
    if (d > maxd)
      return;
    IREP(i, 100001) if (i - w >= 0) dpt[i] = max(dpt[i], dpt[i - w] + v);
    FOR(i, 1, 100001) dpt[i] = max(dpt[i], dpt[i - 1]);
    calc += 200000;
    dp = dpt;
    if (idx * 2 - 1 < N)
      F[idx * 2 - 1].precalc(dp);
    if (idx * 2 < N)
      F[idx * 2].precalc(dp);
  }
  int dfs(vector<int> &V, vector<int> &W) {
    // cout << idx << endl;
    if (d <= maxd)
      return idx;
    calc++;
    V.push_back(v);
    W.push_back(w);
    return F[idx / 2 - 1].dfs(V, W);
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> N;
  REP(i, N) {
    F.emplace_back();
    F[i].idx = i + 1;
    cin >> F[i].v >> F[i].w;
    // F[i].v = 1; F[i].w = 1;
    F[i].calcd();
  }
  vector<int> dp(100001);
  F[0].precalc(dp);

  // cout << calc << endl;

  int Q;
  cin >> Q;
  REP(q, Q) {
    int v, L;
    cin >> v >> L;
    // v = 249999; L = 100000;

    if (F[v - 1].d <= maxd) {
      cout << F[v - 1].dp[L] << "\\n";
      ;
      continue;
    }

    vector<int> V = {0};
    vector<int> W = {0};
    vector<int> dp = F[F[v - 1].dfs(V, W) - 1].dp;

    // REP(i, V.size()) cout << V[i] << (i!=V.size()-1 ? " " : "");
    // cout << "\\n";

    int ans = 0;
    int Vt[1100];
    int Wt[1100];
    Vt[0] = 0;
    Wt[0] = 0;
    int vtsize = 1;
    int idx = 1;
    REP(i, V.size()) {
      REP(j, vtsize) {
        int vsum = V[i] + Vt[j];
        int wsum = W[i] + Wt[j];
        if (wsum <= L && dp[L - wsum] + vsum > ans)
          ans = dp[L - wsum] + vsum;
        Vt[idx] = vsum;
        Wt[idx] = wsum;
        idx++;
        calc++;
      }
      vtsize *= 2;
    }
    // cout << idx << endl;

    cout << ans << "\\n";
  }

  // cout << calc << endl;
}