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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int MAXN = 1 << 18;

int V[MAXN], W[MAXN];
int bap[MAXN];
vector<PII> options[MAXN];
// vector<PII>options[MAXN];

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

  int N;
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> V[i] >> W[i];
  int z = sqrt(N);
  z = max(z, 1);

  options[1].emplace_back(0, 0);
  options[1].emplace_back(W[1], V[1]);
  for (int i = 2; i <= z; i++) {
    int par = i / 2;
    options[i] = options[par];
    for (PII p : options[par]) {
      options[i].emplace_back(p.first + W[i], p.second + V[i]);
    }
    sort(options[i].begin(), options[i].end());
  }
  for (int i = z + 1; i <= N; i++) {
    int par = i / 2;
    if (par > z) {
      bap[i] = bap[par];
      vector<PII> with = options[par];
      for (PII &p : with) {
        p.first += W[i];
        p.second += V[i];
      }
      merge(options[par].begin(), options[par].end(), with.begin(), with.end(),
            back_inserter(options[i]));
      //            sort(options[i].begin(), options[i].end());
    } else {
      bap[i] = par;
      options[i].emplace_back(0, 0);
      options[i].emplace_back(W[i], V[i]);
    }
  }
  for (int i = 1; i <= N; i++) {
    //        cout << i;
    //        if (i <= z) cout << " inside" << endl;
    //        else cout << " outside" << endl;
    //        for (PII p : options[i]) {
    //            cout << p.first << "," << p.second << ":";
    //        }
    //        cout << endl;
    for (int j = 1; j < options[i].size(); j++) {
      options[i][j].second =
          max(options[i][j].second, options[i][j - 1].second);
    }
  }

  int q;
  cin >> q;
  while (q--) {
    int v, L;
    cin >> v >> L;

    if (v <= z) {
      int idx =
          lower_bound(options[v].begin(), options[v].end(), PII(L + 1, -1)) -
          options[v].begin();
      idx--;
      assert(idx >= 0);
      cout << options[v][idx].second << "\\n";
    } else {
      int p = bap[v];
      int mx = 0;
      for (int i = 0, j = options[p].size() - 1;
           i < options[v].size() && options[v][i].first <= L; i++) {
        while (options[v][i].first + options[p][j].first > L) {
          j--;
        }
        mx = max(mx, options[v][i].second + options[p][j].second);
      }
      cout << mx << "\\n";
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int MAXN = 1 << 18;

int V[MAXN], W[MAXN];
int bap[MAXN];
vector<PII> options[MAXN];
// vector<PII>options[MAXN];

int main() {
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> V[i] >> W[i];
  int z = sqrt(N);
  z = max(z, 1);

  options[1].emplace_back(0, 0);
  options[1].emplace_back(W[1], V[1]);
  for (int i = 2; i <= z; i++) {
    int par = i / 2;
    options[i] = options[par];
    for (PII p : options[par]) {
      options[i].emplace_back(p.first + W[i], p.second + V[i]);
    }
    sort(options[i].begin(), options[i].end());
  }
  for (int i = z + 1; i <= N; i++) {
    int par = i / 2;
    if (par > z) {
      options[i] = options[par];
      bap[i] = bap[par];
      for (PII p : options[par]) {
        options[i].emplace_back(p.first + W[i], p.second + V[i]);
      }
      sort(options[i].begin(), options[i].end());
    } else {
      bap[i] = par;
      options[i].emplace_back(0, 0);
      options[i].emplace_back(W[i], V[i]);
    }
  }
  for (int i = 1; i <= N; i++) {
    //        cout << i;
    //        if (i <= z) cout << " inside" << endl;
    //        else cout << " outside" << endl;
    //        for (PII p : options[i]) {
    //            cout << p.first << "," << p.second << ":";
    //        }
    //        cout << endl;
    for (int j = 1; j < options[i].size(); j++) {
      options[i][j].second =
          max(options[i][j].second, options[i][j - 1].second);
    }
  }

  int q;
  cin >> q;
  while (q--) {
    int v, L;
    cin >> v >> L;

    if (v <= z) {
      int idx =
          lower_bound(options[v].begin(), options[v].end(), PII(L + 1, -1)) -
          options[v].begin();
      idx--;
      assert(idx >= 0);
      cout << options[v][idx].second << "\\n";
    } else {
      int p = bap[v];
      int mx = 0;
      for (int i = 0, j = options[p].size() - 1;
           i < options[v].size() && options[v][i].first <= L; i++) {
        while (options[v][i].first + options[p][j].first > L) {
          j--;
        }
        mx = max(mx, options[v][i].second + options[p][j].second);
      }
      cout << mx << "\\n";
    }
  }

  return 0;
}