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

// Amsal Shoma Ro Gaedan Ye Amal Pasandidas !

#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\\n'

using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ll Mod = 1000000007LL;
const int N = 3e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;

const int L = 100010;
const int K = 1024;

int n, dp[12][L];
int ans[N], v[N], w[N];

vector<pair<pll, ll>> Q[N];

vector<pll> V;
int lb[K];
int sv[K], sw[K];

void Build() {
  int nn = V.size();
  for (int mk = 1; mk < (1 << nn); mk++) {
    sv[mk] = sv[mk ^ (1 << lb[mk])] + V[lb[mk]].F;
    sw[mk] = sw[mk ^ (1 << lb[mk])] + V[lb[mk]].S;
  }
}

void Solve(int u, int d) {
  if (u >= K)
    return;
  if (u > n)
    return;
  if (d == 0)
    memset(dp[d], 0, sizeof dp[d]);
  else
    memcpy(dp[d], dp[d - 1], sizeof dp[d - 1]);
  for (int i = L - 1; i >= w[u]; i--)
    dp[d][i] = max(dp[d][i], dp[d][i - w[u]] + v[u]);
  Solve(u << 1, d + 1);
  Solve(u << 1 | 1, d + 1);

  int vv, lp;
  for (auto x : Q[u]) {
    vv = x.F.F;
    lp = x.F.S;
    V.clear();
    while (vv != u) {
      V.pb({v[vv], w[vv]});
      vv /= 2;
    }
    int nn = V.size();
    Build();
    for (int mk = 0; mk < (1 << nn); mk++) {
      if (sw[mk] <= lp)
        ans[x.S] = max(ans[x.S], dp[d][lp - sw[mk]] + sv[mk]);
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  for (int i = 1; i < K; i++) {
    for (int j = 0; j < 10; j++)
      if (i >> j & 1)
        lb[i] = j;
  }
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];
  int q, u, up, l;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> u >> l;
    up = u;
    while (up >= K)
      up /= 2;
    Q[up].pb({{u, l}, i});
  }
  Solve(1, 0);

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


Error: TLE

Fixed submission:

// Amsal Shoma Ro Gaedan Ye Amal Pasandidas !

#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\\n'

using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ll Mod = 1000000007LL;
const int N = 3e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;

const int L = 100010;
const int K = 256;

int n, dp[12][L];
int ans[N], v[N], w[N];

vector<pair<pll, ll>> Q[N];

vector<pll> V;

void Solve(int u, int d) {
  if (u >= K)
    return;
  if (u > n)
    return;
  if (d == 0)
    memset(dp[d], 0, sizeof dp[d]);
  else
    memcpy(dp[d], dp[d - 1], sizeof dp[d - 1]);
  for (int i = L - 1; i >= w[u]; i--)
    dp[d][i] = max(dp[d][i], dp[d][i - w[u]] + v[u]);
  Solve(u << 1, d + 1);
  Solve(u << 1 | 1, d + 1);

  int vv, lp, sv, sw;
  for (auto x : Q[u]) {
    vv = x.F.F;
    lp = x.F.S;
    V.clear();
    while (vv != u) {
      V.pb({v[vv], w[vv]});
      vv /= 2;
    }
    int nn = V.size();
    for (int mk = 0; mk < (1 << nn); mk++) {
      sv = 0;
      sw = 0;
      for (int j = 0; j < nn; j++) {
        if (mk >> j & 1)
          sv += V[j].F, sw += V[j].S;
      }
      if (sw <= lp)
        ans[x.S] = max(ans[x.S], dp[d][lp - sw] + sv);
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];
  int q, u, up, l;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> u >> l;
    up = u;
    while (up >= K)
      up /= 2;
    Q[up].pb({{u, l}, i});
  }
  Solve(1, 0);

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