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

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include <bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;

constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

class DKnapsackQueriesOnATree {
public:
  void format(vector<P> &vec) {
    sort(all(vec));
    for (int i = 0; i < vec.size() - 1; i++) {
      if (vec[i].second > vec[i + 1].second) {
        vec[i + 1].second = vec[i].second;
      }
    }
  }

  void solve(istream &cin, ostream &cout) {
    SPEED;
    ll n;
    cin >> n;
    vector<int> v(1 << 18), w(1 << 18);
    for (int i = 1; i <= n; i++) {
      cin >> v[i] >> w[i];
    }

    vector<vector<P>> dp(1024, vector<P>());
    for (int i = 1; i <= 1023; i++) {
      for (int k = 0; k < 1024; k++) {
        int tmp = i;
        int nowv = 0;
        int noww = 0;
        for (int p = 0; p < 10; p++) {
          if (k & (1 << p)) {
            nowv += v[tmp];
            noww += w[tmp];
          }
          tmp /= 2;
        }
        dp[i].emplace_back(noww, nowv);
      }
      format(dp[i]);
    }

    int q;
    cin >> q;
    while (q--) {
      int p, l;
      cin >> p >> l;
      if (p < 1024) {
        ll ret = 0;
        auto it = upper_bound(all(dp[p]), P(l, INT_MAX));
        auto pos = distance(dp[p].begin(), it);
        if (pos > 0) {
          chmax(ret, dp[p][pos - 1].second);
        }
        cout << ret << endl;
      } else {
        ll ret = 0;
        int idx = -1;
        for (int k = 0; k < 256; k++) {
          int tmp = p;
          int nowv = 0;
          int noww = 0;
          for (int kk = 0; kk < 8; kk++) {
            if (k & (1 << kk)) {
              nowv += v[tmp];
              noww += w[tmp];
            }
            tmp /= 2;
            if (tmp < 1024) {
              idx = tmp;
              break;
            }
          }

          auto it = upper_bound(all(dp[idx]), P(l - noww, INT_MAX));
          auto pos = distance(dp[idx].begin(), it);
          if (pos > 0) {
            chmax(ret, dp[idx][pos - 1].second + nowv);
          }
        }

        cout << ret << endl;
      }
    }
  }
};

signed main() {
  DKnapsackQueriesOnATree solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}

Error: TLE

Fixed submission:

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include <bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;

constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

class DKnapsackQueriesOnATree {
public:
  void format(vector<P> &vec) {
    sort(all(vec));
    for (int i = 0; i < vec.size() - 1; i++) {
      if (vec[i].second > vec[i + 1].second) {
        vec[i + 1].second = vec[i].second;
      }
    }
  }

  void solve(istream &cin, ostream &cout) {
    SPEED;
    ll n;
    cin >> n;
    vector<int> v(1 << 18), w(1 << 18);
    for (int i = 1; i <= n; i++) {
      cin >> v[i] >> w[i];
    }

    vector<vector<P>> dp(512, vector<P>());
    for (int i = 256; i <= 511; i++) {
      for (int k = 0; k < 512; k++) {
        int tmp = i;
        int nowv = 0;
        int noww = 0;
        for (int p = 0; p < 9; p++) {
          if (k & (1 << p)) {
            nowv += v[tmp];
            noww += w[tmp];
          }
          tmp /= 2;
        }
        dp[i].emplace_back(noww, nowv);
      }
      format(dp[i]);
    }

    int q;
    cin >> q;
    while (q--) {
      int p, l;
      cin >> p >> l;
      if (p < 512) {
        int ret = 0;
        for (int k = 0; k < 512; k++) {
          int tmp = p;
          int nowv = 0;
          int noww = 0;
          for (int l = 0; l < 9; l++) {
            if (k & (1 << l)) {
              nowv += v[tmp];
              noww += w[tmp];
            }
            tmp /= 2;
          }
          if (noww <= l) {
            chmax(ret, nowv);
          }
        }
        cout << ret << endl;
      } else {
        vector<P> dp2;
        ll ret = 0;
        int idx = -1;
        for (int k = 0; k < 512; k++) {
          int tmp = p;
          int nowv = 0;
          int noww = 0;
          for (int l = 0; l < 9; l++) {
            if (k & (1 << l)) {
              nowv += v[tmp];
              noww += w[tmp];
            }
            tmp /= 2;
            if (tmp < 512) {
              idx = tmp;
              break;
            }
          }
          dp2.emplace_back(nowv, noww);
        }

        for (auto e : dp2) {
          ll nw = e.second;
          ll nv = e.first;
          auto it = upper_bound(all(dp[idx]), P(l - nw, INT_MAX));
          auto pos = distance(dp[idx].begin(), it);
          if (pos > 0) {
            chmax(ret, dp[idx][pos - 1].second + nv);
          }
        }
        cout << ret << endl;
      }
    }
  }
};

signed main() {
  DKnapsackQueriesOnATree solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}