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

#include <bits/stdc++.h>
using namespace std;

#define ris return *this
#define tmplt template <class T
#define dbgo debug &operator<<
tmplt > struct rge {
  T b, e;
};
tmplt > rge<T> range(T i, T j) { return rge<T>{i, j}; }
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  tmplt > dbgo(T x) {
    cerr << boolalpha << x;
    ris;
  }
  tmplt, class C > dbgo(pair<T, C> x) {
    ris << "(" << x.first << ", " << x.second << ")";
  }
  tmplt > dbgo(rge<T> x) {
    *this << "[";
    for (auto it = x.b; it != x.e; it++) {
      *this << ", " + 2 * (it == x.b) << *it;
    }
    ris << "]";
  }
  tmplt > dbgo(vector<T> x) { ris << range(x.begin(), x.end()); }
#else
  tmplt > dbgo(const T &) { ris; }
#endif
};
#define nav(...) << "[ " << #__VA_ARGS__ ": " << (__VA_ARGS__) << " ] "
using ll = long long;

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define ford(i, n) for (int i = n - 1; i >= 0; i--)

template <typename T> void min_self(T &a, T b) { a = min(a, b); }
template <typename T> void max_self(T &a, T b) { a = max(a, b); }

void rec(vector<pair<ll, ll>> &path, vector<pair<ll, ll>> &ans, int i, int n,
         ll w = 0, ll v = 0) {
  if (i == n) {
    ans.emplace_back(w, v);
  } else {
    rec(path, ans, i + 1, n, w + path[i].first, v + path[i].second);
    rec(path, ans, i + 1, n, w, v);
  }
}

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

  int n;
  cin >> n;
  vector<ll> value(n + 1), weight(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> value[i] >> weight[i];
  }
  int q;
  cin >> q;
  vector<vector<pair<int, int>>> queries(n + 1);
  vector<ll> ret(q);
  for (int i = 0; i < q; i++) {
    int u, lim;
    cin >> u >> lim;
    queries[u].emplace_back(lim, i);
  }
  for (int u = 1; u <= n; u++) {
    if (queries[u].empty()) {
      continue;
    }
    vector<pair<ll, ll>> path, left, right;
    int v = u;
    while (v > 0) {
      path.emplace_back(weight[v], value[v]);
      v /= 2;
    }
    int m = path.size();
    rec(path, left, 0, m / 2);
    rec(path, right, m / 2, m);
    sort(right.begin(), right.end());
    int sz = right.size();
    for (int i = 1; i < sz; i++) {
      max_self(right[i].second, right[i - 1].second);
    }
    sz = left.size();
    for (const pair<int, int> &pp : queries[u]) {
      int L = pp.first, k = pp.second;
      for (int i = 0; i < sz; i++) {
        ll used = left[i].first;
        ll cur = left[i].second;
        const ll inf = 1e18;
        int j = upper_bound(right.begin(), right.end(),
                            pair<ll, ll>{L - used, inf}) -
                right.begin();
        --j;
        if (j < 0) {
          continue;
        }
        max_self(ret[k], cur + right[j].second);
      }
    }
  }
  for (int i = 0; i < q; i++) {
    cout << ret[i] << '\\n';
  }
}

// Author: blondie


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ris return *this
#define tmplt template <class T
#define dbgo debug &operator<<
tmplt > struct rge {
  T b, e;
};
tmplt > rge<T> range(T i, T j) { return rge<T>{i, j}; }
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  tmplt > dbgo(T x) {
    cerr << boolalpha << x;
    ris;
  }
  tmplt, class C > dbgo(pair<T, C> x) {
    ris << "(" << x.first << ", " << x.second << ")";
  }
  tmplt > dbgo(rge<T> x) {
    *this << "[";
    for (auto it = x.b; it != x.e; it++) {
      *this << ", " + 2 * (it == x.b) << *it;
    }
    ris << "]";
  }
  tmplt > dbgo(vector<T> x) { ris << range(x.begin(), x.end()); }
#else
  tmplt > dbgo(const T &) { ris; }
#endif
};
#define nav(...) << "[ " << #__VA_ARGS__ ": " << (__VA_ARGS__) << " ] "
using ll = long long;

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define ford(i, n) for (int i = n - 1; i >= 0; i--)

template <typename T> void min_self(T &a, T b) { a = min(a, b); }
template <typename T> void max_self(T &a, T b) { a = max(a, b); }

void rec(vector<pair<ll, ll>> &path, vector<pair<ll, ll>> &ans, int i, int n,
         ll w = 0, ll v = 0) {
  if (i == n) {
    ans.emplace_back(w, v);
  } else {
    rec(path, ans, i + 1, n, w + path[i].first, v + path[i].second);
    rec(path, ans, i + 1, n, w, v);
  }
}

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

  int n;
  cin >> n;
  vector<ll> value(n + 1), weight(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> value[i] >> weight[i];
  }
  int q;
  cin >> q;
  vector<vector<pair<int, int>>> queries(n + 1);
  vector<ll> ret(q);
  for (int i = 0; i < q; i++) {
    int u, lim;
    cin >> u >> lim;
    queries[u].emplace_back(lim, i);
  }
  for (int u = 1; u <= n; u++) {
    vector<pair<ll, ll>> path, left, right;
    int v = u;
    while (v > 0) {
      path.emplace_back(weight[v], value[v]);
      v /= 2;
    }
    int m = path.size();
    rec(path, left, 0, m / 2);
    rec(path, right, m / 2, m);
    sort(right.begin(), right.end());
    int sz = right.size();
    for (int i = 1; i < sz; i++) {
      max_self(right[i].second, right[i - 1].second);
    }
    sz = left.size();
    for (const pair<int, int> &pp : queries[u]) {
      int L = pp.first, k = pp.second;
      for (int i = 0; i < sz; i++) {
        ll used = left[i].first;
        ll cur = left[i].second;
        const ll inf = 1e18;
        int j = upper_bound(right.begin(), right.end(),
                            pair<ll, ll>{L - used, inf}) -
                right.begin();
        --j;
        if (j < 0) {
          continue;
        }
        max_self(ret[k], cur + right[j].second);
      }
    }
  }
  for (int i = 0; i < q; i++) {
    cout << ret[i] << '\\n';
  }
}

// Author: blondie