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

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

const int INF = 1e9 + 10;
const ll MOD = 1e9 + 7;
const int M = 1e5 + 10;
const int N = (1 << 18) + 1;
const int T = 10;

int v1[N], w1[N];
vector<pair<int, int>> g[N];
pair<int, int> b[N];
pair<int, int> bb[N];

int calc(vector<int> ids) {
  vector<int> v, w;
  for (auto i : ids) {
    v.push_back(v1[i]);
    w.push_back(w1[i]);
  }
  int n = (int)ids.size();
  int cur_ptr = 0;
  for (int mask = 0; mask < (1 << n); ++mask) {
    int sumv = 0, sumw = 0;
    for (int i = 0; i < n; ++i) {
      if (mask & (1 << i)) {
        sumv += v[i];
        sumw += w[i];
      }
    }
    bb[cur_ptr++] = {sumv, sumw};
  }
  sort(bb, bb + cur_ptr);
  int ptr = 0;
  for (int i = 0; i < cur_ptr; ++i) {
    auto el = bb[i];
    if (el.first > M)
      break;
    if (!ptr || b[ptr - 1].second < el.second) {
      b[ptr++] = el;
    }
  }
  return ptr;
}

void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> w1[i] >> v1[i];
  }
  for (int i = 1; i < min(n + 1, (1 << T)); ++i) {
    vector<int> path;
    int cur = i;
    while (cur) {
      path.push_back(cur);
      cur /= 2;
    }
    int len = calc(path);
    g[i].resize(len);
    copy(b, b + len, g[i].begin());
  }

  int q;
  cin >> q;
  while (q--) {
    int v, l;
    cin >> v >> l;
    if (g[v].empty()) {
      vector<int> path;
      int cur = v;
      while (g[cur].empty()) {
        path.push_back(cur);
        cur /= 2;
      }
      int len = calc(path);
      auto &a = g[cur];

      int ptr_b = len - 1;
      int ans = 0;
      for (int i = 0; i < (int)a.size(); ++i) {
        while (ptr_b >= 0 && b[ptr_b].first + a[i].first > l) {
          ptr_b--;
        }
        if (ptr_b >= 0) {
          ans = max(ans, b[ptr_b].second + a[i].second);
        }
      }
      cout << ans << "\\n";
    } else {
      auto it = prev(upper_bound(g[v].begin(), g[v].end(), make_pair(l, INF)));
      cout << (it->second) << "\\n";
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

const int INF = 1e9 + 10;
const ll MOD = 1e9 + 7;
const int M = 1e5 + 10;
const int N = (1 << 18) + 1;
const int T = 9;

int v1[N], w1[N];
vector<pair<int, int>> g[N];

vector<pair<int, int>> calc(vector<int> ids) {
  vector<int> v, w;
  for (auto i : ids) {
    v.push_back(v1[i]);
    w.push_back(w1[i]);
  }
  int n = (int)ids.size();
  vector<pair<int, int>> ret;
  for (int mask = 0; mask < (1 << n); ++mask) {
    int sumv = 0, sumw = 0;
    for (int i = 0; i < n; ++i) {
      if (mask & (1 << i)) {
        sumv += v[i];
        sumw += w[i];
      }
    }
    ret.push_back({sumv, sumw});
  }
  sort(ret.begin(), ret.end());
  vector<pair<int, int>> ans;
  for (auto el : ret) {
    if (el.first > M)
      break;
    if (ans.empty() || ans.back().second < el.second) {
      ans.push_back(el);
    }
  }
  return ans;
}

void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> w1[i] >> v1[i];
  }
  for (int i = 1; i <= min(n, (1 << T)); ++i) {
    vector<int> path;
    int cur = i;
    while (cur) {
      path.push_back(cur);
      cur /= 2;
    }
    g[i] = calc(path);
  }

  int q;
  cin >> q;
  while (q--) {
    int v, l;
    cin >> v >> l;
    if (g[v].empty()) {
      vector<int> path;
      int cur = v;
      while (g[cur].empty()) {
        path.push_back(cur);
        cur /= 2;
      }
      auto b = calc(path);
      auto &a = g[cur];

      int ptr_b = (int)b.size() - 1;
      int ans = 0;
      for (int i = 0; i < (int)a.size(); ++i) {
        while (ptr_b >= 0 && b[ptr_b].first + a[i].first > l) {
          ptr_b--;
        }
        if (ptr_b >= 0) {
          ans = max(ans, b[ptr_b].second + a[i].second);
        }
      }
      cout << ans << "\\n";
    } else {
      auto it = prev(upper_bound(g[v].begin(), g[v].end(), make_pair(l, INF)));
      cout << (it->second) << "\\n";
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  solve();
}