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

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define ss(x) (int)x.size()
#define fi first
#define se second
#define cat(x) cerr << #x << " = " << x << endl
#define rep(i, j, n) for (int i = j; i <= n; ++i)
#define per(i, j, n) for (int i = n; j <= i; --i)
#define all(x) x.begin(), x.end()

using namespace std;

const int N = (1 << 18);
const int M = 1e5 + 10;
const int H = 9;

int n, q, a, b;

int val[N];
int wei[N];
int ans[N];

vector<pair<int, int>> Q[N];

int dp[N];
int h[N];

int V[N];
int W[N];

void dfs(int v, int p) {
  if (v > n)
    return;
  h[v] = h[p] + 1;

  vector<int> ver;
  if (h[v] <= H) {
    int x = v;
    while (x) {
      ver.pb(x);
      x /= 2;
    }
  } else {
    int x = v;
    while (h[x] > H) {
      ver.pb(x);
      x /= 2;
    }
  }

  if (h[v] == H)
    rep(i, 0, M - 1) dp[i] = 0;

  if (ss(Q[v]) || h[v] == H) {

    int S = ss(ver);
    rep(mask, 0, (1 << S) - 1) {
      int waga = 0, war = 0;
      if (mask) {
        int bit = 32 - __builtin_clz(mask) - 1;
        assert((mask & (1 << bit)));
        W[mask] = W[mask ^ (1 << bit)] + wei[ver[bit]];
        V[mask] = V[mask ^ (1 << bit)] + val[ver[bit]];
        waga = W[mask];
        war = V[mask];
      }

      if (waga > M)
        continue;

      if (h[v] == H)
        dp[waga] = max(dp[waga], war);

      for (auto it : Q[v]) {
        if (it.fi < waga)
          continue;
        int z = war + (h[v] > H ? dp[it.fi - waga] : 0);
        ans[it.se] = max(ans[it.se], z);
      }
    }

    if (h[v] == H)
      rep(i, 1, M - 1) dp[i] = max(dp[i], dp[i - 1]);
  }

  dfs(2 * v, v);
  dfs(2 * v + 1, v);
}

int main() {
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d%d", val + i, wei + i);
  scanf("%d", &q);
  rep(i, 1, q) {
    scanf("%d%d", &a, &b);
    Q[a].pb({b, i});
  }
  dfs(1, 0);
  rep(i, 1, q) printf("%d\\n", ans[i]);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define ss(x) (int)x.size()
#define fi first
#define se second
#define cat(x) cerr << #x << " = " << x << endl
#define rep(i, j, n) for (int i = j; i <= n; ++i)
#define per(i, j, n) for (int i = n; j <= i; --i)
#define all(x) x.begin(), x.end()

using namespace std;

const int N = (1 << 18);
const int H = 9;

int n, q, a, b;

int val[N];
int wei[N];
int ans[N];

vector<pair<int, int>> Q[N];

int dp[N];
int h[N];

void dfs(int v, int p) {
  if (v > n)
    return;
  h[v] = h[p] + 1;

  vector<int> ver;
  if (h[v] <= H) {
    int x = v;
    while (x) {
      ver.pb(x);
      x /= 2;
    }
  } else {
    int x = v;
    while (h[x] > H) {
      ver.pb(x);
      x /= 2;
    }
  }

  if (h[v] == H)
    rep(i, 0, N - 1) dp[i] = 0;

  int S = ss(ver);
  rep(mask, 0, (1 << S) - 1) {
    int waga = 0, war = 0;
    rep(j, 0, S - 1) if ((mask >> j) & 1) {
      waga += wei[ver[j]];
      war += val[ver[j]];
    }

    if (h[v] == H)
      if (waga < N)
        dp[waga] = max(dp[waga], war);

    for (auto it : Q[v]) {
      if (it.fi < waga)
        continue;
      int z = war + (h[v] > H ? dp[it.fi - waga] : 0);
      ans[it.se] = max(ans[it.se], z);
    }
  }

  if (h[v] == H)
    rep(i, 1, N - 1) dp[i] = max(dp[i], dp[i - 1]);

  dfs(2 * v, v);
  dfs(2 * v + 1, v);
}

int main() {
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d%d", val + i, wei + i);
  scanf("%d", &q);
  rep(i, 1, q) {
    scanf("%d%d", &a, &b);
    Q[a].pb({b, i});
  }
  dfs(1, 0);
  rep(i, 1, q) printf("%d\\n", ans[i]);

  return 0;
}