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

#include <algorithm>
#include <cstdio>
#include <vector>

const int MN = 1 << 18 | 7;

int N, V[MN], W[MN], vis[MN];
std::vector<std::pair<int, int>> A1[MN], A2[MN], tmp, tmp2;

int main() {
  scanf("%d", &N);
  A1[0].push_back({0, 0});
  A2[0].push_back({0, 0});
  for (int i = 1; i <= N; ++i) {
    scanf("%d%d", &V[i], &W[i]);
    if (__builtin_clz(i) & 1) {
      A1[i] = A1[i / 4];
      tmp.resize(A1[i].size());
      for (int j = 0; j < (int)A1[i].size(); ++j)
        tmp[j] = {A1[i][j].first + W[i], A1[i][j].second + V[i]};
      tmp2.resize(2 * A1[i].size());
      std::merge(A1[i].begin(), A1[i].end(), tmp.begin(), tmp.end(),
                 tmp2.begin());
      A1[i] = tmp2;
    } else {
      A2[i] = A2[i / 4];
      tmp.resize(A2[i].size());
      for (int j = 0; j < (int)A2[i].size(); ++j)
        tmp[j] = {A2[i][j].first + W[i], A2[i][j].second + V[i]};
      tmp2.resize(2 * A2[i].size());
      std::merge(A2[i].begin(), A2[i].end(), tmp.begin(), tmp.end(),
                 tmp2.begin());
      A2[i] = tmp2;
    }
    //		for (auto p : A1[i]) printf("<%d, %d>, ", p.first, p.second);
    //puts(""); 		for (auto p : A2[i]) printf("<%d, %d>, ", p.first, p.second);
    //puts("");
  }
  int Q;
  scanf("%d", &Q);
  while (Q--) {
    int v, L;
    scanf("%d%d", &v, &L);
    int v1 = v, v2 = v;
    if (__builtin_clz(v) & 1)
      v2 /= 2;
    else
      v1 /= 2;
    if (!vis[v]) {
      vis[v] = 1;
      for (int i = 1; i < (int)A1[v1].size(); ++i)
        A1[v1][i].second = std::max(A1[v1][i].second, A1[v1][i - 1].second);
      for (int i = 1; i < (int)A2[v2].size(); ++i)
        A2[v2][i].second = std::max(A2[v2][i].second, A2[v2][i - 1].second);
    }
    int Ans = 0;
    int j = (int)A2[v2].size() - 1;
    for (int i = 0; i < (int)A1[v1].size(); ++i) {
      if (A1[v1][i].first > L)
        break;
      while (A1[v1][i].first + A2[v2][j].first > L)
        --j;
      Ans = std::max(Ans, A1[v1][i].second + A2[v2][j].second);
    }
    printf("%d\\n", Ans);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

const int MN = 1 << 18 | 7;

int N, V[MN], W[MN], vis[MN];
std::vector<std::pair<int, int>> A1[MN], A2[MN], tmp, tmp2;

int main() {
  scanf("%d", &N);
  A1[0].push_back({0, 0});
  A2[0].push_back({0, 0});
  for (int i = 1; i <= N; ++i) {
    scanf("%d%d", &V[i], &W[i]);
    A1[i] = A1[i / 2];
    A2[i] = A2[i / 2];
    if (__builtin_clz(i) & 1) {
      tmp.resize(A1[i].size());
      for (int j = 0; j < (int)A1[i].size(); ++j)
        tmp[j] = {A1[i][j].first + W[i], A1[i][j].second + V[i]};
      tmp2.resize(2 * A1[i].size());
      std::merge(A1[i].begin(), A1[i].end(), tmp.begin(), tmp.end(),
                 tmp2.begin());
      A1[i] = tmp2;
    } else {
      tmp.resize(A2[i].size());
      for (int j = 0; j < (int)A2[i].size(); ++j)
        tmp[j] = {A2[i][j].first + W[i], A2[i][j].second + V[i]};
      tmp2.resize(2 * A2[i].size());
      std::merge(A2[i].begin(), A2[i].end(), tmp.begin(), tmp.end(),
                 tmp2.begin());
      A2[i] = tmp2;
    }
    //		for (auto p : A1[i]) printf("<%d, %d>, ", p.first, p.second);
    //puts(""); 		for (auto p : A2[i]) printf("<%d, %d>, ", p.first, p.second);
    //puts("");
  }
  int Q;
  scanf("%d", &Q);
  while (Q--) {
    int v, L;
    scanf("%d%d", &v, &L);
    if (!vis[v]) {
      vis[v] = 1;
      for (int i = 1; i < (int)A1[v].size(); ++i)
        A1[v][i].second = std::max(A1[v][i].second, A1[v][i - 1].second);
      for (int i = 1; i < (int)A2[v].size(); ++i)
        A2[v][i].second = std::max(A2[v][i].second, A2[v][i - 1].second);
    }
    int Ans = 0;
    int j = (int)A2[v].size() - 1;
    for (int i = 0; i < (int)A1[v].size(); ++i) {
      if (A1[v][i].first > L)
        break;
      while (A1[v][i].first + A2[v][j].first > L)
        --j;
      Ans = std::max(Ans, A1[v][i].second + A2[v][j].second);
    }
    printf("%d\\n", Ans);
  }
  return 0;
}