Problem p02648 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
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;
}

const long long INF = 1e18;
// const ll mod = 1000000007;
ll N;
vector<ll> V, W;
ll K = 9;
vector<l_l> mid[1 << 18];

vector<l_l> f(vector<l_l> a) {
  vector<l_l> tmp;
  int n = a.size();
  for (int i = 0; i < (1 << n); i++) {
    l_l now;
    for (int j = 0; j < n; j++) {
      if (!(i & (1 << j)))
        continue;
      now.first += a[j].second;
      now.second += a[j].first;
    }
    tmp.push_back(now);
  }
  sort(tmp.begin(), tmp.end(), [](l_l a, l_l b) {
    if (a.first != b.first)
      return a.first < b.first;
    return a.second > b.second;
  });
  vector<l_l> ret;
  for (auto a : tmp) {
    if (ret.size() and ret.back().second >= a.second)
      continue;
    ret.push_back(a);
  }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  V.resize(N + 1);
  W.resize(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> V[i] >> W[i];
  }
  for (int i = 1; i <= N; i++) {
    if (i >= (1 << (K - 1)) and i < (1 << (K))) {
      // cerr << "---" << i << "---" << endl;
      vector<l_l> v;
      ll copy = i;
      while (copy != 0) {
        v.push_back({V[copy], W[copy]});
        copy /= 2;
      }
      mid[i] = f(v);
      for (auto tmp : mid[i]) {
        // cerr << tmp.first << " " << tmp.second << endl;
      }
    }
  }
  int Q;
  cin >> Q;
  while (Q--) {
    ll x, L;
    cin >> x >> L;
    if (x < (1 << K)) {
      ll copy = x;
      vector<l_l> v;
      while (copy != 0) {
        v.push_back({V[copy], W[copy]});
        copy /= 2;
      }
      vector<l_l> w = f(v);
      ll ans = 0;
      for (auto tmp : w) {
        if (tmp.first <= L)
          chmax(ans, tmp.second);
      }
      cout << ans << endl;
    } else {
      ll copy = x;
      vector<l_l> v;
      while (copy >= (1 << K)) {
        v.push_back({V[copy], W[copy]});
        copy /= 2;
      }
      auto w = f(v);
      auto q = mid[copy];
      ll ans = 0;
      for (auto tmp : w) {
        l_l s = {L - tmp.first + 1, -INF};
        auto itr = lower_bound(q.begin(), q.end(), s);
        if (itr == q.begin())
          continue;
        itr--;
        chmax(ans, tmp.second + (*itr).second);
      }
      cout << ans << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
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;
}

const long long INF = 1e18;
// const ll mod = 1000000007;
ll N;
vector<ll> V, W;
ll K = 10;
vector<l_l> mid[1 << 18];

vector<l_l> f(vector<l_l> a) {
  vector<l_l> tmp;
  int n = a.size();
  for (int i = 0; i < (1 << n); i++) {
    l_l now;
    for (int j = 0; j < n; j++) {
      if (!(i & (1 << j)))
        continue;
      now.first += a[j].second;
      now.second += a[j].first;
    }
    tmp.push_back(now);
  }
  sort(tmp.begin(), tmp.end(), [](l_l a, l_l b) {
    if (a.first != b.first)
      return a.first < b.first;
    return a.second > b.second;
  });
  vector<l_l> ret;
  for (auto a : tmp) {
    if (ret.size() and ret.back().second >= a.second)
      continue;
    ret.push_back(a);
  }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  V.resize(N + 1);
  W.resize(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> V[i] >> W[i];
  }
  for (int i = 1; i <= N; i++) {
    if (i >= (1 << (K - 1)) and i < (1 << (K))) {
      // cerr << "---" << i << "---" << endl;
      vector<l_l> v;
      ll copy = i;
      while (copy != 0) {
        v.push_back({V[copy], W[copy]});
        copy /= 2;
      }
      mid[i] = f(v);
      for (auto tmp : mid[i]) {
        // cerr << tmp.first << " " << tmp.second << endl;
      }
    }
  }
  int Q;
  cin >> Q;
  while (Q--) {
    ll x, L;
    cin >> x >> L;
    if (x < (1 << K)) {
      ll copy = x;
      vector<l_l> v;
      while (copy != 0) {
        v.push_back({V[copy], W[copy]});
        copy /= 2;
      }
      vector<l_l> w = f(v);
      ll ans = 0;
      for (auto tmp : w) {
        if (tmp.first <= L)
          chmax(ans, tmp.second);
      }
      cout << ans << endl;
    } else {
      ll copy = x;
      vector<l_l> v;
      while (copy >= (1 << K)) {
        v.push_back({V[copy], W[copy]});
        copy /= 2;
      }
      auto w = f(v);
      auto q = mid[copy];
      ll ans = 0;
      for (auto tmp : w) {
        l_l s = {L - tmp.first + 1, -INF};
        auto itr = lower_bound(q.begin(), q.end(), s);
        if (itr == q.begin())
          continue;
        itr--;
        chmax(ans, tmp.second + (*itr).second);
      }
      cout << ans << endl;
    }
  }
  return 0;
}