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

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

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

// Inserted snippets: io, opt, root
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template <typename H> bool chmin(H &v1, const H v2) {
  if (v1 > v2) {
    v1 = v2;
    return true;
  }
  return false;
}
template <typename H> bool chmax(H &v1, const H v2) {
  if (v1 < v2) {
    v1 = v2;
    return true;
  }
  return false;
}
template <typename H> void read(H &head) { cin >> head; }
template <typename H, typename... T> void read(H &head, T &...tail) {
  cin >> head;
  read(tail...);
}
template <typename H> void write(H head) { cout << head << '\\n'; }
template <typename H, typename... T> void write(H head, T... tail) {
  cout << head << " ";
  write(tail...);
}
template <typename... T> void die(T... tok) {
  write(tok...);
  exit(0);
}
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
// End snippets

struct It {
  int v, w;
};

vector<pii> dp, dp2, rv;

vector<pii> solve(vector<It>::iterator it1, vector<It>::iterator it2, int L) {
  dp.clear();
  dp.push_back({0, 0});

  for (auto ii = it1; ii != it2; ii++) {
    int s = sz(dp);
    auto it = *ii;
    for (int i = 0; i < s; i++) {
      auto p = dp[i];
      if (p.first + it.w <= L) {
        dp.push_back({p.first + it.w, p.second + it.v});
      }
    }
  }

  sort(all(dp), [](const pii a, const pii b) {
    if (a.first != b.first)
      return a.first < b.first;
    return a.second > b.second;
  });

  rv.clear();
  int cv = 0;

  for (auto p : dp) {
    chmax(cv, p.second);
    if (rv.empty() || p.first != rv.back().first)
      rv.push_back({p.first, cv});
  }

  return rv;
}

vector<It> k;

const int BLK = 1 << 10;
vector<pii> pre[BLK + 1];

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  int n;
  read(n);
  vector<It> kk(n + 1);
  rep(i, 1, n + 1) read(kk[i].v, kk[i].w);

  dp.reserve(1 << 10);
  dp2.reserve(1 << 10);
  rv.reserve(1 << 10);
  k.reserve(20);

  rep(i, 1, BLK + 1) {
    k.clear();
    for (int j = min(i, n); j >= 1; j >>= 1)
      k.push_back(kk[j]);
    pre[i] = solve(k.begin(), k.end(), 100000);
  }

  int q;
  read(q);
  rep(i, 0, q) {
    int v, l;
    read(v, l);

    k.clear();
    int j = v;
    for (j = v; j > BLK; j >>= 1)
      k.push_back(kk[j]);
    vector<pii> &p1 = pre[j];
    vector<pii> p2 = solve(k.begin(), k.end(), l);

    /*	cout << "1" << endl;
            for (auto p : p1) write(p.first, p.second);
            cout << "2" << endl;
            for (auto p : p2) write(p.first, p.second);
     */
    int ans = 0;

    auto it = prev(p2.end());
    for (auto p : p1) {
      if (p.first > l)
        break;
      while (it != p2.begin() && it->first + p.first > l)
        --it;
      if (it->first + p.first > l)
        continue;
      chmax(ans, p.second + it->second);
    }

    write(ans);
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

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

// Inserted snippets: io, opt, root
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template <typename H> bool chmin(H &v1, const H v2) {
  if (v1 > v2) {
    v1 = v2;
    return true;
  }
  return false;
}
template <typename H> bool chmax(H &v1, const H v2) {
  if (v1 < v2) {
    v1 = v2;
    return true;
  }
  return false;
}
template <typename H> void read(H &head) { cin >> head; }
template <typename H, typename... T> void read(H &head, T &...tail) {
  cin >> head;
  read(tail...);
}
template <typename H> void write(H head) { cout << head << '\\n'; }
template <typename H, typename... T> void write(H head, T... tail) {
  cout << head << " ";
  write(tail...);
}
template <typename... T> void die(T... tok) {
  write(tok...);
  exit(0);
}
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
// End snippets

struct It {
  int v, w;
};

vector<pii> dp, dp2, rv;

vector<pii> solve(vector<It>::iterator it1, vector<It>::iterator it2, int L) {
  dp.clear();
  dp.push_back({0, 0});

  for (auto ii = it1; ii != it2; ii++) {
    int s = sz(dp);
    auto it = *ii;
    for (int i = 0; i < s; i++) {
      auto p = dp[i];
      if (p.first + it.w <= L) {
        dp.push_back({p.first + it.w, p.second + it.v});
      }
    }
  }

  sort(all(dp), [](const pii a, const pii b) {
    if (a.first != b.first)
      return a.first < b.first;
    return a.second > b.second;
  });

  rv.clear();
  int cv = 0;

  for (auto p : dp) {
    chmax(cv, p.second);
    if (rv.empty() || p.first != rv.back().first)
      rv.push_back({p.first, cv});
  }

  return rv;
}

vector<It> k;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  int n;
  read(n);
  vector<It> kk(n + 1);
  rep(i, 1, n + 1) read(kk[i].v, kk[i].w);

  dp.reserve(1 << 10);
  dp2.reserve(1 << 10);
  rv.reserve(1 << 10);
  k.reserve(20);

  int q;
  read(q);
  rep(i, 0, q) {
    int v, l;
    read(v, l);

    k.clear();
    for (int j = v; j >= 1; j >>= 1)
      k.push_back(kk[j]);
    vector<pii> p1 = solve(k.begin(), k.begin() + sz(k) / 2, l);
    vector<pii> p2 = solve(k.begin() + sz(k) / 2, k.end(), l);

    /*	cout << "1" << endl;
            for (auto p : p1) write(p.first, p.second);
            cout << "2" << endl;
            for (auto p : p2) write(p.first, p.second);
     */
    int ans = 0;

    auto it = prev(p2.end());
    for (auto p : p1) {
      while (it != p2.begin() && it->first + p.first > l)
        --it;
      if (it->first + p.first > l)
        continue;
      chmax(ans, p.second + it->second);
    }

    write(ans);
  }
}