Problem p02648 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
int n, pa[333333], v[333333], w[333333], ans[333333], x, y, cur[333333];
int cv[33], cw[33], cnt, sumv[11111], sumw[11111], fun[333333], q;
bool con[333333];
vector<pair<int, int>> que[333333];
void proq(int x) {
  int xx = x;
  if (que[x].empty())
    return;
  cnt = 0;
  bool flg = 0;
  while (x) {
    if (con[x]) {
      flg = 1;
      break;
    }
    cv[cnt] = v[x];
    cw[cnt] = w[x];
    cnt++;
    x = pa[x];
  }
  x = xx;
  int id, ww, mx;
  for (int i = 1; i < (1 << cnt); i++) {
    int cp = fun[i & (-i)], msk = i ^ (i & (-i));
    sumv[i] = sumv[msk] + cv[cp];
    sumw[i] = sumw[msk] + cw[cp];
  }
  if (!flg) {
    for (int i = 0; i < que[x].size(); i++) {
      ww = que[x][i].first;
      id = que[x][i].second;
      mx = 0;
      for (int j = 0; j < (1 << cnt); j++) {
        if (sumw[j] <= ww)
          mx = max(mx, sumv[j]);
      }
      ans[id] = mx;
    }
  } else {
    for (int i = 0; i < que[x].size(); i++) {
      ww = que[x][i].first;
      id = que[x][i].second;
      mx = 0;
      for (int j = 0; j < (1 << cnt); j++) {
        if (sumw[j] <= ww)
          mx = max(mx, sumv[j] + cur[ww - sumw[j]]);
      }
      ans[id] = mx;
    }
  }
}
void upd(int x) {
  con[x] = 1;
  cnt = 0;
  while (x) {
    cv[cnt] = v[x];
    cw[cnt] = w[x];
    cnt++;
    x = pa[x];
  }
  memset(cur, 0, sizeof(cur));
  for (int i = 1; i < (1 << cnt); i++) {
    int cp = fun[i & (-i)], msk = i ^ (i & (-i));
    sumv[i] = sumv[msk] + cv[cp];
    sumw[i] = sumw[msk] + cw[cp];
  }
  for (int i = 0; i < (1 << cnt); i++) {
    if (sumw[i] <= maxn)
      cur[sumw[i]] = max(cur[sumw[i]], sumv[i]);
  }
  for (int i = 1; i <= maxn; i++)
    cur[i] = max(cur[i], cur[i - 1]);
}
void dfs(int i, int dep) {
  proq(i);
  if (dep == 2)
    upd(i);
  if (i * 2 <= n)
    dfs(i * 2, dep + 1);
  if (i * 2 + 1 <= n)
    dfs(i * 2 + 1, dep + 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    pa[i] = i >> 1;
  for (int i = 0; i <= 18; i++)
    fun[1 << i] = i;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &v[i], &w[i]);
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &x, &y);
    que[x].push_back(make_pair(y, i));
  }
  dfs(1, 1);
  for (int i = 1; i <= q; i++)
    printf("%d\n", ans[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
int n, pa[333333], v[333333], w[333333], ans[333333], x, y, cur[333333];
int cv[33], cw[33], cnt, sumv[11111], sumw[11111], fun[333333], q;
bool con[333333];
vector<pair<int, int>> que[333333];
void proq(int x) {
  int xx = x;
  if (que[x].empty())
    return;
  cnt = 0;
  bool flg = 0;
  while (x) {
    if (con[x]) {
      flg = 1;
      break;
    }
    cv[cnt] = v[x];
    cw[cnt] = w[x];
    cnt++;
    x = pa[x];
  }
  x = xx;
  int id, ww, mx;
  for (int i = 1; i < (1 << cnt); i++) {
    int cp = fun[i & (-i)], msk = i ^ (i & (-i));
    sumv[i] = sumv[msk] + cv[cp];
    sumw[i] = sumw[msk] + cw[cp];
  }
  if (!flg) {
    for (int i = 0; i < que[x].size(); i++) {
      ww = que[x][i].first;
      id = que[x][i].second;
      mx = 0;
      for (int j = 0; j < (1 << cnt); j++) {
        if (sumw[j] <= ww)
          mx = max(mx, sumv[j]);
      }
      ans[id] = mx;
    }
  } else {
    for (int i = 0; i < que[x].size(); i++) {
      ww = que[x][i].first;
      id = que[x][i].second;
      mx = 0;
      for (int j = 0; j < (1 << cnt); j++) {
        if (sumw[j] <= ww)
          mx = max(mx, sumv[j] + cur[ww - sumw[j]]);
      }
      ans[id] = mx;
    }
  }
}
void upd(int x) {
  con[x] = 1;
  cnt = 0;
  while (x) {
    cv[cnt] = v[x];
    cw[cnt] = w[x];
    cnt++;
    x = pa[x];
  }
  memset(cur, 0, sizeof(cur));
  for (int i = 1; i < (1 << cnt); i++) {
    int cp = fun[i & (-i)], msk = i ^ (i & (-i));
    sumv[i] = sumv[msk] + cv[cp];
    sumw[i] = sumw[msk] + cw[cp];
  }
  for (int i = 0; i < (1 << cnt); i++) {
    if (sumw[i] <= maxn)
      cur[sumw[i]] = max(cur[sumw[i]], sumv[i]);
  }
  for (int i = 1; i <= maxn; i++)
    cur[i] = max(cur[i], cur[i - 1]);
}
void dfs(int i, int dep) {
  proq(i);
  if (dep == 10)
    upd(i);
  if (i * 2 <= n)
    dfs(i * 2, dep + 1);
  if (i * 2 + 1 <= n)
    dfs(i * 2 + 1, dep + 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    pa[i] = i >> 1;
  for (int i = 0; i <= 18; i++)
    fun[1 << i] = i;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &v[i], &w[i]);
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &x, &y);
    que[x].push_back(make_pair(y, i));
  }
  dfs(1, 1);
  for (int i = 1; i <= q; i++)
    printf("%d\n", ans[i]);
  return 0;
}