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

#include <bits/stdc++.h>
#define pb push_back
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define pi pair<int, int>
using namespace std;

const int N = 3e5 + 5;
const int M = 7;

int n, a[N], b[N], k, v[N], need1[N], need3[N], l[N];
vector<pi> q1[N], q3[N];

bool cmp(pi a, pi b) { return a.second < b.second; }

void dfs(int v) {
  if (need1[v]) {
    q1[v].pb({0, 0});
    int vv = v;
    for (int i = 0; i < M + 1 && vv != 0; i++) {
      int sz = q1[v].size();
      for (int j = 0; j < sz; j++)
        q1[v].pb({q1[v][j].first + a[vv], q1[v][j].second + b[vv]});
      vv /= 2;
    }
    sort(q1[v].begin(), q1[v].end(), cmp);
  }
  if (need3[v]) {
    q3[v].pb({0, 0});
    int vv = v;
    for (int i = 0; i < M + 3 && vv != 0; i++) {
      int sz = q3[v].size();
      for (int j = 0; j < sz; j++)
        q3[v].pb({q3[v][j].first + a[vv], q3[v][j].second + b[vv]});
      vv /= 2;
    }
    sort(q3[v].begin(), q3[v].end(), cmp);
  }
  if (v + v <= n)
    dfs(v + v);
  if (v + v + 1 <= n)
    dfs(v + v + 1);
}

signed main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i] >> b[i];
  cin >> k;
  for (int i = 1; i <= k; i++) {
    cin >> v[i] >> l[i];
    need1[v[i]] = 1;
    int q = v[i];
    for (int j = 0; j < M + 1; j++)
      q /= 2;
    need3[q] = 3;
  }
  q1[0].pb({0, 0});
  q3[0].pb({0, 0});

  dfs(1);
  for (int y = 1; y <= k; y++) {
    int u = v[y];
    int v = u;
    for (int i = 0; i < M + 1; i++)
      v /= 2;
    int ans = 0;
    vector<int> mx_pref(q1[u].size(), q1[u][0].first);
    for (int i = 1; i < q1[u].size(); i++)
      mx_pref[i] = max(mx_pref[i - 1], q1[u][i].first);
    int r = q1[u].size() - 1;
    for (int i = 0; i < q3[v].size(); i++) {
      if (q3[v][i].second > l[y])
        break;
      while (q3[v][i].second + q1[u][r].second > l[y])
        r--;
      ans = max(ans, q3[v][i].first + mx_pref[r]);
    }
    cout << ans << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define pi pair<int, int>
using namespace std;

const int N = 3e5 + 5;
const int M = 9;

int n, dep[N], a[N], b[N], k, v[N], need[N], l[N];
vector<pi> q[N];

bool cmp(pi a, pi b) { return a.second < b.second; }

void dfs(int v) {
  dep[v] = dep[v / 2] + 1;
  if (need[v]) {
    q[v].pb({0, 0});
    int vv = v;
    for (int i = 0; i < M && vv != 0; i++) {
      int x = a[vv];
      int sz = q[v].size();
      for (int j = 0; j < sz; j++)
        q[v].pb({q[v][j].first + a[vv], q[v][j].second + b[vv]});
      vv /= 2;
    }
    sort(q[v].begin(), q[v].end(), cmp);
  }
  if (v + v <= n)
    dfs(v + v);
  if (v + v + 1 <= n)
    dfs(v + v + 1);
}

signed main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i] >> b[i];
  cin >> k;
  for (int i = 1; i <= k; i++) {
    cin >> v[i] >> l[i];
    need[v[i]] = 1;
    int q = v[i];
    for (int j = 0; j < M; j++)
      q /= 2;
    need[q] = 1;
  }
  q[0].pb({0, 0});
  dfs(1);
  for (int y = 1; y <= k; y++) {
    int u = v[y];
    int v = u;
    for (int i = 0; i < M; i++)
      v /= 2;
    int ans = 0;
    vector<int> mx_pref(q[u].size(), q[u][0].first);
    for (int i = 1; i < q[u].size(); i++)
      mx_pref[i] = max(mx_pref[i - 1], q[u][i].first);
    int r = q[u].size() - 1;
    for (int i = 0; i < q[v].size(); i++) {
      if (q[v][i].second > l[y])
        continue;
      while (q[v][i].second + q[u][r].second > l[y])
        r--;
      ans = max(ans, q[v][i].first + mx_pref[r]);
    }
    cout << ans << "\\n";
  }
  return 0;
}