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

#include <bits/stdc++.h>
using namespace std;

#define MX (1 << 18)
#define mod 998244353
#define ll long long
#define ld long double
#define mem(a, b) memset(a, b, sizeof(a))
#define fast()                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define F first
#define S second
#define mp make_pair
#define inf 2e18
#define PI acos(-1)

int dx[] = {+1, -1, 0, 0};
int dy[] = {0, 0, +1, -1};

int n, q;
int V[MX], W[MX];
const int BIG = 100005;
ll dp[(1 << 9)][BIG];

int h;

ll query(int v, int L) {
  int lvl = log2(v);
  if (lvl <= h)
    return dp[v][L];
  vector<int> up;
  while (lvl > h) {
    up.push_back(v);
    v /= 2;
    --lvl;
  }
  ll res = 0;
  for (int mask = 0; mask < (1 << up.size()); mask++) {
    ll nW = 0, nV = 0;
    for (int i = 0; i < up.size(); i++) {
      if (mask & (1 << i))
        nV += V[up[i]], nW += W[up[i]];
    }
    if (nW <= L)
      res = max(res, nV + dp[v][L - nW]);
  }
  return res;
}

void makeChild(int now) {
  vector<int> up;
  int par = now;
  while (par) {
    up.push_back(par);
    par /= 2;
  }
  for (int mask = 0; mask < (1 << up.size()); mask++) {
    ll nW = 0, nV = 0;
    for (int i = 0; i < up.size(); i++) {
      if (mask & (1 << i))
        nV += V[up[i]], nW += W[up[i]];
    }
    if (nW < BIG)
      dp[now][nW] = max(dp[now][nW], nV);
  }
  for (int i = 1; i < BIG; i++)
    dp[now][i] = max(dp[now][i], dp[now][i - 1]);
}
void make(int now) {
  int lvl = log2(now);
  if (lvl > h || now > n)
    return;
  makeChild(now);
  make(now * 2);
  make(now * 2 + 1);
}

int main() {
  fast();
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  cin >> n;
  h = log2(n);
  h = h / 2;
  for (int i = 1; i <= n; i++) {
    cin >> V[i] >> W[i];
  }
  make(1);
  cin >> q;
  for (int i = 1; i <= q; i++) {
    int v, L;
    cin >> v >> L;
    cout << query(v, L) << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define MX (1 << 18)
#define mod 998244353
#define ll long long
#define ld long double
#define mem(a, b) memset(a, b, sizeof(a))
#define fast()                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define F first
#define S second
#define mp make_pair
#define inf 2e18
#define PI acos(-1)

int dx[] = {+1, -1, 0, 0};
int dy[] = {0, 0, +1, -1};

int n, q;
int V[MX], W[MX];
vector<pair<int, ll>> st[MX];
vector<ll> vt[MX];
vector<int> wt[MX];
vector<ll> num[MX];
ll ans[MX];

int h;

ll queryForOne(int v, int L) {
  ll res = 0;
  for (auto it = st[v].begin(); it != st[v].end(); it++) {
    if (it->F > L)
      break;
    res = max(it->S, res);
  }
  return res;
}
ll query(int v, int L) {
  int lvl = log2(v);
  ll res = queryForOne(v, L);
  if (lvl <= h)
    return res;

  int par = v;
  while (lvl > h) {
    par = par / 2;
    --lvl;
  }
  res = max(res, queryForOne(par, L));

  int j = vt[v].size() - 1;
  for (int i = 0; i < st[par].size(); ++i) {
    if (st[par][i].F > L)
      break;
    while (j >= 0 && (st[par][i].F + st[v][j].F) > L)
      --j;
    if (j >= 0)
      res = max(res, vt[par][i] + vt[v][j]);
    else
      break;
  }
  return res;
}
void findAnswer(int now) {
  for (int i = 0; i < wt[now].size(); i++)
    ans[num[now][i]] = query(now, wt[now][i]);
}

void merger(vector<pair<int, ll>> &src, vector<pair<int, ll>> &des1,
            vector<pair<int, ll>> &des2) {
  int i = 0, j = 0;
  int big = 0;
  while (i < des1.size() && j < des2.size()) {
    if (des1[i].F <= des2[j].F) {
      if (des1[i].S > big) {
        src.push_back(des1[i]);
        big = des1[i].S;
      }
      ++i;
    } else {
      if (des2[j].S > big) {
        src.push_back(des2[j]);
        big = des2[j].S;
      }
      ++j;
    }
  }
  while (i < des1.size()) {
    if (des1[i].S > big) {
      src.push_back(des1[i]);
      big = des1[i].S;
    }
    ++i;
  }
  while (j < des2.size()) {
    if (des2[j].S > big) {
      src.push_back(des2[j]);
      big = des2[j].S;
    }
    ++j;
  }
}
void makeChild(int now) {
  int par = now / 2;
  vector<pair<int, ll>> child;
  child.push_back(mp(W[now], V[now]));
  ll big = 0;
  for (auto it = st[par].begin(); it != st[par].end(); it++) {
    if ((it->F + W[now]) > 100000)
      break;
    if (it->S + V[now] > big) {
      big = it->S + V[now];
      child.push_back(mp(it->F + W[now], big));
    }
  }
  merger(st[now], child, st[par]);
  big = 0;
  for (auto it = st[now].begin(); it != st[now].end(); it++) {
    big = max(it->S, big);
    vt[now].push_back(big);
  }
  child.clear();
}
void make(int now) {
  int lvl = log2(now);
  if (lvl == h + 1 || lvl == 0) {
    st[now].push_back(mp(W[now], V[now]));
    vt[now].push_back(V[now]);
  }
  if (lvl == h) {
    findAnswer(now);
    return;
  }
  if (now * 2 <= n)
    makeChild(now * 2);
  if (now * 2 < n)
    makeChild(now * 2 + 1);

  findAnswer(now);
  st[now].clear();
  vt[now].clear();
  // cout<<"make "<<now<<endl;
  // for(auto it= st[now].begin();it!=st[now].end();it++) cout<<it->F<<"
  // "<<it->S<<endl; for(int i=0;i<vt[now].size();i++) cout<<vt[now][i]<<" ";
  // cout<<endl<<endl;;
}

int main() {
  // fast();
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  scanf("%d", &n);
  h = log2(n);
  h = h / 2;
  // cout<<"h "<<h<<endl;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &V[i], &W[i]);
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int v, L;
    scanf("%d%d", &v, &L);
    wt[v].push_back(L);
    num[v].push_back(i);
  }
  for (int i = 1; i <= n; i++)
    make(i);
  for (int i = 1; i <= q; i++)
    cout << ans[i] << endl;
  return 0;
}