alexjercan/bugnet huggingface dataset:
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int const N = (1 << 18) + 7;

struct Node {
  int value;
  int weigh;
};

Node ar[N];
int dp[512][100001];

int solve(int vertex, int Limit) {
  if (vertex < 512)
    return dp[vertex][Limit];

  if (Limit < ar[vertex].weigh)
    return solve(vertex / 2, Limit);
  return max(solve(vertex / 2, Limit),
             ar[vertex].value + solve(vertex / 2, Limit - ar[vertex].weigh));
}

int main() {
  int n, q;
  int v, w;
  scanf("%d", &n);

  for (int i = 1; i <= n; ++i)
    scanf("%d %d", &ar[i].value, &ar[i].weigh);

  // Fill dp
  for (int i = 0; i <= 1e5; ++i) {
    if (i < ar[1].weigh)
      dp[1][i] = 0;
    else
      dp[1][i] = ar[1].value;
  }

  for (int i = 2; i <= min(511, n); ++i) {
    for (int j = 0; j <= 1e5; ++j) {
      if (j < ar[i].weigh)
        dp[i][j] = dp[i / 2][j];
      else
        dp[i][j] = max(dp[i / 2][j], ar[i].value + dp[i / 2][j - ar[i].weigh]);
    }
  }

  scanf("%d", &q);
  for (int i = 1; i <= q; ++i) {
    scanf("%d %d", &v, &w);
    printf("%d\\n", solve(v, w));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int const N = (1 << 18) + 7;

struct Node {
  int value;
  int weigh;
};

Node ar[N];
map<int, int> dp[512]; // weigh, value

int solve(int vertex, int Limit) {
  if (vertex == 1)
    return (Limit >= ar[vertex].weigh ? ar[vertex].value : 0);
  if (vertex < 512 && dp[vertex].count(Limit))
    return dp[vertex][Limit];

  if (ar[vertex].weigh > Limit) {
    int res = solve(vertex / 2, Limit);

    if (vertex < 512)
      dp[vertex][Limit] = res;

    return res;
  }

  int res = max(solve(vertex / 2, Limit),
                ar[vertex].value + solve(vertex / 2, Limit - ar[vertex].weigh));

  if (vertex < 512)
    dp[vertex][Limit] = res;
  return res;
}

int main() {
  ios ::sync_with_stdio();
  cin.tie(0);
  cout.tie(0);

  int n, q;
  int v, w;
  scanf("%d", &n);

  for (int i = 1; i <= n; ++i)
    scanf("%d %d", &ar[i].value, &ar[i].weigh);

  scanf("%d", &q);
  for (int i = 1; i <= q; ++i) {
    scanf("%d %d", &v, &w);
    printf("%d\\n", solve(v, w));
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <ctime>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> V(N + 1);
  vector<int> W(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> V[i] >> W[i];

  int Q;
  cin >> Q;
  vector<int> v(Q);
  vector<int> L(Q);
  for (int i = 0; i < Q; i++)
    cin >> v[i] >> L[i];

  int D = 0;
  while ((1 << D) - 1 < N)
    D++;
  int TD = D / 2;
  if (TD == 0)
    TD = 1;
  vector<vector<int>> T(1 << TD, vector<int>(100001));
  for (int i = W[1]; i <= 100000; i++)
    T[1][i] = V[1];
  for (int i = 2; i < 1 << TD; i++)
    for (int j = 0; j <= 100000; j++) {
      T[i][j] = T[i / 2][j];
      if (j >= W[i])
        T[i][j] = max(T[i][j], T[i / 2][j - W[i]] + V[i]);
    }

  vector<int> W2;
  vector<int> V2;
  for (int q = 0; q < Q; q++) {
    int t = v[q];
    W2.clear();
    V2.clear();
    while (t >= 1 << TD) {
      W2.push_back(W[t]);
      V2.push_back(V[t]);
      t /= 2;
    }
    int ans = 0;
    for (int b = 0; b < 1 << (int)W2.size(); b++) {
      int w = 0;
      int v = 0;
      for (int i = 0; i < (int)W2.size(); i++)
        if (b >> i & 1) {
          w += W2[i];
          v += V2[i];
        }
      if (w <= L[q])
        ans = max(ans, v + T[t][L[q] - w]);
    }
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <ctime>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> V(N + 1);
  vector<int> W(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> V[i] >> W[i];

  int Q;
  cin >> Q;
  vector<int> v(Q);
  vector<int> L(Q);
  for (int i = 0; i < Q; i++)
    cin >> v[i] >> L[i];

  int D = 0;
  while ((1 << D) - 1 < N)
    D++;
  int TD = D / 2;
  vector<vector<int>> T(1 << TD, vector<int>(100001));
  for (int i = W[1]; i <= 100000; i++)
    T[1][i] = V[1];
  for (int i = 2; i < 1 << TD; i++)
    for (int j = 0; j <= 100000; j++) {
      T[i][j] = T[i / 2][j];
      if (j >= W[i])
        T[i][j] = max(T[i][j], T[i / 2][j - W[i]] + V[i]);
    }

  vector<int> W2;
  vector<int> V2;
  for (int q = 0; q < Q; q++) {
    int t = v[q];
    W2.clear();
    V2.clear();
    while (t >= 1 << TD) {
      W2.push_back(W[t]);
      V2.push_back(V[t]);
      t /= 2;
    }
    int ans = 0;
    for (int b = 0; b < 1 << (int)W2.size(); b++) {
      int w = 0;
      int v = 0;
      for (int i = 0; i < (int)W2.size(); i++)
        if (b >> i & 1) {
          w += W2[i];
          v += V2[i];
        }
      if (w <= L[q])
        ans = max(ans, v + T[t][L[q] - w]);
    }
    cout << ans << endl;
  }
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long int ll;
typedef pair<ll, ll> P;

const ll INF = 1e18;
const int MAX_L = 101010;

int main() {
  int N;
  cin >> N;
  vector<P> a(N + 1); // first:weight, second:value
  rep(i, N) cin >> a[i + 1].second >> a[i + 1].first; // 1-indexed

  int K = 9;
  int n1 = (1 << K) - 1; // 深さKまでの要素数
  vector<vector<ll>> dp(n1 + 1, vector<ll>(MAX_L + 1, 0));
  for (int i = 1; i <= min(n1, N); i++) {
    rep(j, MAX_L) {
      int w = a[i].first, v = a[i].second;
      dp[i][j] = max(dp[i][j], dp[i / 2][j]);
      if (j - w >= 0)
        dp[i][j] = max(dp[i][j], dp[i / 2][j - w] + v);
    }
  }

  int Q;
  cin >> Q;
  while (Q--) {
    int v, l;
    cin >> v >> l;
    // 既に計算してあるならそのまま使う
    if (v <= n1) {
      cout << dp[v][l] << endl;
      continue;
    }
    // 計算していない部分の要素を取得
    vector<P> b(0);
    while (v > n1) {
      b.emplace_back(a[v]);
      v /= 2;
    }
    // for(auto x:b) cout << x.first << " ";cout << endl;
    // 全列挙
    int n2 = b.size();
    ll ans = 0;
    for (int bits = 0; bits < (1 << n2); bits++) {
      int sw = 0, sv = 0;
      rep(i, n2) if (bits & (1 << i)) {
        sw += b[i].first;
        sv += b[i].second;
      }
      if (l - sw >= 0) {
        ll tv = dp[v][l - sw];
        ans = max(ans, sv + tv);
      }
    }

    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long int ll;
typedef pair<ll, ll> P;

const ll INF = 1e18;

int main() {
  int N;
  cin >> N;
  vector<P> a(N); // first:weight, second:value
  rep(i, N) cin >> a[i].second >> a[i].first;

  int Q;
  cin >> Q;
  while (Q--) {
    int v, l;
    cin >> v >> l;
    // アイテムの選択肢を取得
    vector<P> b(0);
    while (v) {
      b.emplace_back(a[v - 1]);
      v /= 2;
    }
    // for(auto x:b) cout << x.first << " "; cout << endl;
    // 前半を全列挙
    int n = b.size();
    int n1 = n / 2, n2 = n - n1;
    vector<P> ps(1 << n1);
    for (int bits = 0; bits < (1 << n1); bits++) {
      int sw = 0, sv = 0;
      rep(i, n) if (bits & (1 << i)) {
        sw += b[i].first;
        sv += b[i].second;
      }
      ps[bits] = P(sw, sv);
    }
    // 損な要素を取り除く
    sort(ps.begin(), ps.end());
    int m = 1; // 最終的なpsの要素数
    for (int i = 1; i < (1 << n1); i++) {
      if (ps[m - 1].second < ps[i].second) {
        ps[m] = ps[i];
        m++;
      }
    }
    ps.erase(ps.begin() + m, ps.end());
    // 後ろ半分を全列挙して解を求める
    ll ans = 0;
    for (int bits = 0; bits < (1 << (n2)); bits++) {
      ll sw = 0, sv = 0;
      rep(i, n2) if (bits & (1 << i)) {
        sw += b[n1 + i].first;
        sv += b[n1 + i].second;
      }
      if (sw <= l) {
        ll tv = (lower_bound(ps.begin(), ps.end(), P(l - sw, INF)) - 1)->second;
        ans = max(ans, sv + tv);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repl(i, l, r) for (int i = (l); i < (r); i++)
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
using ll = long long;
const ll MOD = 1000000007;
const int inf = 1e9 + 10;
const ll INF = 4e18 + 10;
const int M = 300000;
int p, v[M], w[M], dp[512][100001];
void solve() {
  int V, L;
  cin >> V >> L;
  V;
  if (V <= p) {
    cout << dp[V][L] << endl;
    return;
  }
  vector<int> A;
  while (V > p) {
    A.push_back(V);
    V = V / 2;
  }
  int s = A.size(), ans = 0;
  rep(bit, 1 << s) {
    int nw = 0, nv = 0;
    rep(i, s) {
      if (bit >> i & 1) {
        nw += w[A[i] - 1];
        nv += v[A[i] - 1];
      }
    }
    if (L >= nw)
      ans = max(ans, nv + dp[V][L - nw]);
  }
  cout << ans << endl;
}
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) scanf("%d%d", &v[i], &w[i]);
  p = (log2(n) + 1) / 2;
  p = (1 << p) - 1;
  rep(i, p) {
    rep(j, 100001) {
      if (j)
        dp[i + 1][j] = max(dp[i + 1][j - 1], dp[(i + 1) / 2][j]);
      if (j >= w[i])
        dp[i + 1][j] = max(dp[i + 1][j], dp[(i + 1) / 2][j - w[i]] + v[i]);
    }
  }
  int q;
  scanf("%d", &q);
  while (q--)
    solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repl(i, l, r) for (int i = (l); i < (r); i++)
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
using ll = long long;
const ll MOD = 1000000007;
const int inf = 1e9 + 10;
const ll INF = 4e18 + 10;
int v[300000], w[300000];
void solve() {
  int t, l;
  scanf("%d%d", &t, &l);
  int N = 0;
  vector<pair<int, int>> p;
  while (t) {
    p.push_back({v[t], w[t]});
    t /= 2;
    N++;
  }
  int n = (N + 1) / 2, m = N / 2;
  vector<pair<int, int>> A;
  rep(bit, 1 << n) {
    int nowv = 0, noww = 0;
    rep(i, n) {
      if ((bit >> i) & 1) {
        nowv += p[i].first;
        noww += p[i].second;
      }
    }
    if (noww < l)
      A.push_back({nowv, noww});
  }
  vector<pair<int, int>> B;
  rep(bit, 1 << m) {
    int nowv = 0, noww = 0;
    rep(i, m) {
      if (bit >> i & 1) {
        nowv += p[n + i].first;
        noww += p[n + i].second;
      }
    }
    if (noww < l)
      B.push_back({noww, -nowv});
  }
  sort(all(B));
  int S = A.size(), T = B.size();
  vector<int> P(T);
  rep(i, T) {
    P[i] = B[i].first;
    if (i)
      B[i].second = max(-B[i].second, B[i - 1].second);
    else
      B[i].second = -B[i].second;
  }
  int ans = 0;
  rep(i, S) {
    int c = upper_bound(all(P), l - A[i].second) - P.begin() - 1;
    ans = max(ans, A[i].first + B[c].second);
  }
  printf("%d\\n", ans);
  return;
}
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) scanf("%d%d", &v[i + 1], &w[i + 1]);
  int q;
  scanf("%d", &q);
  while (q--)
    solve();
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<int> v(n + 1, 0), w(n + 1, 0);
  for (int i = 1; i <= n; ++i) {
    cin >> v[i] >> w[i];
  }
  vector<vector<int>> dp(1 << 9, vector<int>(100001, 0));
  for (int i = 1; i <= min(n, (1 << 9) - 1); ++i) {
    for (int j = 1; j <= 100000; ++j) {
      int p = i / 2;
      dp[i][j] = max(dp[p][j], dp[i][j - 1]);
      if (j - w[i] >= 0) {
        dp[i][j] = max(dp[p][j - w[i]] + v[i], dp[i][j]);
      }
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int t, L;
    cin >> t >> L;
    vector<int> vv, ww;
    while (t >= (1 << 9)) {
      vv.push_back(v[t]);
      ww.push_back(w[t]);
      t /= 2;
    }
    int n = vv.size();
    int res = 0;
    for (int j = 0; j < (1 << n); ++j) {
      int val = 0, wei = 0;
      for (int k = 0; k < n; ++k) {
        if ((j >> k) & 1) {
          val += vv[k];
          wei += ww[k];
        }
      }
      if (L - wei >= 0) {
        res = max(res, val + dp[t][L - wei]);
      } else {
        continue;
      }
    }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

constexpr int INF = 1e9;

int vv[1 << 18], ww[1 << 18];

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> vv[i] >> ww[i];
  }
  int q;
  cin >> q;
  for (int jj = 0; jj < q; ++jj) {
    int a, W;
    cin >> a >> W;
    vector<int> p, v, w;
    while (a) {
      p.push_back(a);
      a /= 2;
    }
    int n = p.size();
    for (auto &x : p) {
      v.push_back(vv[x - 1]);
      w.push_back(ww[x - 1]);
    }
    /////////////////////////////
    vector<pair<int, int>> ps(1 << 9);
    int n2 = n / 2;
    for (int i = 0; i < 1 << n2; ++i) {
      int sw = 0, sv = 0;
      for (int j = 0; j < n2; ++j) {
        if (i >> j & 1) {
          sw += w[j];
          sv += v[j];
        }
      }
      ps[i] = {sw, sv};
    }
    sort(ps.begin(), ps.begin() + (1 << n2));
    int m = 1;
    for (int i = 1; i < 1 << n2; ++i) {
      if (ps[m - 1].second < ps[i].second) {
        ps[m++] = ps[i];
      }
    }
    int res = 0;
    for (int i = 0; i < 1 << (n - n2); ++i) {
      int sw = 0, sv = 0;
      for (int j = 0; j < n - n2; ++j) {
        if (i >> j & 1) {
          sw += w[n2 + j];
          sv += v[n2 + j];
        }
      }
      if (sw <= W) {
        int tv =
            (lower_bound(ps.begin(), ps.begin() + m, make_pair(W - sw, INF)) -
             1)
                ->second;
        res = max(res, sv + tv);
      }
    }
    cout << res << endl;
  }
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int dp[1025][100001], v[323456], w[323456];
int f(int a, int l) {
  if (l < 0)
    return -1000000000;
  if (!a)
    return 0;
  if (a > 1024)
    return max(f(a / 2, l), f(a / 2, l - w[a]) + v[a]);
  if (~dp[a][l])
    return dp[a][l];
  return dp[a][l] = max({f(a / 2, l), f(a, l - 1), f(a / 2, l - w[a]) + v[a]});
}

int main() {
  memset(dp, -1, sizeof(dp));
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> v[i + 1] >> w[i + 1];
  int q;
  cin >> q;
  while (q--) {
    int a, l;
    cin >> a >> l;
    cout << f(a, l) << endl;
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int dp[1025][100001], v[323456], w[323456];
int f(int a, int l) {
  if (l < 0)
    return -1000000000;
  if (a > 1024)
    return max(f(a / 2, l), f(a / 2, l - w[a]) + v[a]);
  if (~dp[a][l])
    return dp[a][l];
  return dp[a][l] = max({f(a / 2, l), f(a, l - 1), f(a / 2, l - w[a]) + v[a]});
}

int main() {
  memset(dp, -1, sizeof(dp));
  dp[0][0] = 0;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> v[i + 1] >> w[i + 1];
  int q;
  cin >> q;
  while (q--) {
    int a, l;
    cin >> a >> l;
    cout << f(a, l) << endl;
  }
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll maxV = (1ll << 18) + 5;
const ll maxL = ll(1e5) + 5;
const ll half = 1ll << 9;
ll N;
ll V[maxV], W[maxV];
ll mdp[half + 5][maxL];

void dfs(ll t) {
  // cerr <<"t=" <<t << endl;
  if (t >= (half)) {
    return;
  }

  for (int i = 0; i < maxL; i++) {
    if (W[t - 1] <= i) {
      mdp[t][i] = max(mdp[t / 2][i], mdp[t / 2][i - W[t - 1]] + V[t - 1]);
    } else {
      mdp[t][i] = mdp[t / 2][i];
    }
  }

  dfs(t * 2);
  dfs(t * 2 + 1);
}

ll revDfs(ll t, ll w) {
  // cerr <<"t=" <<t << endl;
  if (t < (half)) {
    // cerr <<"mdp=" << mdp[P(t,w)] << endl;
    return mdp[t][w];
  }
  if (W[t - 1] <= w) {
    return max(revDfs(t / 2, w), revDfs(t / 2, w - W[t - 1]) + V[t - 1]);
  } else {
    return revDfs(t / 2, w);
  }
}

int main() {

  cin >> N;
  for (int i = 0; i < N; i++) {
    scanf("%lld", &V[i]);
    scanf("%lld", &W[i]);
  }
  dfs(1);
  ll Q;
  cin >> Q;
  // cerr <<"Q=" << Q << endl;
  for (int q = 0; q < Q; q++) {
    ll v, L;
    scanf("%lld", &v);
    scanf("%lld", &L);
    cout << revDfs(v, L) << "\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll maxV = (1ll << 18) + 5;
const ll maxL = ll(1e5) + 1;
ll N;
ll V[maxV], W[maxV];
map<pair<ll, ll>, ll> mdp;

void dfs(ll t) {
  if (t >= (1ll < 9)) {
    return;
  }
  for (int i = 0; i <= maxL; i++) {
    if (W[t - 1] <= i) {
      mdp[P(t, i)] =
          max(mdp[P(t / 2, i)], mdp[P(t / 2, i - W[t - 1])] + V[t - 1]);
    } else {
      mdp[P(t, i)] = mdp[P(t / 2, i)];
    }
  }

  dfs(t * 2);
  dfs(t * 2 + 1);
}

ll revDfs(ll t, ll w) {
  if (t < (1ll < 9)) {
    return mdp[P(t, w)];
  }
  if (W[t - 1] <= w) {
    return max(revDfs(t / 2, w), revDfs(t / 2, w - W[t - 1]) + V[t - 1]);
  } else {
    return revDfs(t / 2, w);
  }
}

int main() {

  cin >> N;
  for (int i = 0; i < N; i++) {
    scanf("%lld", &V[i]);
    scanf("%lld", &W[i]);
  }
  dfs(1);
  ll Q;
  cin >> Q;
  // cerr <<"Q=" << Q << endl;
  for (int q = 0; q < Q; q++) {
    ll v, L;
    scanf("%lld", &v);
    scanf("%lld", &L);
    cout << revDfs(v, L) << "\\n";
  }

  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pi;

const int N = 300005;

int n, v[N], w[N], L, id[25], mx[N], top1, top2;
pi sta1[N], sta2[N], tmp[N];

void get(int x, int y, int &top1, pi *sta1) {
  top1 = 1;
  sta1[0] = make_pair(0, 0);
  for (int i = x; i <= y; i++) {
    int top = 0, p = 0, q = 0, vv = v[id[i]], ww = w[id[i]];
    while (p < top1 && q < top1)
      if (sta1[p].second < sta1[q].second + ww)
        tmp[top++] = sta1[p++];
      else
        tmp[top++] = make_pair(sta1[q].first + vv, sta1[q].second + ww), q++;
    while (p < top1)
      tmp[top++] = sta1[p++];
    while (q < top1)
      tmp[top++] = make_pair(sta1[q].first + vv, sta1[q].second + ww), q++;
    for (int j = 0; j < top; j++)
      sta1[j] = tmp[j];
    top1 = top;
  }
}

bool cmp(pi a, pi b) { return a.second < b.second; }

void solve() {
  int p = top2 - 1, ans = 0;
  mx[0] = sta2[0].first;
  for (int i = 1; i < top2; i++)
    mx[i] = max(mx[i - 1], sta2[i].first);
  for (int i = 0; i < top1; i++) {
    int vv = sta1[i].first, ww = sta1[i].second;
    while (p >= 0 && sta2[p].second + ww > L)
      p--;
    if (p >= 0)
      ans = max(ans, mx[p] + vv);
  }
  printf("%d\\n", ans);
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &v[i], &w[i]);
  int q;
  scanf("%d", &q);
  while (q--) {
    int x;
    scanf("%d%d", &x, &L);
    int tot = 0;
    while (x)
      id[++tot] = x, x /= 2;
    get(1, tot / 2, top1, sta1);
    get(tot / 2 + 1, tot, top2, sta2);
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pi;

const int N = 300005;

int n, v[N], w[N], L, id[25], mx[N], top1, top2;
pi sta1[N], sta2[N];

void dfs(int x, int y, int s1, int s2, int op) {
  if (s2 > L)
    return;
  if (x > y) {
    if (op == 1)
      sta1[top1++] = make_pair(s1, s2);
    else
      sta2[top2++] = make_pair(s1, s2);
    return;
  }
  dfs(x + 1, y, s1, s2, op);
  dfs(x + 1, y, s1 + v[id[x]], s2 + w[id[x]], op);
}

bool cmp(pi a, pi b) { return a.second < b.second; }

void solve() {
  sort(sta1, sta1 + top1, cmp);
  sort(sta2, sta2 + top2, cmp);
  int p = top2 - 1, ans = 0;
  mx[0] = sta2[0].first;
  for (int i = 1; i < top2; i++)
    mx[i] = max(mx[i - 1], sta2[i].first);
  for (int i = 0; i < top1; i++) {
    int vv = sta1[i].first, ww = sta1[i].second;
    while (p >= 0 && sta2[p].second + ww > L)
      p--;
    if (p >= 0)
      ans = max(ans, mx[p] + vv);
  }
  printf("%d\\n", ans);
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &v[i], &w[i]);
  int q;
  scanf("%d", &q);
  while (q--) {
    int x;
    scanf("%d%d", &x, &L);
    int tot = 0;
    while (x)
      id[++tot] = x, x /= 2;
    top1 = top2 = 0;
    dfs(1, tot / 2, 0, 0, 1);
    dfs(tot / 2 + 1, tot, 0, 0, 2);
    solve();
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
#define N (1 << 19)
#define M 100005
#define K (1 << 9)

LL n, m, q, a[N], b[N], c[N], f[K + 5][M];

int main() {
  scanf("%lld", &n);
  for (LL i = 1; i <= n; ++i) {
    scanf("%lld%lld", a + i, b + i);
  }
  m = 100000;
  for (LL i = 1; i <= K && i <= n; ++i) {
    LL t = 0;
    for (LL j = i; j; j /= 2)
      c[t++] = j;
    for (LL j = 0; j < 1 << t; ++j) {
      LL w = 0, v = 0;
      for (LL k = 0; k < t; ++k) {
        if (j >> k & 1) {
          v += a[c[k]];
          w += b[c[k]];
        }
      }
      if (w <= m)
        f[i][w] = max(f[i][w], v);
    }
    for (LL j = 1; j <= m; ++j)
      f[i][j] = max(f[i][j], f[i][j - 1]);
  }
  scanf("%lld", &q);
  while (q--) {
    LL x, y;
    scanf("%lld%lld", &x, &y);
    LL t = 0, ans = 0;
    for (; x > K; x /= 2)
      c[t++] = x;
    for (LL i = 0; i < 1 << t; ++i) {
      LL w = 0, v = 0;
      for (LL k = 0; k < t; ++k) {
        if (i >> k & 1) {
          v += a[c[k]];
          w += b[c[k]];
        }
      }
      if (w <= y)
        ans = max(ans, f[x][y - w] + v);
    }
    printf("%lld\\n", ans);
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
#define N (1 << 19)
#define M 100005
#define K (1 << 8)

LL n, m, q, a[N], b[N], c[N], f[K + 5][M];

int main() {
  scanf("%lld", &n);
  for (LL i = 1; i <= n; ++i) {
    scanf("%lld%lld", a + i, b + i);
  }
  m = 100000;
  for (LL i = 1; i <= K && i <= n; ++i) {
    LL t = 0;
    for (LL j = i; j; j /= 2)
      c[t++] = j;
    for (LL j = 0; j < 1 << t; ++j) {
      LL w = 0, v = 0;
      for (LL k = 0; k < t; ++k) {
        if (j >> k & 1) {
          v += a[c[k]];
          w += b[c[k]];
        }
      }
      if (w <= m)
        f[i][w] = max(f[i][w], v);
    }
    for (LL j = 1; j <= m; ++j)
      f[i][j] = max(f[i][j], f[i][j - 1]);
  }
  scanf("%lld", &q);
  while (q--) {
    LL x, y;
    scanf("%lld%lld", &x, &y);
    LL t = 0, ans = 0;
    for (; x > K; x /= 2)
      c[t++] = x;
    for (LL i = 0; i < 1 << t; ++i) {
      LL w = 0, v = 0;
      for (LL k = 0; k < t; ++k) {
        if (i >> k & 1) {
          v += a[c[k]];
          w += b[c[k]];
        }
      }
      if (w <= y)
        ans = max(ans, f[x][y - w] + v);
    }
    printf("%lld\\n", ans);
  }

  return 0;
}