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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ull = unsigned long long;
using ld = long double;
template <typename T> void _(const char *s, T h) {
  cerr << s << " = " << h << "\\n";
}
template <typename T, typename... Ts> void _(const char *s, T h, Ts... t) {
  int b = 0;
  while (((b += *s == '(') -= *s == ')') != 0 || *s != ',')
    cerr << *s++;
  cerr << " = " << h << ",";
  _(s + 1, t...);
} // break continue pop_back 998244353
// #define int ll
// #define pii pll
#define f first
#define s second
#define pb emplace_back
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define sz(a) ((int)(a).size())
#define sqr(x) ((x) * (x))
struct init {
  init() {
    cin.tie(0);
    iostream::sync_with_stdio(0);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(5);
  }
  ~init() {
#ifdef LOCAL
#define dbg(...) _(#__VA_ARGS__, __VA_ARGS__)
    cerr << "Time elapsed: " << (double)clock() / CLOCKS_PER_SEC << "s.\\n";
#else
#define dbg(...)
#endif
  }
} init;
template <typename T, typename U> void upx(T &x, U y) {
  if (x < y)
    x = y;
}
template <typename T, typename U> void upn(T &x, U y) {
  if (x > y)
    x = y;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int D = 4, dx[] = {+1, 0, -1, 0}, dy[] = {0, +1, 0, -1};

const int N = 1 << 18;
int c[N], w[N];
vector<pii> f[N];
int h[N];
int gb[512];
vector<pii> to1(int v) {
  dbg(v);
  vector<int> p;
  int cur = v;
  do {
    p.pb(cur);
    cur /= 2;
  } while (cur != 0);
  int z = sz(p);
  vector<pii> res;
  int smw[512], smc[512];
  smw[0] = smc[0] = 0;
  res.pb(0, 0);
  for (int mask = 1; mask < (1 << z); ++mask) {
    int i = gb[mask];
    int m2 = mask ^ (1 << i);
    smc[mask] = c[p[i]] + smc[m2];
    smw[mask] = w[p[i]] + smw[m2];
    res.pb(smw[mask], smc[mask]);
  }
  sort(res.begin(), res.end());
  for (int i = 1; i < sz(res); ++i)
    upx(res[i].s, res[i - 1].s);
  return res;
}
int mid;
int kto[N];
vector<pii> tof(int v) {
  vector<int> p;
  int cur = v;
  while (h[cur] > mid) {
    p.pb(cur);
    cur /= 2;
  }
  kto[v] = cur;
  int z = sz(p);
  vector<pii> res;
  int smw[512], smc[512];
  smw[0] = smc[0] = 0;
  res.pb(0, 0);
  for (int mask = 1; mask < (1 << z); ++mask) {
    int i = gb[mask];
    int m2 = mask ^ (1 << i);
    smc[mask] = c[p[i]] + smc[m2];
    smw[mask] = w[p[i]] + smw[m2];
    res.pb(smw[mask], smc[mask]);
  }
  sort(res.begin(), res.end());
  // for(int i=1;i<sz(res);++i)
  //     upx(res[i].s,res[i-1].s);
  return res;
}
int32_t main() {
  for (int i = 1; i < 512; ++i) {
    gb[i] = 0;
    forn(j, 9) if (i >> j & 1) gb[i] = j;
  }
  dbg("kok");
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> c[i] >> w[i];
  h[1] = 0;
  for (int i = 2; i <= n; ++i)
    h[i] = h[i / 2] + 1;
  mid = h[n] / 2;
  dbg(mid, 1 << mid);
  dbg("kok");
  for (int i = 1; i < (1 << mid + 1); ++i) {
    f[i] = to1(i);
  }
  dbg("ok");
  // for(int i=(1<<mid+1);i<=n;++i){
  //     f[i]=tof(i);
  // }
  int q;
  cin >> q;
  while (q--) {
    if (q % 1000 == 0)
      dbg(q);
    int v, maxw;
    cin >> v >> maxw;
    if (h[v] <= mid) {
      int pos = lower_bound(f[v].begin(), f[v].end(), pii{maxw, -1}) -
                f[v].begin() - 1;
      cout << f[v][pos].s << '\\n';
    } else {
      if (f[v].empty())
        f[v] = tof(v);
      int u = kto[v];
      int pos = sz(f[u]) - 1;
      int ans = 0;
      for (pii p : f[v]) {
        while (pos >= 0 && f[u][pos].f + p.f > maxw)
          --pos;
        if (pos < 0)
          break;
        upx(ans, p.s + f[u][pos].s);
      }
      cout << ans << '\\n';
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ull = unsigned long long;
using ld = long double;
template <typename T> void _(const char *s, T h) {
  cerr << s << " = " << h << "\\n";
}
template <typename T, typename... Ts> void _(const char *s, T h, Ts... t) {
  int b = 0;
  while (((b += *s == '(') -= *s == ')') != 0 || *s != ',')
    cerr << *s++;
  cerr << " = " << h << ",";
  _(s + 1, t...);
} // break continue pop_back 998244353
#define int ll
#define pii pll
#define f first
#define s second
#define pb emplace_back
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define sz(a) ((int)(a).size())
#define sqr(x) ((x) * (x))
struct init {
  init() {
    cin.tie(0);
    iostream::sync_with_stdio(0);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(5);
  }
  ~init() {
#ifdef LOCAL
#define dbg(...) _(#__VA_ARGS__, __VA_ARGS__)
    cerr << "Time elapsed: " << (double)clock() / CLOCKS_PER_SEC << "s.\\n";
#else
#define dbg(...)
#endif
  }
} init;
template <typename T, typename U> void upx(T &x, U y) {
  if (x < y)
    x = y;
}
template <typename T, typename U> void upn(T &x, U y) {
  if (x > y)
    x = y;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int D = 4, dx[] = {+1, 0, -1, 0}, dy[] = {0, +1, 0, -1};

const int N = 1 << 18;
int c[N], w[N];
vector<pii> f[N];
int h[N];
vector<pii> to1(int v) {
  vector<int> p;
  int cur = v;
  do {
    p.pb(cur);
    cur /= 2;
  } while (cur != 0);
  int z = sz(p);
  vector<pii> res;
  forn(mask, 1 << z) {
    int sc = 0, sw = 0;
    forn(i, z) {
      if (mask >> i & 1) {
        sc += c[p[i]];
        sw += w[p[i]];
      }
    }
    res.pb(sw, sc);
  }
  sort(res.begin(), res.end());
  for (int i = 1; i < sz(res); ++i)
    upx(res[i].s, res[i - 1].s);
  return res;
}
int mid;
int kto[N];
vector<pii> tof(int v) {
  vector<int> p;
  int cur = v;
  while (h[cur] > mid) {
    p.pb(cur);
    cur /= 2;
  }
  kto[v] = cur;
  int z = sz(p);
  vector<pii> res;
  forn(mask, 1 << z) {
    int sc = 0, sw = 0;
    forn(i, z) {
      if (mask >> i & 1) {
        sc += c[p[i]];
        sw += w[p[i]];
      }
    }
    res.pb(sw, sc);
  }
  sort(res.begin(), res.end());
  for (int i = 1; i < sz(res); ++i)
    upx(res[i].s, res[i - 1].s);
  return res;
}
int32_t main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> c[i] >> w[i];
  h[1] = 0;
  for (int i = 2; i <= n; ++i)
    h[i] = h[i / 2] + 1;
  mid = h[n] / 2;
  for (int i = 1; i < (1 << mid + 1); ++i) {
    f[i] = to1(i);
  }
  for (int i = (1 << mid + 1); i <= n; ++i) {
    f[i] = tof(i);
  }
  int q;
  cin >> q;
  while (q--) {
    int v, maxw;
    cin >> v >> maxw;
    if (h[v] <= mid) {
      int pos = lower_bound(f[v].begin(), f[v].end(), pii{maxw, -1}) -
                f[v].begin() - 1;
      cout << f[v][pos].s << '\\n';
    } else {
      int u = kto[v];
      int pos = sz(f[u]) - 1;
      int ans = 0;
      for (pii p : f[v]) {
        while (pos >= 0 && f[u][pos].f + p.f > maxw)
          --pos;
        if (pos < 0)
          break;
        upx(ans, p.s + f[u][pos].s);
      }
      cout << ans << '\\n';
    }
  }

  return 0;
}