Problem p02648 (C++) — buggy submission, verdict: Runtime Error

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

int wlim = 100010;

void push(vector<P> &res, const P &p) {
  if (p.first < wlim &&
      (res.size() == 0 || p.second > res[(int)res.size() - 1].second)) {
    res.push_back(p);
  }
}

vector<P> update(const vector<P> &lis, int v, int w) {
  int sz = lis.size();
  int p1 = 0, p2 = 0;
  vector<P> res;
  while (p1 < sz || p2 < sz) {
    if (p2 == sz) {
      push(res, lis[p1]);
      ++p1;
    } else if (p1 == sz) {
      push(res, {lis[p2].first + w, lis[p2].second + v});
      ++p2;
    } else {
      if (lis[p1].first < lis[p2].first + w) {
        push(res, lis[p1]);
        ++p1;
      } else if (lis[p1].first > lis[p2].first + w) {
        push(res, {lis[p2].first + w, lis[p2].second + v});
        ++p2;
      } else {
        push(res, {lis[p1].first, max(lis[p1].second, lis[p2].second + v)});
        ++p1;
        ++p2;
      }
    }
  }
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> v(n), w(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});
  }
  int lm = 1 << 10;
  vector<vector<P>> vs(lm);
  vs[0] = {{0, 0}};
  vector<int> ans(q);
  for (int i = 0; i < n; ++i) {
    if (i < lm) {
      vs[i] = update(vs[(i - 1) / 2], v[i], w[i]);
      for (auto &p : que[i]) {
        int ma = 0;
        for (auto &j : vs[i]) {
          if (j.first <= p.first) {
            ma = max(ma, j.second);
          }
        }
        ans[p.second] = ma;
      }
    } else {
      int a = i;
      vector<P> lis = {{0, 0}};
      while (a >= lm) {
        lis = update(lis, v[a], w[a]);
        a = (a - 1) >> 1;
      }

      for (auto &p : que[i]) {
        int l = p.first;
        int k = (int)vs[a].size() - 1;
        int ma = 0;
        for (int j = 0; j < (int)lis.size(); ++j) {
          while (k >= 0 && lis[j].first + vs[a][k].first > l) {
            --k;
          }
          if (k < 0)
            break;
          ma = max(ma, lis[j].second + vs[a][k].second);
        }
        ans[p.second] = ma;
      }
    }
  }
  for (auto &i : ans) {
    cout << i << '\\n';
  }
  return 0;
}


Error: 0

Fixed submission:

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

int wlim = 100010;

vector<P> update(const vector<P> &lis, int v, int w) {
  int sz = lis.size();
  int p1 = 0, p2 = 0;
  vector<P> res;
  while (p1 < sz || p2 < sz) {
    if (p2 == sz) {
      res.push_back(lis[p1]);
      ++p1;
    } else if (p1 == sz) {
      if (lis[p2].first + w < wlim) {
        res.push_back({lis[p2].first + w, lis[p2].second + v});
      }
      ++p2;
    } else {
      if (lis[p1].first < lis[p2].first + w) {
        res.push_back(lis[p1]);
        ++p1;
      } else if (lis[p1].first > lis[p2].first + w) {
        if (lis[p2].first + w < wlim) {
          res.push_back({lis[p2].first + w, lis[p2].second + v});
        }
        ++p2;
      } else {
        res.push_back({lis[p1].first, max(lis[p1].second, lis[p2].second + v)});
        ++p1;
        ++p2;
      }
    }
  }
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> v(n), w(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});
  }
  int lm = 1 << 10;
  vector<vector<P>> vs(lm);
  vs[0] = {{0, 0}};
  vector<int> ans(q);
  for (int i = 0; i < n; ++i) {
    if (i < lm) {
      vs[i] = update(vs[(i - 1) / 2], v[i], w[i]);
      for (auto &p : que[i]) {
        int ma = 0;
        for (auto &j : vs[i]) {
          if (j.first <= p.first) {
            ma = max(ma, j.second);
          }
        }
        ans[p.second] = ma;
      }
    } else {
      int l = i;
      vector<P> lis = {{0, 0}};
      while (l >= lm) {
        lis = update(lis, v[l], w[l]);
        l = (l - 1) >> 1;
      }

      for (auto &p : que[i]) {
        int l = p.first;
        int k = (int)vs[l].size() - 1;
        int ma = 0;
        for (int j = 0; j < (int)lis.size(); ++j) {
          while (k >= 0 && lis[j].first + vs[l][k].first > l) {
            --k;
          }
          if (k < 0)
            break;
          ma = max(ma, lis[j].second + vs[l][k].second);
        }
        ans[p.second] = ma;
      }
    }
  }
  for (auto &i : ans) {
    cout << i << '\\n';
  }
  return 0;
}