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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, ll> pl;

int N, Q, V[(1 << 18)], W[(1 << 18)], x[100005], L[100005], hasquery[(1 << 18)];
ll ans[100005];
vector<pl> queries[(1 << 18)];
vector<pl> depth[(1 << 18)];

void dfs(int x, int d) {
  // cerr << x << ' ' << d << '\\n';
  vector<pl> v;
  // cerr << x << ' ' << x/2 << '\\n';
  if (d == 12 || d == 1) {
    depth[x].push_back(pl(0, 0));
    depth[x].push_back(pl(W[x], V[x]));
  } else {
    for (auto it : depth[x / 2]) {
      if (it.first + W[x] > 100000)
        break;
      v.push_back(pl(it.first + W[x], it.second + V[x]));
    }
    int ptd = 0, ptv = 0;
    ll bval = -1;
    while (ptd < depth[x / 2].size() && ptv < v.size()) {
      if (depth[x / 2][ptd].first < v[ptv].first) {
        if (depth[x / 2][ptd].second > bval) {
          depth[x].push_back(depth[x / 2][ptd]);
          bval = depth[x / 2][ptd].second;
        }
        ptd++;
      } else {
        if (v[ptv].second > bval) {
          depth[x].push_back(v[ptv]);
          bval = v[ptv].second;
        }
        ptv++;
      }
    }
    while (ptd < depth[x / 2].size()) {
      if (depth[x / 2][ptd].second > bval) {
        depth[x].push_back(depth[x / 2][ptd]);
        bval = depth[x / 2][ptd].second;
      }
      ptd++;
    }
    while (ptv < v.size()) {
      if (v[ptv].second > bval) {
        depth[x].push_back(v[ptv]);
        bval = v[ptv].second;
      }
      ptv++;
    }
  }

  /*
  cerr << x << '\\n';
  for (auto it : depth[x]){
          cerr << it.first << ' ';
  }
  cerr << '\\n';
  */
  ll p = x;
  if (d >= 12)
    p >>= (d - 12 + 1);
  else
    p = 0;

  for (auto it : queries[x]) {
    int pt1 = 0, pt2 = depth[p].size() - 1;
    while (pt1 < depth[x].size()) {
      while (pt2 >= 0 && depth[p][pt2].first + depth[x][pt1].first > it.first) {
        pt2--;
      }
      if (pt2 >= 0)
        ans[it.second] =
            max(ans[it.second], depth[x][pt1].second + depth[p][pt2].second);
      pt1++;
    }
  }
  if (2 * x <= N)
    dfs(2 * x, d + 1);
  if (2 * x + 1 <= N)
    dfs(2 * x + 1, d + 1);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  for (int i = 1; i <= N; ++i) {
    cin >> V[i] >> W[i];
  }
  cin >> Q;
  for (int i = 1; i <= Q; ++i) {
    cin >> x[i] >> L[i];
    queries[x[i]].push_back(pl(L[i], i));
    while (x[i] > 0 && !hasquery[x[i]]) {
      hasquery[x[i]] = 1;
      x[i] /= 2;
    }
  }
  for (int i = 1; i <= N; ++i) {
    sort(queries[i].begin(), queries[i].end());
  }
  depth[0].push_back(pl(0, 0));
  dfs(1, 1);
  for (int i = 1; i <= Q; ++i) {
    cout << ans[i] << '\\n';
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, ll> pl;

int N, Q, V[(1 << 18)], W[(1 << 18)], x[100005], L[100005], hasquery[(1 << 18)];
ll ans[100005];
vector<pl> queries[(1 << 18)];
vector<pl> depth[(1 << 18)];

void dfs(int x, int d) {
  // cerr << x << ' ' << d << '\\n';
  vector<pl> v;
  // cerr << x << ' ' << x/2 << '\\n';
  if (d == 10 || d == 1) {
    depth[x].push_back(pl(0, 0));
    depth[x].push_back(pl(W[x], V[x]));
  } else {
    for (auto it : depth[x / 2]) {
      if (it.first + W[x] > 100000)
        break;
      v.push_back(pl(it.first + W[x], it.second + V[x]));
    }
    int ptd = 0, ptv = 0;
    ll bval = -1;
    while (ptd < depth[x / 2].size() && ptv < v.size()) {
      if (depth[x / 2][ptd].first < v[ptv].first) {
        if (depth[x / 2][ptd].second > bval) {
          depth[x].push_back(depth[x / 2][ptd]);
          bval = depth[x / 2][ptd].second;
        }
        ptd++;
      } else {
        if (v[ptv].second > bval) {
          depth[x].push_back(v[ptv]);
          bval = v[ptv].second;
        }
        ptv++;
      }
    }
    while (ptd < depth[x / 2].size()) {
      if (depth[x / 2][ptd].second > bval) {
        depth[x].push_back(depth[x / 2][ptd]);
        bval = depth[x / 2][ptd].second;
      }
      ptd++;
    }
    while (ptv < v.size()) {
      if (v[ptv].second > bval) {
        depth[x].push_back(v[ptv]);
        bval = v[ptv].second;
      }
      ptv++;
    }
  }

  /*
  cerr << x << '\\n';
  for (auto it : depth[x]){
          cerr << it.first << ' ';
  }
  cerr << '\\n';
  */
  ll p = x;
  if (d >= 10)
    p >>= (d - 10 + 1);
  else
    p = 0;

  for (auto it : queries[x]) {
    int pt1 = 0, pt2 = depth[p].size() - 1;
    while (pt1 < depth[x].size()) {
      while (pt2 >= 0 && depth[p][pt2].first + depth[x][pt1].first > it.first) {
        pt2--;
      }
      if (pt2 >= 0)
        ans[it.second] =
            max(ans[it.second], depth[x][pt1].second + depth[p][pt2].second);
      pt1++;
    }
  }
  if (2 * x <= N)
    dfs(2 * x, d + 1);
  if (2 * x + 1 <= N)
    dfs(2 * x + 1, d + 1);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  for (int i = 1; i <= N; ++i) {
    cin >> V[i] >> W[i];
  }
  cin >> Q;
  for (int i = 1; i <= Q; ++i) {
    cin >> x[i] >> L[i];
    queries[x[i]].push_back(pl(L[i], i));
    while (x[i] > 0 && !hasquery[x[i]]) {
      hasquery[x[i]] = 1;
      x[i] /= 2;
    }
  }
  for (int i = 1; i <= N; ++i) {
    sort(queries[i].begin(), queries[i].end());
  }
  depth[0].push_back(pl(0, 0));
  dfs(1, 1);
  for (int i = 1; i <= Q; ++i) {
    cout << ans[i] << '\\n';
  }
}