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

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second

using namespace std;

typedef long long ll;

const int LAST = 11;

int value[(1 << 18) + 7], weight[(1 << 18) + 7];
vector<pair<int, int>> spis[(1 << 18) + 7];
int deep[(1 << 18) + 7];

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, quer;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> value[i] >> weight[i];

  for (int i = 2; i <= n; i++)
    deep[i] = deep[i / 2] + 1;

  spis[0].pb(mp(0, 0));
  for (int i = 1; i <= n; i++) {
    if (deep[i] == LAST + 1) {
      spis[i].pb(mp(0, 0)), spis[i].pb(mp(weight[i], value[i]));
      continue;
    }
    spis[i] = spis[i / 2];

    vector<pair<int, int>> cop;
    for (int j = 0; j < (int)spis[i].size(); j++)
      cop.pb(mp(spis[i][j].x + weight[i], spis[i][j].y + value[i]));
    vector<pair<int, int>> cops((int)cop.size() + (int)spis[i].size());
    merge(spis[i].begin(), spis[i].end(), cop.begin(), cop.end(), cops.begin());
    spis[i] = cops;
  }

  cin >> quer;
  for (int rep = 1; rep <= quer; rep++) {
    int vert, cap;
    cin >> vert >> cap;

    if (deep[vert] <= LAST) {
      int ans = 0;
      for (int i = 0; i < (int)spis[vert].size(); i++)
        if (spis[vert][i].x <= cap)
          ans = max(ans, spis[vert][i].y);
      cout << ans << '\\n';
      continue;
    }

    int mid = 0, cop = vert;
    while (cop > 0) {
      if (deep[cop] == LAST)
        mid = cop;
      cop /= 2;
    }
    int uk = -1, tek = 0, ans = 0;
    for (int i = (int)spis[vert].size() - 1; i >= 0; i--) {
      while (uk < (int)spis[mid].size() - 1 &&
             spis[mid][uk + 1].x + spis[vert][i].x <= cap) {
        uk++;
        tek = max(tek, spis[mid][uk].y);
      }
      if (uk != -1)
        ans = max(ans, spis[vert][i].y + tek);
    }
    cout << ans << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second

using namespace std;

typedef long long ll;

int value[(1 << 18) + 7], weight[(1 << 18) + 7];

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, quer;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> value[i] >> weight[i];
  cin >> quer;
  for (int rep = 1; rep <= quer; rep++) {
    int vert, cap;
    cin >> vert >> cap;

    vector<pair<int, int>> spis, ans1, ans2;
    while (vert > 0) {
      spis.pb(mp(weight[vert], value[vert]));
      vert /= 2;
    }
    int kol1 = (int)spis.size() / 2;
    int kol2 = (int)spis.size() - kol1;
    for (int mask = 0; mask < (1 << kol1); mask++) {
      int val = 0, wei = 0;
      for (int i = 0; i < kol1; i++)
        if ((mask >> i) & 1)
          wei += spis[i].x, val += spis[i].y;
      if (wei <= cap)
        ans1.pb(mp(wei, val));
    }
    for (int mask = 0; mask < (1 << kol2); mask++) {
      int val = 0, wei = 0;
      for (int i = 0; i < kol2; i++)
        if ((mask >> i) & 1)
          wei += spis[i + kol1].x, val += spis[i + kol1].y;
      if (wei <= cap)
        ans2.pb(mp(wei, val));
    }
    sort(ans1.begin(), ans1.end());
    sort(ans2.begin(), ans2.end());
    int uk = -1, tek = 0, ans = 0;
    for (int i = (int)ans1.size() - 1; i >= 0; i--) {
      while (uk < (int)ans2.size() - 1 && ans2[uk + 1].x + ans1[i].x <= cap) {
        uk++;
        tek = max(tek, ans2[uk].y);
      }
      if (uk != -1)
        ans = max(ans, ans1[i].y + tek);
    }
    cout << ans << '\\n';
  }
  return 0;
}