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

#include <bits/stdc++.h>

int ri() {
  int n;
  scanf("%d", &n);
  return n;
}
std::vector<std::vector<std::pair<int, int>>> qs;
std::vector<int64_t> res;

using Table = std::vector<std::pair<int, int64_t>>;
Table merge(Table a, int cost, int profit) {
  int m = a.size();
  a.push_back({1000000000, 1000000000000000000});
  int left = 0, right = 0;
  Table res;
  while (left < m || right < m) {
    assert(left <= m && right <= m);
    bool consume_left = a[left].first < a[right].first + cost;
    auto tmp = consume_left ? a[left++] : a[right++];
    if (!consume_left)
      tmp.first += cost, tmp.second += profit;
    if (tmp.first > 100000)
      continue;
    while (res.size() && res.back().first == tmp.first &&
           res.back().second < tmp.second)
      res.pop_back();
    if (res.size() && res.back().second >= tmp.second)
      continue;
    res.push_back(tmp);
  }
  return res;
}

std::vector<std::pair<int, int>> info;
Table top{{0, 0}}, bottom{{0, 0}};
int threshold = 11;
int n;
void dfs(int i, int depth) {
  Table bak;
  if (depth < threshold)
    bak = top, top = merge(top, info[i].first, info[i].second);
  else
    bak = bottom, bottom = merge(bottom, info[i].first, info[i].second);
  for (auto j : qs[i]) {
    int head = 0;
    for (int k = bottom.size(); k--;) {
      while (head < (int)top.size() &&
             top[head].first + bottom[k].first <= j.first)
        head++;
      if (head)
        res[j.second] =
            std::max(res[j.second], top[head - 1].second + bottom[k].second);
    }
  }
  if (i < (n - 1) / 2)
    for (int j = 1; j <= 2; j++)
      dfs((i << 1) + j, depth + 1);

  if (depth < threshold)
    top = bak;
  else
    bottom = bak;
}

int main() {
  n = ri();
  info.resize(n);
  for (int i = 0; i < n; i++)
    info[i].second = ri(), info[i].first = ri();

  if (n < 1000) {
    int q = ri();
    for (int i = 0; i < q; i++) {
      int x = ri() - 1;
      int l = ri();
      std::vector<std::pair<int64_t, int64_t>> all{{0, 0}};
      for (int j = x;; j = (j - 1) / 2) {
        auto next = all;
        for (auto k : all)
          next.push_back({k.first + info[j].first, k.second + info[j].second});
        all = next;
        if (!j)
          break;
      }
      int64_t res = 0;
      for (auto i : all)
        if (i.first <= l)
          res = std::max(res, i.second);
      printf("%" PRId64 "\\n", res);
    }
    return 0;
  }

  int q = ri();
  qs.resize(n);
  res.resize(q);
  for (int i = 0; i < q; i++) {
    int x = ri() - 1;
    int l = ri();
    qs[x].push_back({l, i});
  }

  dfs(0, 0);

  for (auto i : res)
    printf("%" PRId64 "\\n", i);

  return 0;
}


Error: -6
ef337c08-f276-4fe6-9e44-62bfc7568a2b.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02648/C++/s601638590.cpp:52: int main(): Assertion `n >= 1000' failed.


Fixed submission:

#include <bits/stdc++.h>

int ri() {
  int n;
  scanf("%d", &n);
  return n;
}
std::vector<std::vector<std::pair<int, int>>> qs;
std::vector<int64_t> res;

using Table = std::vector<std::pair<int, int64_t>>;
Table merge(Table a, int cost, int profit) {
  int m = a.size();
  a.push_back({1000000000, 1000000000000000000});
  int left = 0, right = 0;
  Table res;
  while (left < m || right < m) {
    bool consume_left = a[left].first < a[right].first + cost;
    auto tmp = consume_left ? a[left++] : a[right++];
    if (!consume_left)
      tmp.first += cost, tmp.second += profit;
    if (tmp.first > 100000)
      continue;
    while (res.size() && res.back().first == tmp.first &&
           res.back().second < tmp.second)
      res.pop_back();
    if (res.size() && res.back().second >= tmp.second)
      continue;
    res.push_back(tmp);
  }
  return res;
}

std::vector<std::pair<int, int>> info;
Table top{{0, 0}}, bottom{{0, 0}};
int threshold = 12;
int n;
void dfs(int i, int depth) {
  Table bak;
  if (depth < threshold)
    bak = top, top = merge(top, info[i].first, info[i].second);
  else
    bak = bottom, bottom = merge(bottom, info[i].first, info[i].second);
  for (auto j : qs[i]) {
    int head = 0;
    for (int k = bottom.size(); k--;) {
      while (head < (int)top.size() &&
             top[head].first + bottom[k].first <= j.first)
        head++;
      if (head)
        res[j.second] =
            std::max(res[j.second], top[head - 1].second + bottom[k].second);
    }
  }
  if (i < (n - 1) / 2)
    for (int j = 1; j <= 2; j++)
      dfs((i << 1) + j, depth + 1);

  if (depth < threshold)
    top = bak;
  else
    bottom = bak;
}

int main() {
  n = ri();
  assert(n >= 1000);
  info.resize(n);
  for (int i = 0; i < n; i++)
    info[i].second = ri(), info[i].first = ri();

  int q = ri();
  qs.resize(n);
  res.resize(q);
  for (int i = 0; i < q; i++) {
    int x = ri() - 1;
    int l = ri();
    qs[x].push_back({l, i});
  }

  dfs(0, 0);

  for (auto i : res)
    printf("%" PRId64 "\\n", i);

  return 0;
}