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

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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for (int64 i = 0; i < (n); i++)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value != 0>::type fill_v(U &u, const V... v) {
  u = U(v...);
}

template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value == 0>::type fill_v(U &u, const V... v) {
  for (auto &e : u)
    fill_v<T>(e, v...);
}

vector<int64> V, W;
vector<vector<PLL>> q;
vector<int64> res;
int64 N;

void dfs(int64 v, vector<vector<PLL>> &st, int64 d) {
  //  cout << v << endl;
  vector<vector<PLL>> tmp(st);
  int fl = d >= 11;
  vector<PLL> nxt(st[fl]);
  for (auto &x : st[fl]) {
    nxt.emplace_back(x.fs + W[v], x.sc + V[v]);
  }
  sort(all(nxt));
  st[fl] = vector<PLL>();
  st[fl].reserve(nxt.size());
  for (auto &&x : nxt) {
    //    if (st[fl].size())
    //      cout << st[fl].back().fs << " " << st[fl].back().sc << " : " << x.fs
    //      << " " << x.sc << endl;
    if (st[fl].empty()) {
      st[fl].push_back(x);
    } else if (st[fl].back().fs == x.fs) {
      st[fl].pop_back();
      st[fl].push_back(x);
    } else if (st[fl].back().sc < x.sc) {
      st[fl].push_back(x);
    }
  }

  //  REP(i, 2) {
  //    cout << v << " " << i << ": ";
  //    for (auto x : st[i]) {
  //      cout << "(" << x.fs << " " << x.sc << ") ";
  //    }
  //    cout << endl;
  //  }

  sort(all(q[v]), [](PLL a, PLL b) { return a.sc < b.sc; });

  int64 l = 0, pre = 0;
  for (auto idxL : q[v]) {
    int64 idx, L;
    tie(idx, L) = idxL;
    res[idx] = pre;
    FOR(i, l, st[fl].size()) {
      PLL x = st[fl][i];
      if (x.fs > L)
        break;
      int64 now = x.sc;
      if (fl) {
        int ind =
            upper_bound(all(st[0]), PLL(L - x.fs, INF_LL)) - st[0].begin();
        now += st[0][ind - 1].sc;
      } else {
        l = i;
      }
      chmax(res[idx], now);
      chmax(pre, now);
    }
  }

  REP(i, 2) {
    if (v * 2 + i + 1 < N)
      dfs(v * 2 + i + 1, st, d + 1);
  }
  swap(st, tmp);
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  V = vector<int64>(N);
  W = vector<int64>(N);
  REP(i, N) cin >> V[i] >> W[i];
  int64 Q;
  cin >> Q;
  vector<int64> v(Q), L(Q);
  res.resize(Q, 0);
  q = vector<vector<PLL>>(N);
  REP(i, Q) {
    cin >> v[i] >> L[i];
    q[v[i] - 1].emplace_back(i, L[i]);
  }
  vector<vector<PLL>> st(2, vector<PLL>({PLL(0, 0)}));
  dfs(0, st, 0);
  REP(i, res.size()) { cout << res[i] << endl; }
}


Error: TLE

Fixed submission:

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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for (int64 i = 0; i < (n); i++)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value != 0>::type fill_v(U &u, const V... v) {
  u = U(v...);
}

template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value == 0>::type fill_v(U &u, const V... v) {
  for (auto &e : u)
    fill_v<T>(e, v...);
}

vector<int64> V, W;
vector<vector<PLL>> q;
vector<int64> res;
int64 N;

void dfs(int64 v, vector<vector<PLL>> &st, int64 d) {
  //  cout << v << endl;
  vector<vector<PLL>> tmp(st);
  int fl = d >= 9;
  vector<PLL> nxt(st[fl]);
  for (auto &x : st[fl]) {
    nxt.emplace_back(x.fs + W[v], x.sc + V[v]);
  }
  sort(all(nxt));
  st[fl] = vector<PLL>();
  st[fl].reserve(nxt.size());
  for (auto &&x : nxt) {
    //    if (st[fl].size())
    //      cout << st[fl].back().fs << " " << st[fl].back().sc << " : " << x.fs
    //      << " " << x.sc << endl;
    if (st[fl].empty()) {
      st[fl].push_back(x);
    } else if (st[fl].back().fs == x.fs) {
      st[fl].pop_back();
      st[fl].push_back(x);
    } else if (st[fl].back().sc < x.sc) {
      st[fl].push_back(x);
    }
  }

  //  REP(i, 2) {
  //    cout << v << " " << i << ": ";
  //    for (auto x : st[i]) {
  //      cout << "(" << x.fs << " " << x.sc << ") ";
  //    }
  //    cout << endl;
  //  }

  for (auto idxL : q[v]) {
    int64 idx, L;
    tie(idx, L) = idxL;
    res[idx] = 0;
    for (auto x : st[fl]) {
      if (x.fs > L)
        break;
      int64 now = x.sc;
      if (fl) {
        int ind =
            upper_bound(all(st[0]), PLL(L - x.fs, INF_LL)) - st[0].begin();
        now += st[0][ind - 1].sc;
      }
      chmax(res[idx], now);
    }
  }

  REP(i, 2) {
    if (v * 2 + i + 1 < N)
      dfs(v * 2 + i + 1, st, d + 1);
  }
  swap(st, tmp);
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  V = vector<int64>(N);
  W = vector<int64>(N);
  REP(i, N) cin >> V[i] >> W[i];
  int64 Q;
  cin >> Q;
  vector<int64> v(Q), L(Q);
  res.resize(Q, 0);
  q = vector<vector<PLL>>(N);
  REP(i, Q) {
    cin >> v[i] >> L[i];
    q[v[i] - 1].emplace_back(i, L[i]);
  }
  vector<vector<PLL>> st(2, vector<PLL>({PLL(0, 0)}));
  dfs(0, st, 0);
  REP(i, res.size()) { cout << res[i] << endl; }
}