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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

int main() {
  constexpr int MAX_W = 100000;
  int N;
  cin >> N;
  vector<int> V(N), W(N);
  REP(i, N) scanf("%d %d", &V[i], &W[i]);

  int Q;
  cin >> Q;
  vector<int> vert(Q), L(Q);
  REP(i, Q) scanf("%d %d", &vert[i], &L[i]), --vert[i];

  int t9 = 1 << 9;
  chmin(t9, N);

  vector<vector<int>> dp(t9, vector<int>(MAX_W + 1, -INF));

  dp[0][0] = 0;
  dp[0][W[0]] = V[0];
  REP(i, MAX_W) chmax(dp[0][i + 1], dp[0][i]);

  FOR(i, 1, t9) {
    int p = (i - 1) / 2;

    REP(j, MAX_W + 1) {
      int cur = dp[p][j];
      if (j - W[i] >= 0) {
        int val = dp[p][j - W[i]];
        if (val != -INF)
          chmax(cur, val + V[i]);
      }
      dp[i][j] = cur;
      if (j < MAX_W)
        chmax(dp[i][j + 1], dp[i][j]);
    }
  }

  REP(i, Q) {
    // vert[i], L[i]
    if (vert[i] < t9) {
      printf("%d\\n", dp[vert[i]][L[i]]);
      continue;
    }

    vector<int> v;
    int curp = vert[i];
    while (curp >= t9) {
      v.emplace_back(curp);
      curp = (curp - 1) / 2;
    }
    int ans = -INF;
    REP(s, 1 << v.size()) {
      int curv = 0, curw = 0;
      REP(j, v.size()) if (s >> j & 1) {
        curv += V[v[j]];
        curw += W[v[j]];
      }
      if (L[i] - curw >= 0)
        chmax(ans, dp[curp][L[i] - curw] + curv);
    }
    printf("%d\\n", ans);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

template <typename T> struct SegmentTree {
  using value_type = T;
  using const_reference = const value_type &;
  using F = std::function<value_type(const_reference, const_reference)>;
  using size_type = std::size_t;

  SegmentTree(size_type n_, const F &f, const_reference id_elem)
      : f(f), id_elem(id_elem) {
    n = 1;
    while (n < n_)
      n <<= 1;
    node.resize(2 * n, id_elem);
  }

  size_type size() const noexcept { return n; }

  void set(size_type i, const_reference x) {
    assert(i < size());
    node[i += n] = x;
    while (i > 1) {
      i >>= 1;
      node[i] = f(node[i << 1], node[i << 1 | 1]);
    }
  }

  void add(size_type i, const_reference x) {
    assert(i < size());
    i += n;
    node[i] = f(node[i], x);
    while (i > 1) {
      i >>= 1;
      node[i] = f(node[i << 1], node[i << 1 | 1]);
    }
  }

  value_type fold(size_type l, size_type r) const {
    assert(l <= size() && r <= size());
    std::vector<size_type> front, back;
    value_type lv = id_elem, rv = id_elem;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        lv = f(lv, node[l++]);
      if (r & 1)
        rv = f(node[r - 1], rv);
    }
    return f(lv, rv);
  }

  size_type lower_bound(const_reference x) const {
    if (node[1] < x)
      return n;
    size_type idx;
    value_type s = id_elem;
    for (idx = 1; idx < n;) {
      value_type nex = f(s, node[idx << 1]);
      if (nex < x) {
        idx = idx << 1 | 1;
        s = nex;
      } else
        idx = idx << 1;
    }
    return idx - n;
  }

  size_type upper_bound(const_reference x) const {
    if (node[1] <= x)
      return n;
    size_type idx;
    value_type s = id_elem;
    for (idx = 1; idx < n;) {
      value_type nex = f(s, node[idx << 1]);
      if (nex <= x) {
        idx = idx << 1 | 1;
        s = nex;
      } else
        idx = idx << 1;
    }
    return idx - n;
  }

private:
  size_type n;
  F f;
  value_type id_elem;
  std::vector<value_type> node;
};

int main() {
  constexpr int MAX_W = 100000;
  int N;
  cin >> N;
  vector<int> V(N), W(N);
  REP(i, N) scanf("%d %d", &V[i], &W[i]);

  int Q;
  cin >> Q;
  vector<int> vert(Q), L(Q);
  REP(i, Q) scanf("%d %d", &vert[i], &L[i]), --vert[i];
  vector<int> ans(Q);

  int t9 = 1 << 9;
  chmin(t9, N);

  vector<SegmentTree<int>> seg(
      t9, SegmentTree<int>(
              MAX_W + 1, [](int x, int y) { return max(x, y); }, -INF));

  seg[0].set(0, 0);
  seg[0].set(W[0], V[0]);
  FOR(i, 1, t9) {
    int p = (i - 1) / 2;

    REP(j, MAX_W + 1) {
      int cur = seg[p].fold(j, j + 1);
      if (j - W[i] >= 0) {
        int val = seg[p].fold(j - W[i], j - W[i] + 1);
        if (val != -INF) {
          chmax(cur, val + V[i]);
        }
      }
      seg[i].set(j, cur);
    }
  }

  // vector<vector<int>> qv;
  // REP(i, Q) qv[vert[i]].emplace_back(i);

  REP(i, Q) {
    // vert[i], L[i]
    if (vert[i] < t9) {
      printf("%d\\n", seg[vert[i]].fold(0, L[i] + 1));
      continue;
    }

    vector<int> v;
    int curp = vert[i];
    while (curp >= t9) {
      v.emplace_back(curp);
      curp = (curp - 1) / 2;
    }
    int ans = -INF;
    REP(s, 1 << v.size()) {
      int curv = 0, curw = 0;
      REP(j, v.size()) if (s >> j & 1) {
        curv += V[v[j]];
        curw += W[v[j]];
      }
      if (L[i] - curw + 1 > 0)
        chmax(ans, seg[curp].fold(0, L[i] - curw + 1) + curv);
    }
    printf("%d\\n", ans);
  }
  return 0;
}