Problem p02648 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
#define mod 1000000007
#define dom 998244353
#define all(c) begin(c), end(c)
#define rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
#define long long long
#define List vector
#define var auto
#define Count size()
#define Length size()
#define in :
#define foreach for
using pi = pair<int, int>;
int dd[] = {0, 1, 0, -1, 0}; // →↓←↑

const int P = 1 << 8;
const int Q = 1 << 9;
int N;
vector<int> V;
vector<int> W;
vector<vector<pi>> D;

void mode4(int v, int L, vector<pi> Z) {
  int ans = 0;
  int K = Z.size();
  for (int i = 0; i < 1 << K; i++) {
    int sw = 0, sv = 0;
    for (int j = 0; j < K; j++) {
      if (i >> j & 1) {
        sw += Z[j].first;
        sv += Z[j].second;
      }
    }
    if (sw <= L) {
      var q = pi(L - sw, INT_MAX / 3);
      // cout << D[0].size() << " " << v - P << endl;

      // var B = lower_bound(all(D[v - P]), make_pair(L - sw, INT_MAX));
      // int pos = distance(D[v - P].begin(), B);
      // cout << pos << endl;
      int tv =
          (lower_bound(all(D[v - P]), make_pair(L - sw, INT_MAX)) - 1)->second;
      chmax(ans, sv + tv);
    }
  }

  cout << ans << "\n";
}
void mode1(int v, int L) {
  vector<pi> Z;
  int j = v;
  while (j > 0) {
    Z.push_back(pi(W[j], V[j]));
    j >>= 1;
  }
  int K = Z.size();
  int ans = 0;
  for (int i = 0; i < 1 << K; i++) {
    int sw = 0, sv = 0;
    for (int j = 0; j < K; j++) {
      if (i >> j & 1) {
        sw += Z[j].first;
        sv += Z[j].second;
      }
    }
    if (sw <= L) {
      chmax(ans, sv);
    }
  }
  cout << ans << "\n";
}
void mode2(int v, int L) {
  vector<pi> Z;
  mode4(v, L, Z);
}
void mode3(int v, int L) {
  vector<pi> Z;
  int j = v;
  while (j >= Q) {
    Z.push_back(pi(W[j], V[j]));
    j >>= 1;
  }
  mode4(j, L, Z);
}

void solve() {
  int N;
  cin >> N;
  V = vector<int>(N + 1);
  W = vector<int>(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> V[i] >> W[i];
    // V[i] = i;W[i] = i;
  }

  D = vector<vector<pi>>(P);
  for (int ww = P; ww <= min(N, Q - 1); ww++) {
    vector<pi> Z;
    int j = ww;
    while (j > 0) {
      Z.push_back(pi(W[j], V[j]));
      j >>= 1;
    }

    vector<pi> ps;
    int K = Z.size();
    for (int i = 0; i < 1 << K; i++) {
      int sw = 0, sv = 0;
      for (int j = 0; j < K; j++) {
        if (i >> j & 1) {
          sw += Z[j].first;
          sv += Z[j].second;
        }
      }
      ps.push_back(pi(sw, sv));
    }
    sort(all(ps));

    int m = 1;
    for (int i = 1; i < ps.size(); i++) {
      if (ps[m - 1].second < ps[i].second) {
        ps[m++] = ps[i];
      }
    }
    ps.resize(m);
    D[ww - P] = ps;
  }

  int T;
  cin >> T;
  for (int t = 0; t < T; t++) {
    int v, L;
    cin >> v >> L;
    // v = t; L = t;
    // cout << "id" << t << endl;
    if (v < P) {
      mode1(v, L);
    } else if (v < Q) {
      mode2(v, L);
    } else {
      mode3(v, L);
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(11);
  solve();
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define mod 1000000007
#define dom 998244353
#define all(c) begin(c), end(c)
#define rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
#define long long long
#define List vector
#define var auto
#define Count size()
#define Length size()
#define in :
#define foreach for
using pi = pair<int, int>;
int dd[] = {0, 1, 0, -1, 0}; // →↓←↑

const int P = 1 << 9;
const int Q = 1 << 10;
int N;
vector<int> V;
vector<int> W;
vector<vector<pi>> D;

void mode4(int v, int L, vector<pi> Z) {
  int ans = 0;
  int K = Z.size();
  for (int i = 0; i < 1 << K; i++) {
    int sw = 0, sv = 0;
    for (int j = 0; j < K; j++) {
      if (i >> j & 1) {
        sw += Z[j].first;
        sv += Z[j].second;
      }
    }
    if (sw <= L) {
      var q = pi(L - sw, INT_MAX / 3);
      // cout << D[0].size() << " " << v - P << endl;

      // var B = lower_bound(all(D[v - P]), make_pair(L - sw, INT_MAX));
      // int pos = distance(D[v - P].begin(), B);
      // cout << pos << endl;
      int tv =
          (lower_bound(all(D[v - P]), make_pair(L - sw, INT_MAX)) - 1)->second;
      chmax(ans, sv + tv);
    }
  }

  cout << ans << "\n";
}
void mode1(int v, int L) {
  vector<pi> Z;
  int j = v;
  while (j > 0) {
    Z.push_back(pi(W[j], V[j]));
    j >>= 1;
  }
  int K = Z.size();
  int ans = 0;
  for (int i = 0; i < 1 << K; i++) {
    int sw = 0, sv = 0;
    for (int j = 0; j < K; j++) {
      if (i >> j & 1) {
        sw += Z[j].first;
        sv += Z[j].second;
      }
    }
    if (sw <= L) {
      chmax(ans, sv);
    }
  }
  cout << ans << "\n";
}
void mode2(int v, int L) {
  vector<pi> Z;
  mode4(v, L, Z);
}
void mode3(int v, int L) {
  vector<pi> Z;
  int j = v;
  while (j >= Q) {
    Z.push_back(pi(W[j], V[j]));
    j >>= 1;
  }
  mode4(j, L, Z);
}

void solve() {
  int N;
  cin >> N;
  V = vector<int>(N + 1);
  W = vector<int>(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> V[i] >> W[i];
    // V[i] = i;W[i] = i;
  }

  D = vector<vector<pi>>(P);
  for (int ww = P; ww <= min(N, Q - 1); ww++) {
    vector<pi> Z;
    int j = ww;
    while (j > 0) {
      Z.push_back(pi(W[j], V[j]));
      j >>= 1;
    }

    vector<pi> ps;
    int K = Z.size();
    for (int i = 0; i < 1 << K; i++) {
      int sw = 0, sv = 0;
      for (int j = 0; j < K; j++) {
        if (i >> j & 1) {
          sw += Z[j].first;
          sv += Z[j].second;
        }
      }
      ps.push_back(pi(sw, sv));
    }
    sort(all(ps));

    int m = 1;
    for (int i = 1; i < ps.size(); i++) {
      if (ps[m - 1].second < ps[i].second) {
        ps[m++] = ps[i];
      }
    }
    ps.resize(m);
    D[ww - P] = ps;
  }

  int T;
  cin >> T;
  for (int t = 0; t < T; t++) {
    int v, L;
    cin >> v >> L;
    // v = t; L = t;
    // cout << "id" << t << endl;
    if (v < P) {
      mode1(v, L);
    } else if (v < Q) {
      mode2(v, L);
    } else {
      mode3(v, L);
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(11);
  solve();
  return 0;
}