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

#include <bits/stdc++.h>
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for (ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T> constexpr bool chmax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}
template <typename T> constexpr bool chmin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

/*-------------------------------------------*/

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<ll> v(n + 1), w(n + 1);
  rep(i, n) cin >> v[i + 1] >> w[i + 1];

  vector<pair<int, int>> x, y[10001];
  vector<int> a, b;

  y[0].emplace_back(0, 0);
  for (int i = 1; i <= min(n, 10000); i++) {
    x.clear();
    for (auto [p, q] : y[i >> 1]) {
      x.emplace_back(p, q);
      if (p + w[i] <= 100000)
        x.emplace_back(p + w[i], q + v[i]);
    }
    sort(ALL(x));
    int mx = -1;
    for (auto p : x)
      if (chmax(mx, p.second))
        y[i].push_back(p);
  }

  int m;
  cin >> m;
  rep(_, m) {
    int q, l;
    cin >> q >> l;
    if (q <= 10000)
      cout << (upper_bound(ALL(y[q]), make_pair(l, 1 << 30)) - 1)->second
           << "\\n";
    else {
      a.clear();
      b.clear();
      int N = 0;
      for (; q > 10000; q >>= 1) {
        a.push_back(v[q]);
        b.push_back(w[q]);
        N++;
      }
      int ret = -1;
      rep(i, 1 << N) {
        int sumv = 0, sumw = 0;
        rep(j, N) if (i >> j & 1) {
          sumv += a[j];
          sumw += b[j];
        }
        if (sumw <= l) {
          chmax(ret,
                sumv +
                    (upper_bound(ALL(y[q]), make_pair(l - sumw, 1 << 30)) - 1)
                        ->second);
        }
      }
      cout << ret << "\\n";
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for (ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T> constexpr bool chmax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}
template <typename T> constexpr bool chmin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

/*-------------------------------------------*/

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<ll> v(n + 1), w(n + 1);
  rep(i, n) cin >> v[i + 1] >> w[i + 1];

  vector<pair<int, int>> x, y[1000];
  vector<int> a, b;

  y[0].emplace_back(0, 0);
  for (int i = 1; i * i <= n; i++) {
    x.clear();
    for (auto [p, q] : y[i >> 1]) {
      x.emplace_back(p, q);
      if (p + w[i] <= 100000)
        x.emplace_back(p + w[i], q + v[i]);
    }
    sort(ALL(x));
    int mx = -1;
    for (auto p : x)
      if (chmax(mx, p.second))
        y[i].push_back(p);
  }

  int m;
  cin >> m;
  rep(_, m) {
    int q, l;
    cin >> q >> l;
    if ((ll)q * q <= n)
      cout << (upper_bound(ALL(y[q]), make_pair(l, 1 << 30)) - 1)->second
           << "\\n";
    else {
      a.clear();
      b.clear();
      int N = 0;
      for (; (ll)q * q > n; q >>= 1) {
        a.push_back(v[q]);
        b.push_back(w[q]);
        N++;
      }
      int ret = -1;
      rep(i, 1 << N) {
        int sumv = 0, sumw = 0;
        rep(j, N) if (i >> j & 1) {
          sumv += a[j];
          sumw += b[j];
        }
        if (sumw <= l) {
          chmax(ret,
                sumv +
                    (upper_bound(ALL(y[q]), make_pair(l - sumw, 1 << 30)) - 1)
                        ->second);
        }
      }
      cout << ret << "\\n";
    }
  }

  return 0;
}