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

#include "bits/stdc++.h"
#include <random>
#define REP(i, n) for (ll i = 0; i < ll(n); ++i)
#define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < ll(n); ++i)
#define RFOR(i, m, n) for (ll i = ll(n) - 1; i >= ll(m); --i)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

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

using namespace std;

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int n;
vi ans;
void f(vector<vector<pii>> &query, vi &has, vi &v, vi &w, const vi &dp, int i) {
  auto dp1 = dp;
  RREP(j, dp1.size() - w[i]) { chmax(dp1[j + w[i]], dp1[j] + v[i]); }
  for (auto p : query[i]) {
    ans[p.second] = dp1[p.first];
  }
  if (i * 2 + 1 < n && has[i * 2 + 1])
    f(query, has, v, w, dp1, i * 2 + 1);
  if (i * 2 + 2 < n && has[i * 2 + 2])
    f(query, has, v, w, dp1, i * 2 + 2);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  vi v(n), w(n);
  constexpr int d = 10;
  REP(i, n) { cin >> v[i] >> w[i]; }
  int q;
  cin >> q;
  vector<pii> p(q);
  REP(i, q) {
    cin >> p[i].first >> p[i].second;
    p[i].first--;
  }

  const int SIZE = 1e5 + 1;
  vvl l(1 << d);
  {
    vl dp(SIZE, 0);
    FOR(i, w[0], dp.size()) dp[i] = v[0];
    l[0] = dp;
  }
  FOR(i, 1, min(n, (int)l.size())) {
    l[i] = l[(i - 1) / 2];
    RREP(j, SIZE - w[i]) { chmax(l[i][j + w[i]], l[i][j] + v[i]); }
  }
  REP(i, q) {
    int x = p[i].first, y = p[i].second;
    if (x < l.size())
      cout << l[x][y] << "\\n";
    else {
      vector<pii> k;
      ll ans = -INF;
      while (x >= (1 << d) - 1) {
        k.emplace_back(v[x], w[x]);
        x = (x - 1) / 2;
      }
      REP(i, 1 << (k.size())) {
        ll sumv = 0, sumw = 0;
        REP(j, k.size()) {
          if (i & (1 << j)) {
            sumv += k[j].first;
            sumw += k[j].second;
          }
        }
        if (sumw <= y) {
          chmax(ans, sumv + l[x][y - sumw]);
        }
      }
      cout << ans << "\\n";
    }
  }
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <random>
#define REP(i, n) for (ll i = 0; i < ll(n); ++i)
#define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < ll(n); ++i)
#define RFOR(i, m, n) for (ll i = ll(n) - 1; i >= ll(m); --i)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

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

using namespace std;

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int n;
vi ans;
void f(vector<vector<pii>> &query, vi &has, vi &v, vi &w, const vi &dp, int i) {
  auto dp1 = dp;
  RREP(j, dp1.size() - w[i]) { chmax(dp1[j + w[i]], dp1[j] + v[i]); }
  for (auto p : query[i]) {
    ans[p.second] = dp1[p.first];
  }
  if (i * 2 + 1 < n && has[i * 2 + 1])
    f(query, has, v, w, dp1, i * 2 + 1);
  if (i * 2 + 2 < n && has[i * 2 + 2])
    f(query, has, v, w, dp1, i * 2 + 2);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  vi v(n), w(n);
  constexpr int d = 9;
  REP(i, n) { cin >> v[i] >> w[i]; }
  int q;
  cin >> q;
  vector<pii> p(q);
  REP(i, q) {
    cin >> p[i].first >> p[i].second;
    p[i].first--;
  }

  const int SIZE = 1e5 + 1;
  vvl l(1 << d);
  {
    vl dp(SIZE, 0);
    FOR(i, w[0], dp.size()) dp[i] = v[0];
    l[0] = dp;
  }
  FOR(i, 1, min(n, (int)l.size())) {
    l[i] = l[(i - 1) / 2];
    RREP(j, SIZE - w[i]) { chmax(l[i][j + w[i]], l[i][j] + v[i]); }
  }
  REP(i, q) {
    int x = p[i].first, y = p[i].second;
    if (x < l.size())
      cout << l[x][y] << endl;
    else {
      vector<pii> k;
      ll ans = -INF;
      while (x >= (1 << d) - 1) {
        k.emplace_back(v[x], w[x]);
        x = (x - 1) / 2;
      }
      REP(i, 1 << (k.size())) {
        ll sumv = 0, sumw = 0;
        REP(j, k.size()) {
          if (i & (1 << j)) {
            sumv += k[j].first;
            sumw += k[j].second;
          }
        }
        if (sumw <= y) {
          chmax(ans, sumv + l[x][y - sumw]);
        }
      }
      cout << ans << endl;
    }
  }
}