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

#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>

using namespace std;

int n;
int q;
int v[1 << 18], w[1 << 18];
vector<pair<int, int>> dp[1 << 10];

int fxxk(int x, vector<pair<int, int>> &brute_force) {
  if (x < (1 << 10)) {
    return x;
  }
  int l = brute_force.size();
  for (int i = 0; i < l; i++) {
    brute_force.push_back(
        {brute_force[i].first + w[x], brute_force[i].second + v[x]});
  }
  return fxxk(x / 2, brute_force);
}

void solve(int u) {
  if (!dp[u].empty()) {
    return;
  }
  if (dp[u / 2].empty()) {
    solve(u / 2);
  }
  for (pair<int, int> pii : dp[u / 2]) {
    int ww = pii.first;
    int vv = pii.second;
    dp[u].push_back(pii);
    dp[u].push_back({ww + w[u], vv + v[u]});
  }
  sort(dp[u].begin(), dp[u].end());
  for (int i = 1; i < dp[u].size(); i++) {
    if (dp[u][i].second < dp[u][i - 1].second) {
      dp[u][i].second = dp[u][i - 1].second;
    }
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &v[i], &w[i]);
  }
  dp[1].push_back({0, 0});
  dp[1].push_back({w[1], v[1]});
  for (int i = 1; i <= n && i < (1 << 10); i++) {
    solve(i);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int u, l;
    scanf("%d %d", &u, &l);
    if (u < (1 << 10)) {
      auto it = lower_bound(dp[u].begin(), dp[u].end(), make_pair(l + 1, 0));
      if (it == dp[u].begin()) {
        printf("0\\n");
      } else {
        printf("%d\\n", (--it)->second);
      }
    } else {
      vector<pair<int, int>> brute_force;
      brute_force.push_back({0, 0});
      brute_force.push_back({w[u], v[u]});
      int k = fxxk(u / 2, brute_force);
      sort(brute_force.begin(), brute_force.end(), [](auto x, auto y) {
        if (x.first == y.first) {
          return y.second < x.second;
        } else {
          return x.first < y.first;
        }
      });
      for (int j = 1; j < brute_force.size(); j++) {
        if (brute_force[j].second < brute_force[j - 1].second) {
          brute_force[j].second = brute_force[j - 1].second;
        }
      }
      int llllllll = brute_force.size();
      int p1 = dp[k].size() - 1;
      int p2 = 0;
      int ans = 0;
      while (p2 < llllllll && p1 >= 0) {
        if (dp[k][p1].first + brute_force[p2].first > l) {
          --p1;
        } else {
          ans = max(ans, dp[k][p1].second + brute_force[p2].second);
          ++p2;
        }
      }
      printf("%d\\n", ans);
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>

using namespace std;

int n;
int q;
int v[1 << 18], w[1 << 18];
vector<pair<int, int>> dp[1 << 10];

int fxxk(int x, vector<pair<int, int>> &brute_force) {
  if (x < (1 << 1)) {
    return x;
  }
  int l = brute_force.size();
  for (int i = 0; i < l; i++) {
    brute_force.push_back(
        {brute_force[i].first + w[x], brute_force[i].second + v[x]});
  }
  return fxxk(x / 2, brute_force);
}

void solve(int u) {
  if (!dp[u].empty()) {
    return;
  }
  if (dp[u / 2].empty()) {
    solve(u / 2);
  }
  for (pair<int, int> pii : dp[u / 2]) {
    int ww = pii.first;
    int vv = pii.second;
    dp[u].push_back(pii);
    dp[u].push_back({ww + w[u], vv + v[u]});
  }
  sort(dp[u].begin(), dp[u].end());
  for (int i = 1; i < dp[u].size(); i++) {
    if (dp[u][i].second < dp[u][i - 1].second) {
      dp[u][i].second = dp[u][i - 1].second;
    }
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &v[i], &w[i]);
  }
  dp[1].push_back({0, 0});
  dp[1].push_back({w[1], v[1]});
  for (int i = 1; i <= n && i < (1 << 1); i++) {
    solve(i);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int u, l;
    scanf("%d %d", &u, &l);
    if (u < (1 << 1)) {
      auto it = lower_bound(dp[u].begin(), dp[u].end(), make_pair(l + 1, 0));
      if (it == dp[u].begin()) {
        printf("0\\n");
      } else {
        printf("%d\\n", (--it)->second);
      }
    } else {
      vector<pair<int, int>> brute_force;
      brute_force.push_back({0, 0});
      brute_force.push_back({w[u], v[u]});
      int k = fxxk(u / 2, brute_force);
      sort(brute_force.begin(), brute_force.end(), [](auto x, auto y) {
        if (x.first == y.first) {
          return y.second < x.second;
        } else {
          return x.first < y.first;
        }
      });
      for (int j = 1; j < brute_force.size(); j++) {
        if (brute_force[j].second < brute_force[j - 1].second) {
          brute_force[j].second = brute_force[j - 1].second;
        }
      }
      int llllllll = brute_force.size();
      int p1 = dp[k].size() - 1;
      int p2 = 0;
      int ans = 0;
      while (p2 < llllllll && p1 >= 0) {
        if (dp[k][p1].first + brute_force[p2].first > l) {
          --p1;
        } else {
          ans = max(ans, dp[k][p1].second + brute_force[p2].second);
          ++p2;
        }
      }
      printf("%d\\n", ans);
    }
  }
}