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

#include <bits/stdc++.h>
using namespace std;

// template {{{
#define range(i, l, r) for (int i = (int)(l); i < (int)(r); (i) += 1)
#define rrange(i, l, r) for (int i = (int)(r)-1; i >= (int)(l); (i) -= 1)

#define whole(f, x, ...)                                                       \\
  ([&](decltype((x)) container) {                                              \\
    return (f)(begin(container), end(container), ##__VA_ARGS__);               \\
  })(x)
#define rwhole(f, x, ...)                                                      \\
  ([&](decltype((x)) container) {                                              \\
    return (f)(rbegin(container), rend(container), ##__VA_ARGS__);             \\
  })(x)

#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl

using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;

// constexpr i32 mod   = 998244353;
constexpr i32 mod = 1e9 + 7;
constexpr i32 inf = 1001001001;
constexpr i64 infll = 1001001001001001001ll;

constexpr int dx[] = {0, -1, 1, 0, -1, 1, -1, 1};
constexpr int dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};

struct IoSetup {
  IoSetup(int x = 15) {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(x);
    cerr << fixed << setprecision(x);
  }
} iosetup;

template <typename T = i64> T input() {
  T x;
  cin >> x;
  return x;
}

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  range(i, 0, v.size()) { os << v[i] << (i + 1 != v.size() ? " " : ""); }
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v)
    is >> in;
  return is;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T> vector<T> make_vector(size_t a, T b) {
  return vector<T>(a, b);
}
template <typename... Ts> auto make_vector(size_t a, Ts... ts) {
  return vector<decltype(make_vector(ts...))>(a, make_vector(ts...));
}

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
// }}}

constexpr int border = 12;
int n;
vector<int> vs, us;
vector<vector<pair<int, int>>> qs;
vector<int> ans;

void dfs(int idx, int dep, const vector<int> &dp, int w, int v) {
  if (idx >= n)
    return;

  for (auto p : qs[idx]) {
    int nw = p.second - w;

    if (nw < 0)
      continue;
    chmax(ans[p.first], v + dp[nw]);

    nw -= us[idx];
    if (nw < 0)
      continue;
    chmax(ans[p.first], v + dp[nw] + vs[idx]);
  }

  if (dep < border) {
    vector<int> dp2 = dp;
    rrange(i, us[idx], dp2.size()) {
      chmax(dp2[i], dp2[i - us[idx]] + vs[idx]);
    }

    dfs(2 * idx + 1, dep + 1, dp2, w, v);
    dfs(2 * idx + 2, dep + 1, dp2, w, v);
  } else {
    dfs(2 * idx + 1, dep + 1, dp, w, v);
    dfs(2 * idx + 2, dep + 1, dp, w, v);
    dfs(2 * idx + 1, dep + 1, dp, w + us[idx], v + vs[idx]);
    dfs(2 * idx + 2, dep + 1, dp, w + us[idx], v + vs[idx]);
  }
}

void solver() {
  cin >> n;

  vs.resize(n);
  us.resize(n);

  range(i, 0, n) { cin >> vs[i] >> us[i]; }

  int q = input();
  qs.resize(n);
  range(i, 0, q) {
    int v = input() - 1, l = input();
    qs[v].emplace_back(i, l);
  }

  vector<int> dp(100001), idx;
  ans.resize(q);
  dfs(0, 0, dp, 0, 0);

  cout << ans << endl;
}

signed main(int argc, char *argv[]) { solver(); }


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// template {{{
#define range(i, l, r) for (int i = (int)(l); i < (int)(r); (i) += 1)
#define rrange(i, l, r) for (int i = (int)(r)-1; i >= (int)(l); (i) -= 1)

#define whole(f, x, ...)                                                       \\
  ([&](decltype((x)) container) {                                              \\
    return (f)(begin(container), end(container), ##__VA_ARGS__);               \\
  })(x)
#define rwhole(f, x, ...)                                                      \\
  ([&](decltype((x)) container) {                                              \\
    return (f)(rbegin(container), rend(container), ##__VA_ARGS__);             \\
  })(x)

#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl

using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;

// constexpr i32 mod   = 998244353;
constexpr i32 mod = 1e9 + 7;
constexpr i32 inf = 1001001001;
constexpr i64 infll = 1001001001001001001ll;

constexpr int dx[] = {0, -1, 1, 0, -1, 1, -1, 1};
constexpr int dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};

struct IoSetup {
  IoSetup(int x = 15) {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(x);
    cerr << fixed << setprecision(x);
  }
} iosetup;

template <typename T = i64> T input() {
  T x;
  cin >> x;
  return x;
}

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  range(i, 0, v.size()) { os << v[i] << (i + 1 != v.size() ? " " : ""); }
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v)
    is >> in;
  return is;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T> vector<T> make_vector(size_t a, T b) {
  return vector<T>(a, b);
}
template <typename... Ts> auto make_vector(size_t a, Ts... ts) {
  return vector<decltype(make_vector(ts...))>(a, make_vector(ts...));
}

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
// }}}

constexpr int border = 12;
int n;
vector<int> vs, us;
vector<vector<pair<int, int>>> qs;
vector<int> ans;

inline void dfs(vector<int> dp, int v, vector<int> &idx) {
  if (v >= n)
    return;
  if (v > (1 << border) - 2) {
    idx.push_back(v);

    for (auto p : qs[v]) {
      range(bit, 0, (1 << idx.size())) {
        int sum_v = 0, sum_w = 0;
        range(i, 0, idx.size()) {
          if (bit & (1 << i)) {
            sum_v += vs[idx[i]];
            sum_w += us[idx[i]];
          }
        }

        if (p.second - sum_w < 0)
          continue;
        chmax(ans[p.first], sum_v + dp[p.second - sum_w]);
      }
    }

    dfs(dp, 2 * v + 1, idx);
    dfs(dp, 2 * v + 2, idx);
    idx.pop_back();
  }

  rrange(i, us[v], dp.size()) { chmax(dp[i], dp[i - us[v]] + vs[v]); }

  for (auto p : qs[v]) {
    ans[p.first] = dp[p.second];
  }

  if (2 * v + 1 > (1 << border) - 2) {
    range(i, 1, dp.size()) { chmax(dp[i], dp[i - 1]); }
  }

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

void solver() {
  cin >> n;

  vs.resize(n);
  us.resize(n);

  range(i, 0, n) { cin >> vs[i] >> us[i]; }

  int q = input();
  qs.resize(n);
  range(i, 0, q) {
    int v = input() - 1, l = input();
    qs[v].emplace_back(i, l);
  }

  vector<int> dp(100001), idx;
  ans.resize(q);
  dfs(dp, 0, idx);

  cout << ans << endl;
}

signed main(int argc, char *argv[]) { solver(); }