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

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 300300;
typedef pair<int, int> P;
typedef vector<P> VP;
P a[N];
VP ans[N];
int v[N], l[N];
int mxl[N];
int d[N];
const int X = 9;
int main() {
  memset(mxl, -1, sizeof(mxl));
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  d[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].second >> a[i].first;
    d[i] = d[i >> 1] + 1;
  }
  int q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> v[i] >> l[i];
    mxl[v[i]] = max(mxl[v[i]], l[i]);
  }
  for (int i = n; i >= 1; i--) {
    mxl[i >> 1] = max(mxl[i >> 1], mxl[i]);
  }
  ans[0].push_back(P(0, 0));
  for (int i = 1; i <= n; i++) {
    P p = a[i];
    VP &ps = ans[i];
    const VP &qaq = d[i] == X + 1 ? ans[0] : ans[i >> 1];
    // cerr<<i<<" "<<(d[i]==X+1)<<endl;
    int w = qaq.size();
    ps = qaq;
    for (P tp : qaq) {
      if (tp.first + p.first <= mxl[i])
        ps.push_back(P{tp.first + p.first, tp.second + p.second});
      else
        break;
    }
    inplace_merge(ps.begin(), ps.begin() + w, ps.end());
    for (int i = 0; i + 1 < (int)ps.size(); i++) {
      if (ps[i + 1].second <= ps[i].second)
        ps[i + 1] = ps[i];
    }
    for (int i = (int)ps.size() - 1; i >= 1; i--) {
      if (ps[i].first == ps[i - 1].first)
        ps[i - 1] = ps[i];
    }
    ps.erase(unique(ps.begin(), ps.end()), ps.end());
    assert(is_sorted(ps.begin(), ps.end()));
  }
  for (int i = 1; i <= q; i++) {
    // cerr<<"query "<<i<<endl;
    int x = v[i];
    const VP &v1 = ans[x];
    // cerr<<"v1: "<<x<<endl;
    if (d[x] <= X) {
      // cerr<<i<<endl;
      cout << (--upper_bound(v1.begin(), v1.end(), l[i],
                             [](int l, P p) { return l < p.first; }))
                  ->second
           << endl;
      continue;
    }
    while (d[x] >= X + 1)
      x >>= 1;
    const VP &v2 = ans[x];
    // cerr<<"v2: "<<x<<endl;
    VP::const_iterator w = --v2.end();
    int ans = 0;
    for (P p : v1) {
      while (w->first + p.first > l[i]) {
        if (w == v2.begin())
          goto no;
        w--;
      }
      ans = max(ans, w->second + p.second);
    }
  no:;
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 300300;
int w[N], v[N];
int lg[N];
typedef pair<int, int> P;
typedef vector<P> VP;
VP qaq(VP a, int l) {
  VP ps;
  ps.push_back(make_pair(0, 0));
  for (P p : a) {
    VP tmp;
    for (P tp : ps) {
      if (tp.first + p.first <= l) {
        tmp.push_back(P{tp.first + p.first, tp.second + p.second});
      } else {
        break;
      }
    }
    int w = ps.size();
    ps.insert(ps.end(), tmp.begin(), tmp.end());
    inplace_merge(ps.begin(), ps.begin() + w, ps.end());
    for (int i = 0; i + 1 < (int)ps.size(); i++) {
      if (ps[i + 1].second <= ps[i].second)
        ps[i + 1] = ps[i];
    }
    for (int i = (int)ps.size() - 1; i >= 1; i--) {
      if (ps[i].first == ps[i - 1].first)
        ps[i - 1] = ps[i];
    }
    ps.erase(unique(ps.begin(), ps.end()), ps.end());
  }
  return VP(ps.begin(), ps.end());
}
int solve(VP a, int l) {
  int n = a.size();
  int mid = n / 2;
  VP m1 = qaq(VP(a.begin(), a.begin() + mid), l);
  VP m2 = qaq(VP(a.begin() + mid, a.end()), l);
  int ans = 0;
  VP::iterator it = --m2.end();
  for (P p : m1) {
    while (it->first + p.first > l) {
      it--;
    }
    ans = max(ans, p.second + it->second);
  }
  return ans;
}
int main() {
  for (int i = 0; (1 << i) < N; i++)
    lg[1 << i] = i;
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int u, l;
    cin >> u >> l;
    VP a;
    while (u) {
      a.push_back(P(w[u], v[u]));
      u >>= 1;
    }
    cout << solve(a, l) << endl;
  }
  return 0;
}