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

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pi;
#define f first
#define s second
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
const int maxn = (1 << 18) + 50;
const int maxw = 100000;
const int INF = LLONG_MAX / 4;

int n;
int q;

int weight[maxn], value[maxn];
int dp[512][100010];

int solve(int v, int wleft) {
  if (v < 512)
    return dp[v][wleft];
  int res = solve(v / 2, wleft);
  if (wleft >= weight[v])
    res = max(res, solve(v / 2, wleft - weight[v]) + value[v]);

  return res;
}

int32_t main() {
  FAST cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> value[i] >> weight[i];
  }

  for (int i = 1; i < min(n + 1, (1ll << 9)); i++) {
    for (int j = 0; j <= maxw; j++) {
      dp[i][j] = dp[i / 2][j];
      if (j >= weight[i])
        dp[i][j] = max(dp[i][j], dp[i / 2][j - weight[i]] + value[i]);
    }
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    int V, L;
    cin >> V >> L;
    cout << solve(V, L) << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pi;
#define f first
#define s second
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
const int maxn = (1 << 18) + 50;
const int INF = LLONG_MAX / 4;

int n;
int value[maxn];
int weight[maxn];

int q;

vector<pi> lefta[maxn];
vector<pi> righta[maxn];

bool cmp(pi a, pi b) {
  if (a.f == b.f)
    return a.s > b.s;
  else
    return a.f < b.f;
}

void dfs(int x, int p) {
  if (x > n)
    return;
  int lc = x * 2, rc = x * 2 + 1;
  // cout << x << "\\n";

  if (lefta[p].size() > righta[p].size()) { // put in righta
    vector<pi> temp;
    for (auto cur : righta[p]) {
      temp.push_back(cur);
      temp.push_back(pi(cur.f + weight[x], cur.s + value[x]));
    }

    sort(temp.begin(), temp.end(), cmp);
    for (auto cur : temp) {
      if (righta[x].empty() or cur.s > righta[x].back().s) {
        righta[x].push_back(cur);
      }
    }
    lefta[x] = lefta[p];
  } else { // put in lefta
    vector<pi> temp;
    for (auto cur : lefta[p]) {
      temp.push_back(cur);
      temp.push_back(pi(cur.f + weight[x], cur.s + value[x]));
    }

    sort(temp.begin(), temp.end(), cmp);
    for (auto cur : temp) {
      if (lefta[x].empty() or cur.s > lefta[x].back().s) {
        lefta[x].push_back(cur);
      }
    }
    righta[x] = righta[p];
  }

  // if (x == 9) {
  // cout << "LEFT\\n";
  // for (auto cur: lefta[x]) cout << cur.f << " " << cur.s << "\\n";
  // cout << "RIGHT\\n";
  // for (auto cur: righta[x]) cout << cur.f << " " << cur.s << "\\n";
  // cout << "\\n";
  //}

  if (lc <= n) {
    dfs(lc, x);
  }

  if (rc <= n) {
    dfs(rc, x);
  }
}

int32_t main() {
  FAST cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> value[i] >> weight[i];
  }

  lefta[0].push_back(pi(0, 0));
  righta[0].push_back(pi(0, 0));
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    reverse(righta[i].begin(), righta[i].end());

  cin >> q;
  for (int i = 0; i < q; i++) {
    int v, L;
    cin >> v >> L;
    int ans = 0;

    auto it = righta[v].begin();
    for (auto cur : lefta[v]) {
      if (cur.f > L)
        break;
      int avail = L - cur.f;

      while ((*it).f > avail)
        it++;
      ans = max(ans, (*it).s + cur.s);
    }
    cout << ans << "\\n";
  }
}