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

#include <algorithm>
#include <climits>
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;

#define ALL(v) begin(v), end(v)
typedef pair<int, int> pii;

int main() {
  int n;
  scanf("%d", &n);
  vector<pii> vals(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", &vals[i].second, &vals[i].first);
  }
  int thr = min(n, 5000);
  vector<vector<pii>> rs(thr);
  rs[0].emplace_back(0, 0);
  rs[0].emplace_back(vals[0]);
  for (int u = 1; u < thr; ++u) {
    int par = (u - 1) >> 1;
    rs[u] = rs[par];
    for (const pii &p : rs[par]) {
      rs[u].emplace_back(p.first + vals[u].first, p.second + vals[u].second);
    }
    sort(ALL(rs[u]));
  }
  for (int u = 0; u < thr; ++u) {
    int prv = 0;
    for (pii &p : rs[u]) {
      p.second = prv = max(p.second, prv);
    }
  }

  int q;
  scanf("%d", &q);
  vector<pii> sub;
  for (; q--;) {
    int v, l;
    scanf("%d%d", &v, &l);
    --v;

    int ans = 0;
    if (v < thr) {
      ans = upper_bound(ALL(rs[v]), pii(l, INT_MAX))[-1].second;
    } else {
      sub.clear();
      while (v >= thr) {
        sub.push_back(vals[v]);
        v = (v - 1) >> 1;
      }

      const vector<pii> &r = rs[v];
      int sz = sub.size();
      for (int S = 1 << sz; S--;) {
        int x = 0, y = 0;
        for (int i = 0; i < sz; ++i) {
          if (S >> i & 1) {
            x += sub[i].first;
            y += sub[i].second;
          }
        }
        if (x > l) {
          continue;
        }
        int z = upper_bound(ALL(r), pii(l - x, INT_MAX))[-1].second;
        ans = max(ans, y + z);
      }
    }
    printf("%d\\n", ans);
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;

#define ALL(v) begin(v), end(v)
typedef pair<int, int> pii;

int main() {
  int n;
  scanf("%d", &n);
  vector<pii> vals(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", &vals[i].second, &vals[i].first);
  }
  int p2h;
  for (p2h = 1; p2h * p2h < n; p2h *= 2)
    ;
  vector<vector<pii>> rs(p2h);
  rs[0].emplace_back(0, 0);
  rs[0].emplace_back(vals[0]);
  for (int u = 1; u < p2h; ++u) {
    int par = (u - 1) >> 1;
    rs[u] = rs[par];
    for (const pii &p : rs[par]) {
      rs[u].emplace_back(p.first + vals[u].first, p.second + vals[u].second);
    }
    sort(ALL(rs[u]));
  }
  for (int u = 0; u < p2h; ++u) {
    int prv = 0;
    for (pii &p : rs[u]) {
      p.second = prv = max(p.second, prv);
    }
  }

  int q;
  scanf("%d", &q);
  vector<pii> sub;
  for (; q--;) {
    int v, l;
    scanf("%d%d", &v, &l);
    --v;

    int ans = 0;
    if (v < p2h) {
      ans = upper_bound(ALL(rs[v]), pii(l, INT_MAX))[-1].second;
    } else {
      sub.clear();
      while (v >= p2h) {
        sub.push_back(vals[v]);
        v = (v - 1) >> 1;
      }

      const vector<pii> &r = rs[v];
      int sz = sub.size();
      for (int S = 1 << sz; S--;) {
        int x = 0, y = 0;
        for (int i = 0; i < sz; ++i) {
          if (S >> i & 1) {
            x += sub[i].first;
            y += sub[i].second;
          }
        }
        if (x > l) {
          continue;
        }
        int z = upper_bound(ALL(r), pii(l - x, INT_MAX))[-1].second;
        ans = max(ans, y + z);
      }
    }
    printf("%d\\n", ans);
  }
}