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

#include <algorithm>
#include <cstdio>
#include <vector>

using std::vector;

const int MaxK = 18, MaxN = 1 << MaxK, MaxS = 1 << 9;
const int MaxQ = 100005;

int n, q, W = 0;

struct item_t {
  int w, v;

  inline item_t add(const item_t &other) const {
    return {w + other.w, v + other.v};
  }
} items[MaxN];

struct query_t {
  int w, id;
};
vector<query_t> buk[MaxN];

int ans[MaxQ];

inline void append(vector<item_t> &f, const item_t &cur) {
  if (f.empty() || cur.v > f.back().v)
    f.push_back(cur);
}

vector<item_t> trans(const vector<item_t> &f, const item_t &item) {
  vector<item_t> res;
  auto it = f.begin();
  for (auto cur : f) {
    while (it != f.end() && it->w + item.w <= cur.w)
      append(res, it++->add(item));
    append(res, cur);
  }
  while (it != f.end() && it->w + item.w <= W)
    append(res, it++->add(item));
  return res;
}

inline bool cmp_w(const item_t &a, const item_t &b) { return a.w < b.w; }

struct solve {
  const vector<item_t> &dp;
  vector<item_t> tmp;
  int res;

  solve(int u, int w, const vector<item_t> &f) : dp(f), res(0) {
    for (; u >= MaxS; u >>= 1)
      tmp.push_back(items[u]);
    dfs(tmp.begin(), w, 0);
  }

  void dfs(vector<item_t>::iterator cur, int w, int v) {
    if (w < 0)
      return;
    if (w == 0 || cur == tmp.end()) {
      auto it = --std::upper_bound(dp.begin(), dp.end(), (item_t){w, 0}, cmp_w);
      res = std::max(res, v + it->v);
      return;
    }
    auto nxt = std::next(cur);
    dfs(nxt, w, v);
    dfs(nxt, w - cur->w, v + cur->v);
  }
};

void dfs(int u, const vector<item_t> &f) {
  if (u > n)
    return;
  const vector<item_t> &g = u < MaxS ? trans(f, items[u]) : f;
  for (auto q : buk[u])
    ans[q.id] = solve(u, q.w, g).res;
  dfs(u << 1, g);
  dfs(u << 1 | 1, g);
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &items[i].v, &items[i].w);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    int u, w;
    scanf("%d%d", &u, &w);
    buk[u].push_back({w, i});
    W = std::max(W, w);
  }
  dfs(1, {{0, 0}});
  for (int i = 0; i < q; ++i)
    printf("%d\\n", ans[i]);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

const int MaxK = 18, MaxN = 1 << MaxK;
const int MaxQ = 100005, MaxW = 100005;

int n, q, W = 0;

struct item_t {
  int w, v;
} items[MaxN];

struct query_t {
  int w, id;
};
std::vector<query_t> buk[MaxN];

int dp[MaxK + 1][MaxW];
int ans[MaxQ];

void trans(const int *f, int *g, const item_t &item) {
  std::copy(f, f + W + 1, g);
  for (int i = item.w; i <= W; ++i) {
    g[i] = std::max(g[i], f[i - item.w] + item.v);
  }
}

void dfs(int u, int dep) {
  if (u > n)
    return;
  int *F = dp[dep], *G = dp[dep + 1];
  trans(F, G, items[u]);
  for (auto q : buk[u])
    ans[q.id] = G[q.w];
  dfs(u << 1, dep + 1);
  dfs(u << 1 | 1, dep + 1);
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &items[i].v, &items[i].w);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    int u, w;
    scanf("%d%d", &u, &w);
    buk[u].push_back({w, i});
    W = std::max(W, w);
  }
  dfs(1, 0);
  for (int i = 0; i < q; ++i)
    printf("%d\\n", ans[i]);
  return 0;
}