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

#include <iostream>
#include <vector>

#define ws __ws
using namespace std;

const int MAX_N = 1 << 18;
const int MAX_Q = 1e5 + 5;
const int MAX_L = 1e5 + 5;
const int MAX_LG = 20;
const int HALF = 9;

int dp[MAX_LG]
      [MAX_L]; // dp[l][w] is max value you can get with <= w weight at layer l

void add(int val, int weight, int layer) {
  for (int i = 0; i < MAX_L; i++) {
    if (i - weight >= 0) {
      dp[layer][i] = max(dp[layer - 1][i], dp[layer - 1][i - weight] + val);
    } else {
      dp[layer][i] = dp[layer - 1][i];
    }
  }
}

vector<pair<int, int>> qs[MAX_N]; // <id, L>
int ans[MAX_Q];

int vs[MAX_N];
int ws[MAX_N];

int n;

vector<pair<int, int>> stk;

int lgs[MAX_N];
pair<int, int> memo[MAX_N];
int solve(int d, int L) {
  if (d <= HALF) {
    return dp[d][L];
  }

  int stks = (int)stk.size();
  int sol = 0;
  for (int mask = 0; mask < 1 << stks; mask++) {
    pair<int, int> cur;
    if (mask == 0) {
      cur = make_pair(0, 0);
    } else {
      int lsb = mask & -mask;
      cur = memo[mask - lsb];
      cur.first += stk[lgs[lsb]].first;
      cur.second += stk[lgs[lsb]].second;
    }
    memo[mask] = cur;

    if (cur.second <= L) {
      sol = max(sol, cur.first + dp[HALF][L - cur.second]);
    }
  }
  return sol;
}

void dfs(int u, int d) {
  if (d <= HALF) {
    add(vs[u], ws[u], d);
  } else {
    stk.push_back(make_pair(vs[u], ws[u]));
  }

  for (auto pr : qs[u]) {
    ans[pr.first] = solve(d, pr.second);
  }

  if (2 * u <= n)
    dfs(2 * u, d + 1);
  if (2 * u + 1 <= n)
    dfs(2 * u + 1, d + 1);

  if (d > HALF) {
    stk.pop_back();
  }
}

int main() {
  ios::sync_with_stdio(false);

  for (int i = 0; (1 << i) < MAX_N; i++) {
    lgs[1 << i] = i;
  }

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> vs[i] >> ws[i];
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int v, L;
    cin >> v >> L;
    qs[v].push_back(make_pair(i, L));
  }

  dfs(1, 1);

  for (int i = 0; i < q; i++) {
    cout << ans[i] << '\\n';
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

#define ws __ws
using namespace std;

const int MAX_N = 1 << 18;
const int MAX_Q = 1e5 + 5;
const int MAX_L = 1e5 + 5;
const int MAX_LG = 20;

int dp[MAX_LG]
      [MAX_L]; // dp[l][w] is max value you can get with <= w weight at layer l

void add(int val, int weight, int layer) {
  for (int i = 0; i < MAX_L; i++) {
    if (i - weight >= 0) {
      dp[layer][i] = max(dp[layer - 1][i], dp[layer - 1][i - weight] + val);
    } else {
      dp[layer][i] = dp[layer - 1][i];
    }
  }
}

vector<pair<int, int>> qs[MAX_N]; // <id, L>
int ans[MAX_Q];

int vs[MAX_N];
int ws[MAX_N];

int n;
void dfs(int u, int d) {
  add(vs[u], ws[u], d);
  for (auto pr : qs[u]) {
    ans[pr.first] = dp[d][pr.second];
  }

  if (2 * u <= n)
    dfs(2 * u, d + 1);
  if (2 * u + 1 <= n)
    dfs(2 * u + 1, d + 1);
}

int main() {
  ios::sync_with_stdio(false);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> vs[i] >> ws[i];
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int v, L;
    cin >> v >> L;
    qs[v].push_back(make_pair(i, L));
  }

  dfs(1, 1);

  for (int i = 0; i < q; i++) {
    cout << ans[i] << '\\n';
  }
}