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

#include <algorithm>
#include <stdio.h>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int n, q, v[300000], w[300000], h[300000];
vector<pii> memo[(1 << 12) + 100];
void rec(vector<pii> &r, int x) {
  if (x) {
    r.emplace_back(w[x], v[x]);
    rec(r, x / 2);
  }
}
int rec2(vector<pii> &r, int x) {
  if (h[x] == 12)
    return x;
  r.emplace_back(w[x], v[x]);
  return rec2(r, x / 2);
}
vector<pii> ref(vector<pii> &s) {
  sort(s.begin(), s.end());
  vector<pii> r;
  r.emplace_back(s[0]);
  for (int i = 1; i < s.size(); i++)
    if (r.back().second < s[i].second)
      r.emplace_back(s[i]);
  return r;
}
vector<pii> enu(const vector<pii> &s) {
  vector<pii> r;
  r.emplace_back(0, 0);
  for (int i = 0; i < s.size(); i++) {
    int c = r.size();
    for (int j = 0; j < c; j++)
      r.emplace_back(r[j].first + s[i].first, r[j].second + s[i].second);
  }
  return ref(r);
}
vector<pii> enu2(const vector<pii> &s, pii v) {
  vector<pii> r(s.begin(), s.end());
  int c = r.size();
  for (int i = 0; i < c; i++)
    r.emplace_back(r[i].first + v.first, r[i].second + v.second);
  return ref(r);
}

int solve(int u, int l) {
  vector<pii> p;
  if (h[u] <= 12)
    return upper_bound(memo[u].begin(), memo[u].end(), pii(l, 2e9))[-1].second;
  int cen = rec2(p, u);
  vector<pii> v = memo[cen];
  vector<pii> w = enu(p);
  if (v.size() > w.size())
    v.swap(w);
  int max_v = 0;
  for (int i = 0; i < v.size() && v[i].first <= l; i++) {
    int val =
        upper_bound(w.begin(), w.end(), pii(l - v[i].first, 1e9))[-1].second;
    max_v = max(max_v, v[i].second + val);
  }
  return max_v;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d %d", v + i, w + i), h[i] = h[i / 2] + 1;
  h[n + 1] = 1e9;
  memo[0].emplace_back(0, 0);
  for (int i = 1; h[i] <= 12; i++)
    memo[i] = enu2(memo[i / 2], pii(w[i], v[i]));
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int u, l;
    scanf("%d %d", &u, &l);
    printf("%d\\n", solve(u, l));
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int n, q, v[300000], w[300000], h[300000];
vector<pii> memo[256];
void rec(vector<pii> &r, int x) {
  if (x) {
    r.emplace_back(w[x], v[x]);
    rec(r, x / 2);
  }
}
int rec2(vector<pii> &r, int x) {
  if (h[x] == 9)
    return x;
  r.emplace_back(w[x], v[x]);
  return rec2(r, x / 2);
}
vector<pii> ref(vector<pii> &s) {
  sort(s.begin(), s.end());
  vector<pii> r;
  r.emplace_back(s[0]);
  for (int i = 1; i < s.size(); i++)
    if (r.back().second < s[i].second)
      r.emplace_back(s[i]);
  return r;
}
vector<pii> enu(const vector<pii> &s) {
  vector<pii> r;
  r.emplace_back(0, 0);
  for (int i = 0; i < s.size(); i++) {
    int c = r.size();
    for (int j = 0; j < c; j++)
      r.emplace_back(r[j].first + s[i].first, r[j].second + s[i].second);
  }
  return ref(r);
}

int solve(int u, int l) {
  vector<pii> p;
  if (h[u] < 9) {
    rec(p, u);
    vector<pii> q = enu(p);
    return upper_bound(q.begin(), q.end(), pii(l, 2e9))[-1].second;
  }
  if (h[u] == 9)
    return upper_bound(memo[u - 256].begin(), memo[u - 256].end(),
                       pii(l, 2e9))[-1]
        .second;
  int cen = rec2(p, u);
  vector<pii> v = memo[cen - 256];
  vector<pii> w = enu(p);
  if (v.size() > w.size())
    v.swap(w);
  int max_v = 0;
  for (int i = 0; i < v.size() && v[i].first <= l; i++) {
    int val =
        upper_bound(w.begin(), w.end(), pii(l - v[i].first, 1e9))[-1].second;
    max_v = max(max_v, v[i].second + val);
  }
  return max_v;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d %d", v + i, w + i), h[i] = h[i / 2] + 1;
  for (int i = 0; i < 256; i++) {
    vector<pii> p;
    rec(p, i + 256);
    memo[i] = enu(p);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int u, l;
    scanf("%d %d", &u, &l);
    printf("%d\\n", solve(u, l));
  }
}