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<int> res;

using Table = std::vector<std::pair<int, int>>;
Table merge(Table a, int cost, int profit) {
  int m = a.size();
  a.push_back({1000000000, 1000000000});
  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;
    // 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) {
  if (i >= n)
    return;
  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;
    int max = -1000000000;
    for (int k = bottom.size(); k--;) {
      while (head < (int)top.size() &&
             top[head].first + bottom[k].first <= j.first)
        max = std::max(max, top[head++].second);
      res[j.second] = std::max(res[j.second], max + bottom[k].second);
    }
  }
  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();

  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("%d\\n", i);

  return 0;
}


Error: 0

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<int> res;

using Table = std::vector<std::pair<int, int>>;
Table merge(Table a, int cost, int profit) {
  int m = a.size();
  a.push_back({1000000000, 1000000000});
  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;
    // 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;
    int max = -1000000000;
    for (int k = bottom.size(); k--;) {
      while (head < (int)top.size() &&
             top[head].first + bottom[k].first <= j.first)
        max = std::max(max, top[head++].second);
      res[j.second] = std::max(res[j.second], max + 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() {
  int n_ = ri();
  for (n = 1; n < n_; n <<= 1)
    ;
  n--;
  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("%d\\n", i);

  return 0;
}