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

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
const int M = 1000000007;

vector<int> v, w;

vector<int> getvec(int p) {
  int lim = 100010;
  vector<int> vv, ww;
  for (int j = p;; j = (j - 1) >> 1) {
    vv.push_back(v[j]);
    ww.push_back(w[j]);
    if (j == 0)
      break;
  }

  int m = vv.size();
  vector<int> lis(lim);
  for (int i = 0; i < (1 << m); ++i) {
    int vs = 0, ws = 0;
    for (int j = 0; j < m; ++j) {
      if ((i >> j) & 1) {
        vs += vv[j];
        ws += ww[j];
      }
    }
    if (ws < lim) {
      lis[ws] = max(lis[ws], vs);
    }
  }
  for (int i = 1; i < lim; ++i) {
    lis[i] = max(lis[i], lis[i - 1]);
  }
  return lis;
}

vector<P> getvec(int p, int par) {
  int lim = 100010;
  vector<int> vv, ww;
  for (int j = p;; j = (j - 1) >> 1) {
    vv.push_back(v[j]);
    ww.push_back(w[j]);
    if (j == par)
      break;
  }

  int m = vv.size();
  vector<P> lis;
  for (int i = 0; i < (1 << m); ++i) {
    int vs = 0, ws = 0;
    for (int j = 0; j < m; ++j) {
      if ((i >> j) & 1) {
        vs += vv[j];
        ws += ww[j];
      }
    }
    if (ws < lim) {
      lis.push_back({ws, vs});
    }
  }
  return lis;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  v = vector<int>(n);
  w = vector<int>(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i] >> w[i];
  }
  int q;
  cin >> q;
  vector<vector<P>> que(n, vector<P>());
  for (int i = 0; i < q; ++i) {
    int u, l;
    cin >> u >> l;
    --u;
    que[u].push_back({l, i});
  }
  vector<vector<int>> memo(1 << 10);
  vector<int> ans(q);
  for (int i = 0; i < n; ++i) {
    if (i < (1 << 10)) {
      vector<int> lis = getvec(i);
      memo[i] = lis;
      for (auto &p : que[i]) {
        int l = p.first;
        ans[p.second] = lis[l];
      }
    } else {
      int pp = i;
      while (((pp - 1) >> 1) >= (1 << 10)) {
        pp = (pp - 1) >> 1;
      }
      const vector<int> &lis1 = memo[(pp - 1) >> 1];
      vector<P> lis2 = getvec(i, pp);

      for (auto &p : que[i]) {
        int l = p.first;
        int ma = 0;
        for (auto &j : lis2) {
          if (j.first <= l) {
            ma = max(ma, lis1[l - j.first] + j.second);
          }
        }
        ans[p.second] = ma;
      }
    }
  }
  for (auto &i : ans) {
    cout << i << '\\n';
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
const int M = 1000000007;

vector<int> v, w;

vector<int> getvec(int p) {
  int lim = 100010;
  vector<int> vv, ww;
  for (int j = p;; j = (j - 1) >> 1) {
    vv.push_back(v[j]);
    ww.push_back(w[j]);
    if (j == 0)
      break;
  }

  int m = vv.size();
  vector<int> lis(lim);
  for (int i = 0; i < (1 << m); ++i) {
    int vs = 0, ws = 0;
    for (int j = 0; j < m; ++j) {
      if ((i >> j) & 1) {
        vs += vv[j];
        ws += ww[j];
      }
    }
    if (ws < lim) {
      lis[ws] = max(lis[ws], vs);
    }
  }
  for (int i = 1; i < lim; ++i) {
    lis[i] = max(lis[i], lis[i - 1]);
  }
  return lis;
}

vector<P> getvec(int p, int par) {
  int lim = 100010;
  vector<int> vv, ww;
  for (int j = p;; j = (j - 1) >> 1) {
    vv.push_back(v[j]);
    ww.push_back(w[j]);
    if (j == par)
      break;
  }

  int m = vv.size();
  vector<P> lis;
  for (int i = 0; i < (1 << m); ++i) {
    int vs = 0, ws = 0;
    for (int j = 0; j < m; ++j) {
      if ((i >> j) & 1) {
        vs += vv[j];
        ws += ww[j];
      }
    }
    if (ws < lim) {
      lis.push_back({ws, vs});
    }
  }
  return lis;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  v = vector<int>(n);
  w = vector<int>(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i] >> w[i];
  }
  int q;
  cin >> q;
  vector<vector<P>> que(n, vector<P>());
  for (int i = 0; i < q; ++i) {
    int u, l;
    cin >> u >> l;
    --u;
    que[u].push_back({l, i});
  }
  vector<vector<int>> memo(1 << 9);
  vector<int> ans(q);
  for (int i = 0; i < n; ++i) {
    if (i < (1 << 9)) {
      vector<int> lis = getvec(i);
      memo[i] = lis;
      for (auto &p : que[i]) {
        int l = p.first;
        ans[p.second] = lis[l];
      }
    } else {
      int pp = i;
      while (((pp - 1) >> 1) >= (1 << 9)) {
        pp = (pp - 1) >> 1;
      }
      const vector<int> &lis1 = memo[(pp - 1) >> 1];
      vector<P> lis2 = getvec(i, pp);

      for (auto &p : que[i]) {
        int l = p.first;
        int ma = 0;
        for (auto &j : lis2) {
          if (j.first <= l) {
            ma = max(ma, lis1[l - j.first] + j.second);
          }
        }
        ans[p.second] = ma;
      }
    }
  }
  for (auto &i : ans) {
    cout << i << '\\n';
  }

  return 0;
}