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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits.h>
#include <map>
#include <utility>
#include <vector>
using namespace std;
using P = pair<int, int>;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int MAX = (1 << 18) + 10;
const int CHANGE = 1024;
int n;
vector<int> v(MAX), w(MAX);
vector<vector<ll>> d(CHANGE, vector<ll>(100010, 0));
void dfs(int idx) {
  if (idx * 2 > n || idx * 2 >= CHANGE)
    return;
  for (int i = 0; i < 100010; i++) {
    chmax(d[idx * 2][i], d[idx][i]);
    if (i + w[idx * 2] < 100010)
      chmax(d[idx * 2][i + w[idx * 2]], d[idx][i] + v[idx * 2]);
  }
  for (int i = 1; i < 100010; i++) {
    chmax(d[idx * 2][i], d[idx * 2][i - 1]);
  }
  dfs(idx * 2);
  if (idx * 2 + 1 > n || idx * 2 + 1 >= CHANGE)
    return;
  for (int i = 0; i < 100010; i++) {
    chmax(d[idx * 2 + 1][i], d[idx][i]);
    if (i + w[idx * 2 + 1] < 100010)
      chmax(d[idx * 2 + 1][i + w[idx * 2 + 1]], d[idx][i] + v[idx * 2 + 1]);
  }
  for (int i = 1; i < 100010; i++) {
    chmax(d[idx * 2 + 1][i], d[idx * 2 + 1][i - 1]);
  }
  dfs(idx * 2 + 1);
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];
  d[1][w[1]] = v[1];
  for (int i = 1; i < 100010; i++)
    chmax(d[1][i], d[1][i - 1]);
  dfs(1);
  int q;
  cin >> q;
  while (q--) {
    int u, l;
    ll ret = 0;
    cin >> u >> l;
    if (u < CHANGE) {
      ret = d[u][l];
    } else {
      vector<int> root;
      int now = u;
      while (now >= CHANGE) {
        root.emplace_back(now);
        now /= 2;
      }
      for (int bit = 0; bit < (1 << root.size()); bit++) {
        ll vcnt = 0, wcnt = 0;
        for (int i = 0; i < root.size(); i++) {
          if (bit & (1 << i)) {
            vcnt += v[root[i]];
            wcnt += w[root[i]];
          }
        }
        if (wcnt <= l)
          chmax(ret, vcnt + d[now][l - wcnt]);
      }
    }
    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits.h>
#include <map>
#include <utility>
#include <vector>
using namespace std;
using P = pair<int, int>;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int MAX = (1 << 18) + 10;
int n;
vector<int> v(MAX), w(MAX);
vector<vector<ll>> d(512, vector<ll>(100010, 0));
void dfs(int idx) {
  if (idx * 2 > n || idx * 2 >= 512)
    return;
  for (int i = 0; i < 100010; i++) {
    chmax(d[idx * 2][i], d[idx][i]);
    if (i + w[idx * 2] < 100010)
      chmax(d[idx * 2][i + w[idx * 2]], d[idx][i] + v[idx * 2]);
  }
  for (int i = 1; i < 100010; i++) {
    chmax(d[idx * 2][i], d[idx * 2][i - 1]);
  }
  dfs(idx * 2);
  if (idx * 2 + 1 > n || idx * 2 + 1 >= 512)
    return;
  for (int i = 0; i < 100010; i++) {
    chmax(d[idx * 2 + 1][i], d[idx][i]);
    if (i + w[idx * 2 + 1] < 100010)
      chmax(d[idx * 2 + 1][i + w[idx * 2 + 1]], d[idx][i] + v[idx * 2 + 1]);
  }
  for (int i = 1; i < 100010; i++) {
    chmax(d[idx * 2 + 1][i], d[idx * 2 + 1][i - 1]);
  }
  dfs(idx * 2 + 1);
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];
  d[1][w[1]] = v[1];
  for (int i = 1; i < 100010; i++)
    chmax(d[1][i], d[1][i - 1]);
  dfs(1);
  int q;
  cin >> q;
  while (q--) {
    int u, l;
    ll ret = 0;
    cin >> u >> l;
    if (u < 512) {
      ret = d[u][l];
    } else {
      vector<int> root;
      int now = u;
      while (now >= 512) {
        root.emplace_back(now);
        now /= 2;
      }
      for (int bit = 0; bit < (1 << root.size()); bit++) {
        ll vcnt = 0, wcnt = 0;
        for (int i = 0; i < root.size(); i++) {
          if (bit & (1 << i)) {
            vcnt += v[root[i]];
            wcnt += w[root[i]];
          }
        }
        if (wcnt <= l)
          chmax(ret, vcnt + d[now][l - wcnt]);
      }
    }
    cout << ret << endl;
  }
}