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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
constexpr char ln = '\\n';

const int mx = 100010;
const ll mod = 1e9 + 7;

#define bit(n, k) ((n >> k) & 1) //*n no k bit me 1 or 0*/

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> v(n + 1), w(n + 1);
  rep(i, n) { cin >> v[i + 1] >> w[i + 1]; }
  vector<vector<P>> data((1 << 10) + 5); // d9 made hanbun-zenrekkyo
  REP(i, 1, min(n + 1, 1 << 10)) {
    vector<int> ls;
    if (i == 1) {
      data[i].emplace_back(w[i], v[i]);
      data[i].emplace_back(0, 0);
    } else {
      for (auto it : data[i / 2]) {
        data[i].emplace_back(it.F, it.S);
        data[i].emplace_back(it.F + w[i], it.S + v[i]);
      }
    }

    sort(data[i].begin(), data[i].end());
    REP(j, 1, data[i].size()) {
      data[i][j].S = max(data[i][j].S, data[i][j - 1].S);
    }
  }

  int q;
  cin >> q;
  while (q--) {
    int x, L;
    cin >> x >> L;
    if (x < min(n + 1, 1 << 10)) {
      int pos = lower_bound(data[x].begin(), data[x].end(), P(L + 1, 0)) -
                data[x].begin();
      int ans = data[x][pos - 1].S;
      cout << ans << ln;
    } else {
      vector<int> ls;
      int tmp = x;
      while (tmp >= (1 << 10)) {
        ls.emplace_back(tmp);
        tmp /= 2;
      }
      //      sort(ls.rbegin(),ls.rend());
      int sz = ls.size();
      int ans = 0;
      vector<int> d;
      for (auto it : data[tmp]) {
        d.emplace_back(it.F);
      }
      rep(b, 1 << sz) {
        int vsum = 0, wsum = 0;
        rep(j, sz) if (bit(b, j)) {
          vsum += v[ls[j]];
          wsum += w[ls[j]];
        }
        if (wsum > L)
          continue;
        int pos = lower_bound(d.begin(), d.end(), L - wsum + 1) - d.begin();
        // int pos =
        // lower_bound(data[tmp].begin(),data[tmp].end(),P(L-wsum+1,0)) -
        // data[tmp].begin();
        ans = max(ans, vsum + data[tmp][pos - 1].S);
      }
      cout << ans << ln;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
constexpr char ln = '\\n';

const int mx = 100010;
const ll mod = 1e9 + 7;

#define bit(n, k) ((n >> k) & 1) //*n no k bit me 1 or 0*/

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> v(n + 1), w(n + 1);
  rep(i, n) { cin >> v[i + 1] >> w[i + 1]; }
  vector<vector<P>> data((1 << 9) + 5); // d9 made hanbun-zenrekkyo
  REP(i, 1, min(n + 1, 1 << 9)) {
    vector<int> ls;
    if (i == 1) {
      data[i].emplace_back(w[i], v[i]);
      data[i].emplace_back(0, 0);
    } else {
      for (auto it : data[i / 2]) {
        data[i].emplace_back(it.F, it.S);
        data[i].emplace_back(it.F + w[i], it.S + v[i]);
      }
    }

    sort(data[i].begin(), data[i].end());
    REP(j, 1, data[i].size()) {
      data[i][j].S = max(data[i][j].S, data[i][j - 1].S);
    }
  }

  int q;
  cin >> q;
  while (q--) {
    int x, L;
    cin >> x >> L;
    if (x < min(n + 1, 1 << 9)) {
      int pos = lower_bound(data[x].begin(), data[x].end(), P(L + 1, 0)) -
                data[x].begin();
      int ans = data[x][pos - 1].S;
      cout << ans << ln;
    } else {
      vector<int> ls;
      int tmp = x;
      while (tmp >= (1 << 9)) {
        ls.emplace_back(tmp);
        tmp /= 2;
      }
      //      sort(ls.rbegin(),ls.rend());
      int sz = ls.size();
      int ans = 0;
      vector<int> d;
      for (auto it : data[tmp]) {
        d.emplace_back(it.F);
      }
      rep(b, 1 << sz) {
        int vsum = 0, wsum = 0;
        rep(j, sz) if (bit(b, j)) {
          vsum += v[ls[j]];
          wsum += w[ls[j]];
        }
        if (wsum > L)
          continue;
        int pos = lower_bound(d.begin(), d.end(), L - wsum + 1) - d.begin();
        // int pos =
        // lower_bound(data[tmp].begin(),data[tmp].end(),P(L-wsum+1,0)) -
        // data[tmp].begin();
        ans = max(ans, vsum + data[tmp][pos - 1].S);
      }
      cout << ans << ln;
    }
  }

  return 0;
}