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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fore(b, c) for (int val0 = b; val0 < c; val0++)
#define forr(k, c, s) for (int k = c; k < s; k++)
#define pb push_back
#define mmp make_pair
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int, int> ii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long double ld;
typedef vector<vii> al;
typedef vector<ll> vl;
const int INF = 1e9;
const ll INFL = 1LL << 61;
const ll MN = 100010;
const ll MM = (1 << 18) + 10;
const ll SQ = (1 << 9) | 1;
ll wa[MM], va[MM];
// maybe less dumb lol
vii ok[SQ];
void pre(int u) {
  if (u == 0) {
    ok[u].push_back({0, 0});
    return;
  }
  vii &su = ok[u >> 1];
  ok[u] = su;
  for (int i = 0; i < su.size(); i++) {
    ii co = su[i];
    ok[u].push_back({co.first + wa[u], co.second + va[u]});
  }
  sort(ok[u].begin(), ok[u].end());
  int rs = -1;
  for (int i = 0; i < ok[u].size(); i++) {
    rs = max(rs, ok[u][i].second);
    ok[u][i].second = max(ok[u][i].second, rs);
  }
}
ll sqan(int u, int wo) {
  ii se = {wo + 1, -1};
  auto it = upper_bound(ok[u].begin(), ok[u].end(), se);
  --it;
  return it->second;
}
ll ds(int u, int wo) {
  if (u == 0) {
    return 0;
  }
  if (u < SQ) {
    return sqan(u, wo);
  }
  ll res = ds(u >> 1, wo);
  if (wo >= wa[u]) {
    res = max(res, ds(u >> 1, wo - wa[u]) + va[u]);
  }
  return res;
}
int main() {
  ios::sync_with_stdio(0);
  cout.precision(20);
  cout.tie(0);
  cin.tie(0);
  ll n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> va[i] >> wa[i];
  }
  for (int i = 0; i < SQ; i++) {
    pre(i);
  }
  int q;
  cin >> q;
  while (q--) {
    int u, w;
    cin >> u >> w;
    cout << ds(u, w) << '\\n';
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fore(b, c) for (int val0 = b; val0 < c; val0++)
#define forr(k, c, s) for (int k = c; k < s; k++)
#define pb push_back
#define mmp make_pair
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int, int> ii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long double ld;
typedef vector<vii> al;
typedef vector<ll> vl;
const int INF = 1e9;
const ll INFL = 1LL << 61;
const ll MN = 100010;
const ll MM = (1 << 18) + 10;
// obviously make it even worse
struct chash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
typedef long long ll;
gp_hash_table<ll, ll, chash> dp;
ll wa[MM], va[MM];
ll ds(int u, int wo) {
  if (u == 0) {
    return 0;
  }
  ll hv = ((ll(u)) << 20) | wo;
  if (dp.find(hv) != dp.end()) {
    return dp[hv];
  }
  ll res = ds(u >> 1, wo);
  if (wo >= wa[u]) {
    res = max(res, ds(u >> 1, wo - wa[u]) + va[u]);
  }
  return dp[hv] = res;
}
int main() {
  ios::sync_with_stdio(0);
  cout.precision(20);
  cout.tie(0);
  cin.tie(0);
  ll n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> va[i] >> wa[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int u, w;
    cin >> u >> w;
    cout << ds(u, w) << '\\n';
  }
}