Problem p02648 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rep1(i, n) for (int i = 1; i <= int(n); i++)
#define per(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define per1(i, n) for (int i = int(n); i > 0; i--)
#define all(c) c.begin(), c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
template <class T, class U> void chmax(T &x, U y) {
  if (x < y)
    x = y;
}
template <class T, class U> void chmin(T &x, U y) {
  if (y < x)
    x = y;
}
template <class T> void mkuni(V<T> &v) {
  sort(all(v));
  v.erase(unique(all(v)), v.end());
}
template <class S, class T>
ostream &operator<<(ostream &o, const pair<S, T> &p) {
  return o << "(" << p.fs << "," << p.sc << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) {
  o << "{";
  for (const T &v : vc)
    o << v << ",";
  o << "}";
  return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

#ifdef LOCAL
#define show(x)                                                                \
  cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream &os) { os << endl; }
template <class T, class... Args>
void dmpr(ostream &os, const T &t, const Args &...args) {
  os << t << " ~ ";
  dmpr(os, args...);
}
#define shows(...)                                                             \
  cerr << "LINE" << __LINE__ << " : ";                                         \
  dmpr(cerr, ##__VA_ARGS__)
#define dump(x)                                                                \
  cerr << "LINE" << __LINE__ << " : " << #x << " = {";                         \
  for (auto v : x)                                                             \
    cerr << v << ",";                                                          \
  cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false); // DON'T USE scanf/printf/puts !!
  cout << fixed << setprecision(20);

  int N;
  cin >> N;
  V<int> p(N), w(N);
  rep(i, N) cin >> p[i] >> w[i];
  int Q;
  cin >> Q;
  using P = pair<int, int>;
  VV<P> v2Wq(N);
  rep(q, Q) {
    int v, W;
    cin >> v >> W;
    v--;
    v2Wq[v].pb(W, q);
  }
  V<int> dep(N);
  dep[0] = 1;
  V<int> root(N);
  VV<P> wps(N);
  rep1(i, N - 1) dep[i] = dep[(i - 1) / 2] + 1;
  V<int> ans(Q);
  rep(i, N) {
    if (dep[i] <= 12) {
      V<int> vs;
      {
        int v = i;
        rep(k, dep[i]) {
          vs.pb(v);
          v = (v - 1) / 2;
        }
      }
      V<P> ps;
      rep(s, 1 << dep[i]) {
        int pp = 0, ww = 0;
        rep(j, dep[i]) if (s & 1 << j) ww += w[vs[j]], pp += p[vs[j]];
        ps.pb(P(ww, pp));
      }
      sort(all(ps), [&](P l, P r) { return P(l.fs, -l.sc) < P(r.fs, -r.sc); });
      {
        int mx = -1;
        for (P p : ps) {
          if (p.sc > mx) {
            mx = p.sc;
            wps[i].pb(p);
          }
        }
      }
      for (const auto [W, q] : v2Wq[i]) {
        int idx = lower_bound(all(wps[i]), P(W + 1, -1)) - wps[i].begin() - 1;
        ans[q] = wps[i][idx].sc;
      }
    } else {
      root[i] = root[(i - 1) / 2];
      V<int> vs;
      {
        int v = i;
        while (v != root[i]) {
          vs.pb(v);
          v = (v - 1) / 2;
        }
      }
      rep(s, 1 << si(vs)) {
        int pp = 0, ww = 0;
        rep(j, dep[i]) if (s & 1 << j) ww += w[vs[j]], pp += p[vs[j]];
        for (const auto [W, q] : v2Wq[i]) {
          int idx = lower_bound(all(wps[root[i]]), P(W - ww + 1, -1)) -
                    wps[root[i]].begin() - 1;
          if (idx == -1)
            continue;
          chmax(ans[q], pp + wps[root[i]][idx].sc);
        }
      }
    }
  }
  rep(i, Q) cout << ans[i] << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rep1(i, n) for (int i = 1; i <= int(n); i++)
#define per(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define per1(i, n) for (int i = int(n); i > 0; i--)
#define all(c) c.begin(), c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
template <class T, class U> void chmax(T &x, U y) {
  if (x < y)
    x = y;
}
template <class T, class U> void chmin(T &x, U y) {
  if (y < x)
    x = y;
}
template <class T> void mkuni(V<T> &v) {
  sort(all(v));
  v.erase(unique(all(v)), v.end());
}
template <class S, class T>
ostream &operator<<(ostream &o, const pair<S, T> &p) {
  return o << "(" << p.fs << "," << p.sc << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) {
  o << "{";
  for (const T &v : vc)
    o << v << ",";
  o << "}";
  return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

#ifdef LOCAL
#define show(x)                                                                \
  cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream &os) { os << endl; }
template <class T, class... Args>
void dmpr(ostream &os, const T &t, const Args &...args) {
  os << t << " ~ ";
  dmpr(os, args...);
}
#define shows(...)                                                             \
  cerr << "LINE" << __LINE__ << " : ";                                         \
  dmpr(cerr, ##__VA_ARGS__)
#define dump(x)                                                                \
  cerr << "LINE" << __LINE__ << " : " << #x << " = {";                         \
  for (auto v : x)                                                             \
    cerr << v << ",";                                                          \
  cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false); // DON'T USE scanf/printf/puts !!
  cout << fixed << setprecision(20);

  int N;
  cin >> N;
  V<int> p(N), w(N);
  rep(i, N) cin >> p[i] >> w[i];
  int Q;
  cin >> Q;
  using P = pair<int, int>;
  VV<P> v2Wq(N);
  rep(q, Q) {
    int v, W;
    cin >> v >> W;
    v--;
    v2Wq[v].pb(W, q);
  }
  V<int> dep(N);
  dep[0] = 1;
  V<int> root(N);
  VV<P> wps(N);
  rep1(i, N - 1) dep[i] = dep[(i - 1) / 2] + 1;
  V<int> ans(Q);
  rep(i, N) {
    if (dep[i] <= 12) {
      root[i] = i;
      V<int> vs;
      {
        int v = i;
        rep(k, dep[i]) {
          vs.pb(v);
          v = (v - 1) / 2;
        }
      }
      V<P> ps;
      rep(s, 1 << dep[i]) {
        int pp = 0, ww = 0;
        rep(j, dep[i]) if (s & 1 << j) ww += w[vs[j]], pp += p[vs[j]];
        ps.pb(P(ww, pp));
      }
      sort(all(ps), [&](P l, P r) { return P(l.fs, -l.sc) < P(r.fs, -r.sc); });
      {
        int mx = -1;
        for (P p : ps) {
          if (p.sc > mx) {
            mx = p.sc;
            wps[i].pb(p);
          }
        }
      }
      for (const auto [W, q] : v2Wq[i]) {
        int idx = lower_bound(all(wps[i]), P(W + 1, -1)) - wps[i].begin() - 1;
        ans[q] = wps[i][idx].sc;
      }
    } else {
      root[i] = root[(i - 1) / 2];
      V<int> vs;
      {
        int v = i;
        while (v != root[i]) {
          vs.pb(v);
          v = (v - 1) / 2;
        }
      }
      rep(s, 1 << si(vs)) {
        int pp = 0, ww = 0;
        rep(j, dep[i]) if (s & 1 << j) ww += w[vs[j]], pp += p[vs[j]];
        for (const auto [W, q] : v2Wq[i]) {
          int idx = lower_bound(all(wps[root[i]]), P(W - ww + 1, -1)) -
                    wps[root[i]].begin() - 1;
          if (idx == -1)
            continue;
          chmax(ans[q], pp + wps[root[i]][idx].sc);
        }
      }
    }
  }
  rep(i, Q) cout << ans[i] << '\n';
}