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

#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int lu;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef set<ll> sl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> ii;
typedef pair<ll, ll> il;
#define sz(a) ll((a).size())
#define test(t)                                                                \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define tr(c, i)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define Vi vector
#define pr pair
#define mpr make_pair
#define fi first
#define se second
#define isnum(a) (a >= 48 && a <= 57)
#define isS(a) (a >= 'a' && a <= 'z')
#define isU(a) (a >= 'A' && a <= 'Z')
#define toS(a) (isU(a) ? a + 32 : a)
#define toU(a) (isS(a) ? a - 32 : a)
#define toC(a) a & 15

vector<ll> sprime;
ll d[100005];

vector<char> prime(1000000 + 5, true);
void sieve(ll n) {
  // O(nloglogn) time
  prime[0] = prime[1] = false;
  for (ll i = 2; i <= n; ++i) {
    if (prime[i]) {
      sprime.pb(i);
      for (ll j = 2 * i; j <= n; j += i) {
        prime[j] = false;
      }
    }
  }
}
#define forn(i, e) for (int i = 0; i < e; i++)
#define rep(i, a, b) for (i = a; i < b; i++)
#define REP(i, a, b) for (i = a; i <= b; i++)
#define reprev(i, a, b) for (i = a; i >= b; i--)

// Convex Hull Trick
typedef ll LT;
typedef pair<LT, LT> LP;
struct CHT {
  deque<LP> d;
  LT get(LT x) {
    while (sz(d) >= 2) {
      LT a = d[0].fi * x + d[0].se;
      LT b = d[1].fi * x + d[1].se;
      if (a <= b)
        break; // get min
      d.pop_front();
    }
    return d[0].fi * x + d[0].se;
  }
  void add(LP x) { // decreasing order of x.fi
    while (sz(d) >= 2) {
      LP y = d[sz(d) - 1];
      LP z = d[sz(d) - 2];
      if ((x.se - y.se) * (z.fi - y.fi) >= (y.fi - x.fi) * (y.se - z.se))
        break; // both
      d.pop_back();
    }
    d.pb(x);
  }
};
//

#define leadzero(a) __builtin_clz(a)      // count leading zeros
#define trailzero(a) __builtin_ctz(a)     // count trailing zeros
#define bitcount(a) __builtin_popcount(a) // count set bits (add ll)
#define ln cout << endl
#define sp cout << " "
#define maxe(a) *max_element(a.begin(), a.end())
#define maxi(a) max_element(a.begin(), a.end()) - a.begin()
#define mine(a) *min_element(a.begin(), a.end())
#define mini(a) min_element(a.begin(), a.end()) - a.begin()
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll fastpowMOD(ll a, ll p, ll MOD) {
  if (p == 0)
    return 1;
  ll z = fastpowMOD(a, p / 2, MOD);
  z = (z * z) % MOD;
  if (p % 2)
    z = (z * a) % MOD;
  return z;
}
ll fastpow(ll a, ll p) {
  if (p == 0)
    return 1;
  ll z = fastpow(a, p / 2);
  z = (z * z);
  if (p % 2)
    z = (z * a);
  return z;
}
// CHT stands for convex hull trick
ll search1(ll a[], ll f, ll n) {
  ll left = 1;
  ll right = n + 1;
  while (right - left > 1) {
    ll mid = left + (right - left) / 2;
    if (a[mid] < f) {
      left = mid;
    } else
      right = mid;
  }
  return a[left];
}

const ll INF = 1e18;
vector<ll> lazy;
vector<ll> tree;

void updateRange(ll node, ll start, ll end, ll l, ll r, ll val) {
  if (lazy[node] != 0) {
    // This node needs to be updated
    tree[node] += (/*end - start*/ +1) * lazy[node]; // Update it
    if (start != end) {
      lazy[node * 2] += lazy[node];     // Mark child as lazy
      lazy[node * 2 + 1] += lazy[node]; // Mark child as lazy
    }
    lazy[node] = 0; // Reset it
  }
  if (start > end or start > r or
      end < l) // Current segment is not within range [l, r]
    return;
  if (start >= l and end <= r) {
    // Segment is fully within range
    lazy[node] += (/*end - start*/ +1) * val;
    if (start != end) {
      // Not leaf node
      lazy[node * 2] += lazy[node];
      lazy[node * 2 + 1] += lazy[node];
    }
    tree[node] += lazy[node];
    lazy[node] = 0;
    return;
  }
  ll mid = (start + end) / 2;
  updateRange(node * 2, start, mid, l, r, val);       // Updating left child
  updateRange(node * 2 + 1, mid + 1, end, l, r, val); // Updating right child
  tree[node] =
      max(tree[node * 2], tree[node * 2 + 1]); // Updating root with max value
}

ll queryRange(ll node, ll start, ll end, ll l, ll r) {
  if (lazy[node] != 0) {
    // This node needs to be updated
    tree[node] += (/*end - start +*/ 1) * lazy[node]; // Update it
    if (start != end) {
      lazy[node * 2] += lazy[node];     // Mark child as lazy
      lazy[node * 2 + 1] += lazy[node]; // Mark child as lazy
    }
    lazy[node] = 0; // Reset it
  }

  if (start > end or start > r or end < l)
    return -INF; // Out of range

  if (start >= l and end <= r) // Current segment is totally within range [l, r]
    return tree[node];
  ll mid = (start + end) / 2;
  if (r <= mid)
    return queryRange(node * 2, start, mid, l, r);
  else if (l >= mid + 1)
    return queryRange(node * 2 + 1, mid + 1, end, l, r);
  ll p1 = queryRange(node * 2, start, mid, l, r);       // Query left child
  ll p2 = queryRange(node * 2 + 1, mid + 1, end, l, r); // Query right child
  // tree[node] = max(tree[node*2],tree[node*2+1]);
  return max(p1, p2);
}
void convert_reduce(ll arr[], ll n) {
  vector<pair<ll, ll>> v;
  for (ll i = 0; i < n; i++)
    v.push_back(make_pair(arr[i], i));
  sort(v.begin(), v.end());
  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    arr[v[i].second] = cnt;
    ll j = i;
    while (j < n && v[j].fi == v[i].fi)
      arr[v[j++].second] = cnt;
    i = j - 1;
    cnt++;
  }
}

// for bfs
ll dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

bool valid(ll i, ll j, ll n, ll m) {
  return i >= 1 && i <= n && j >= 1 && j <= m;
}

ll MOD = 998244353;

/*
D - Knapsack Queries on a tree
https://atcoder.jp/contests/tokiomarine2020/tasks/tokiomarine2020_d
https://img.atcoder.jp/tokiomarine2020/editorial.pdf

For simplicity, let N = 2^(2K) −1. Also, let M := max Li. We will
introduce an algorithm that works in O(N +2^K(M +Q)) time. In this
problem, K = 9 and M = 105, so 2^K(M +Q) ≈ 10^8, and thus it will
run in time.
First, for each vertex v at depth K −1 or shallower and each
non-negative integer k at most M, we compute Dv,k: the maximum possible
total value of items when we choose items from v and its ancestors so
that the total weight is at most k. By computing them in the order
from distance from the root, we can find all Dv,k in O(2KM) time.
Then, we will process the query. Let v be the specified vertex.
Among the vertices that are v or its ancestors, at most K have
depths of K or deeper. Thus, we can try all subsets of the
items in the vertices at depth K or deeper in O(2K) time. After
deciding the subset to choose from the vertices at depth K or deeper,
we can use D∗,∗ to know the optimal subset to choose
from the vertices at depth K − 1 or shallower in O(1) time.
Thus, we can process each query in O(2^K) time.
Therefore, we can solve the problem in a total of O(N + 2^K(M + Q)) time.

*/
const ll N = (ll)3e5 + 5;
ll v[N], w[N];
ll dp[(1 << 9) + 5][(ll)1e5 + 4];

int main() {
  // fastio;
  ll n, i, j, k, q;
  // cin>>n;
  scanf("%lld", &n);
  REP(i, 1, n) scanf("%lld %lld", &v[i], &w[i]);
  ll lim = (1 << 9);
  // ll dp[lim+5][(ll)1e5];
  memset(dp, 0, sizeof(dp));
  rep(i, 1, min(lim, n + 1)) {
    ll par = i / 2;
    rep(j, 0, (ll)1e5 + 1) {
      dp[i][j] = dp[par][j];
      if (j >= w[i]) {
        dp[i][j] = max(dp[i][j], dp[par][j - w[i]] + v[i]);
      }
      if (j > 0)
        dp[i][j] = max(dp[i][j], dp[i][j - 1]);
    }
  }
  scanf("%lld", &q);
  ll val[11];
  REP(i, 0, 10) val[i] = (1 << i);
  while (q--) {
    ll u, L;
    scanf("%lld %lld", &u, &L);
    if (u < lim) {
      printf("%lld\\n", dp[u][L]);
      continue;
    }
    vl path;
    ll tm = u;
    while (tm > 0) {
      path.pb(tm);
      tm /= 2;
    }
    ll len = sz(path) - 9;
    ll ans = 0;
    rep(i, 0, val[len]) {
      ll sumv = 0, sumw = 0;
      rep(j, 0, len) {
        if (i >> j & 1) {
          sumv += v[path[j]];
          sumw += w[path[j]];
        }
      }
      if (sumw <= L) {
        ans = max(ans, sumv + dp[path[len]][L - sumw]);
      }
    }
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int lu;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef set<ll> sl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> ii;
typedef pair<ll, ll> il;
#define sz(a) ll((a).size())
#define test(t)                                                                \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define tr(c, i)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define Vi vector
#define pr pair
#define mpr make_pair
#define fi first
#define se second
#define isnum(a) (a >= 48 && a <= 57)
#define isS(a) (a >= 'a' && a <= 'z')
#define isU(a) (a >= 'A' && a <= 'Z')
#define toS(a) (isU(a) ? a + 32 : a)
#define toU(a) (isS(a) ? a - 32 : a)
#define toC(a) a & 15

vector<ll> sprime;
ll d[100005];

vector<char> prime(1000000 + 5, true);
void sieve(ll n) {
  // O(nloglogn) time
  prime[0] = prime[1] = false;
  for (ll i = 2; i <= n; ++i) {
    if (prime[i]) {
      sprime.pb(i);
      for (ll j = 2 * i; j <= n; j += i) {
        prime[j] = false;
      }
    }
  }
}
#define forn(i, e) for (int i = 0; i < e; i++)
#define rep(i, a, b) for (i = a; i < b; i++)
#define REP(i, a, b) for (i = a; i <= b; i++)
#define reprev(i, a, b) for (i = a; i >= b; i--)

// Convex Hull Trick
typedef ll LT;
typedef pair<LT, LT> LP;
struct CHT {
  deque<LP> d;
  LT get(LT x) {
    while (sz(d) >= 2) {
      LT a = d[0].fi * x + d[0].se;
      LT b = d[1].fi * x + d[1].se;
      if (a <= b)
        break; // get min
      d.pop_front();
    }
    return d[0].fi * x + d[0].se;
  }
  void add(LP x) { // decreasing order of x.fi
    while (sz(d) >= 2) {
      LP y = d[sz(d) - 1];
      LP z = d[sz(d) - 2];
      if ((x.se - y.se) * (z.fi - y.fi) >= (y.fi - x.fi) * (y.se - z.se))
        break; // both
      d.pop_back();
    }
    d.pb(x);
  }
};
//

#define leadzero(a) __builtin_clz(a)      // count leading zeros
#define trailzero(a) __builtin_ctz(a)     // count trailing zeros
#define bitcount(a) __builtin_popcount(a) // count set bits (add ll)
#define ln cout << endl
#define sp cout << " "
#define maxe(a) *max_element(a.begin(), a.end())
#define maxi(a) max_element(a.begin(), a.end()) - a.begin()
#define mine(a) *min_element(a.begin(), a.end())
#define mini(a) min_element(a.begin(), a.end()) - a.begin()
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll fastpowMOD(ll a, ll p, ll MOD) {
  if (p == 0)
    return 1;
  ll z = fastpowMOD(a, p / 2, MOD);
  z = (z * z) % MOD;
  if (p % 2)
    z = (z * a) % MOD;
  return z;
}
ll fastpow(ll a, ll p) {
  if (p == 0)
    return 1;
  ll z = fastpow(a, p / 2);
  z = (z * z);
  if (p % 2)
    z = (z * a);
  return z;
}
// CHT stands for convex hull trick
ll search1(ll a[], ll f, ll n) {
  ll left = 1;
  ll right = n + 1;
  while (right - left > 1) {
    ll mid = left + (right - left) / 2;
    if (a[mid] < f) {
      left = mid;
    } else
      right = mid;
  }
  return a[left];
}

const ll INF = 1e18;
vector<ll> lazy;
vector<ll> tree;

void updateRange(ll node, ll start, ll end, ll l, ll r, ll val) {
  if (lazy[node] != 0) {
    // This node needs to be updated
    tree[node] += (/*end - start*/ +1) * lazy[node]; // Update it
    if (start != end) {
      lazy[node * 2] += lazy[node];     // Mark child as lazy
      lazy[node * 2 + 1] += lazy[node]; // Mark child as lazy
    }
    lazy[node] = 0; // Reset it
  }
  if (start > end or start > r or
      end < l) // Current segment is not within range [l, r]
    return;
  if (start >= l and end <= r) {
    // Segment is fully within range
    lazy[node] += (/*end - start*/ +1) * val;
    if (start != end) {
      // Not leaf node
      lazy[node * 2] += lazy[node];
      lazy[node * 2 + 1] += lazy[node];
    }
    tree[node] += lazy[node];
    lazy[node] = 0;
    return;
  }
  ll mid = (start + end) / 2;
  updateRange(node * 2, start, mid, l, r, val);       // Updating left child
  updateRange(node * 2 + 1, mid + 1, end, l, r, val); // Updating right child
  tree[node] =
      max(tree[node * 2], tree[node * 2 + 1]); // Updating root with max value
}

ll queryRange(ll node, ll start, ll end, ll l, ll r) {
  if (lazy[node] != 0) {
    // This node needs to be updated
    tree[node] += (/*end - start +*/ 1) * lazy[node]; // Update it
    if (start != end) {
      lazy[node * 2] += lazy[node];     // Mark child as lazy
      lazy[node * 2 + 1] += lazy[node]; // Mark child as lazy
    }
    lazy[node] = 0; // Reset it
  }

  if (start > end or start > r or end < l)
    return -INF; // Out of range

  if (start >= l and end <= r) // Current segment is totally within range [l, r]
    return tree[node];
  ll mid = (start + end) / 2;
  if (r <= mid)
    return queryRange(node * 2, start, mid, l, r);
  else if (l >= mid + 1)
    return queryRange(node * 2 + 1, mid + 1, end, l, r);
  ll p1 = queryRange(node * 2, start, mid, l, r);       // Query left child
  ll p2 = queryRange(node * 2 + 1, mid + 1, end, l, r); // Query right child
  // tree[node] = max(tree[node*2],tree[node*2+1]);
  return max(p1, p2);
}
void convert_reduce(ll arr[], ll n) {
  vector<pair<ll, ll>> v;
  for (ll i = 0; i < n; i++)
    v.push_back(make_pair(arr[i], i));
  sort(v.begin(), v.end());
  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    arr[v[i].second] = cnt;
    ll j = i;
    while (j < n && v[j].fi == v[i].fi)
      arr[v[j++].second] = cnt;
    i = j - 1;
    cnt++;
  }
}

// for bfs
ll dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

bool valid(ll i, ll j, ll n, ll m) {
  return i >= 1 && i <= n && j >= 1 && j <= m;
}

ll MOD = 998244353;

/*
D - Knapsack Queries on a tree
https://atcoder.jp/contests/tokiomarine2020/tasks/tokiomarine2020_d
https://img.atcoder.jp/tokiomarine2020/editorial.pdf

For simplicity, let N = 2^(2K) −1. Also, let M := max Li. We will
introduce an algorithm that works in O(N +2^K(M +Q)) time. In this
problem, K = 9 and M = 105, so 2^K(M +Q) ≈ 10^8, and thus it will
run in time.
First, for each vertex v at depth K −1 or shallower and each
non-negative integer k at most M, we compute Dv,k: the maximum possible
total value of items when we choose items from v and its ancestors so
that the total weight is at most k. By computing them in the order
from distance from the root, we can find all Dv,k in O(2KM) time.
Then, we will process the query. Let v be the specified vertex.
Among the vertices that are v or its ancestors, at most K have
depths of K or deeper. Thus, we can try all subsets of the
items in the vertices at depth K or deeper in O(2K) time. After
deciding the subset to choose from the vertices at depth K or deeper,
we can use D∗,∗ to know the optimal subset to choose
from the vertices at depth K − 1 or shallower in O(1) time.
Thus, we can process each query in O(2^K) time.
Therefore, we can solve the problem in a total of O(N + 2^K(M + Q)) time.

*/
const ll N = (ll)3e5 + 5;
ll v[N], w[N];
ll dp[(1 << 9) + 5][(ll)1e5 + 4];

int main() {
  // fastio;
  ll n, i, j, k, q;
  // cin>>n;
  scanf("%lld", &n);
  REP(i, 1, n) scanf("%lld %lld", &v[i], &w[i]);
  ll lim = (1 << 9);
  // ll dp[lim+5][(ll)1e5];
  memset(dp, 0, sizeof(dp));
  rep(i, 1, min(lim, n + 1)) {
    ll par = i / 2;
    rep(j, 0, (ll)1e5 + 1) {
      dp[i][j] = dp[par][j];
      if (j >= w[i]) {
        dp[i][j] = max(dp[i][j], dp[par][j - w[i]] + v[i]);
      }
      if (j > 0)
        dp[i][j] = max(dp[i][j], dp[i][j - 1]);
    }
  }
  scanf("%lld", &q);
  ll val[11];
  REP(i, 0, 10) val[i] = (1 << i);
  while (q--) {
    ll u, L;
    scanf("%lld %lld", &u, &L);
    if (u < lim) {
      printf("%lld\\n", dp[u][L]);
      continue;
    }
    vl path;
    ll tm = u;
    while (tm > 0) {
      path.pb(tm);
      tm /= 2;
    }
    ll len = sz(path) - 9;
    ll ans = 0;
    rep(i, 0, val[len]) {
      ll sumv = 0, sumw = 0;
      rep(j, 0, len) {
        if (i & (1 << j)) {
          sumv += v[path[j]];
          sumw += w[path[j]];
        }
      }
      if (sumw <= L) {
        ans = max(ans, sumv + dp[path[len]][L - sumw]);
      }
    }
    printf("%lld\\n", ans);
  }
  return 0;
}