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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for (int(i) = (a)-1; (i) >= 0; (i)--)
#define FORR(i, a, b) for (int(i) = (a)-1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template <typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define EB emplace_back
#define GREATER(T) T, VT<T> greater<T>
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;

struct item_type {
  LL V, W;
  item_type() {
    V = 0;
    W = 0;
  }
};
struct query_type {
  int v;
  LL L;
};

int get_parent(const int i) {
  if (i == 0)
    return -1;
  return (i - 1) / 2;
}

const int MAX_BIT = 18;
const int MAX_N = (1 << MAX_BIT) + 10;
const int MAX_W = 1e5 + 10;
int N;
item_type items[MAX_N];
int Q;
query_type queries[MAX_N];
LL dp[1 << (MAX_BIT / 2)][MAX_W];
int get_lim() { return min(N, MAX_BIT / 2); }

LL calc(query_type query) {
  const auto L = get_lim();
  VI cand;
  int point = query.v;
  {
    while (point >= (1 << L)) {
      cand.EB(point);
      point = get_parent(point);
    }
  }

  LL res = dp[point][query.L];
  REP(i, (1 << cand.size())) {
    LL cur_w = 0, cur_v = 0;
    REP(j, cand.size()) {
      if (i & (1 << j)) {
        cur_w += items[cand[j]].W;
        cur_v += items[cand[j]].V;
      }
    }
    const auto nokori = query.L - cur_w;
    if (nokori < 0)
      continue;
    chmax(res, cur_v);
    chmax(res, cur_v + dp[point][nokori]);
  }
  return res;
}

void pre_calc() {
  memset(dp, -1, sizeof(-1));
  const int L = get_lim();
  REP(i, (1 << L)) {
    const int par = get_parent(i);
    if (par == -1) {
      // i == 0
      dp[i][items[i].W] = items[i].V;
      dp[0][0] = 0;
      continue;
    }
    REP(j, MAX_W) if (dp[par][j] != -1) {
      chmax(dp[i][j], dp[par][j]);
      if (j + items[i].W < MAX_W) {
        chmax(dp[i][j + items[i].W], dp[par][j] + items[i].V);
      }
    }
  }
  REP(i, (1 << L)) {
    REP(j, MAX_W - 1) { chmax(dp[i][j + 1], dp[i][j]); }
  }
}

void func() {
  REP(_, Q) {
    const auto query = queries[_];
    const auto ans = calc(query);
    printf("%lld\\n", ans);
  }
}

int main(void) {
  scanf("%d", &N);
  REP(i, N) { scanf("%lld%lld", &items[i].V, &items[i].W); }
  scanf("%d", &Q);
  REP(i, Q) {
    scanf("%d%lld", &queries[i].v, &queries[i].L);
    queries[i].v--;
  }

  pre_calc();
  func();
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for (int(i) = (a)-1; (i) >= 0; (i)--)
#define FORR(i, a, b) for (int(i) = (a)-1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template <typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define EB emplace_back
#define GREATER(T) T, VT<T> greater<T>
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;

struct item_type {
  LL V, W;
  item_type() {
    V = 0;
    W = 0;
  }
};
struct query_type {
  int v;
  LL L;
};

int par(const int i) { return (i - 1) / 2; }

const int MAX = (1 << 18) + 10;
int N;
item_type items[MAX];
int Q;
query_type queries[MAX];
VT<item_type> memo[MAX];
bool memoize[MAX];

VT<item_type> calc2(const VI &cand) {
  if (cand.empty())
    return VT<item_type>();
  if (memoize[cand.back()])
    return memo[cand.back()];
  VT<item_type> vec;
  REP(i, (1 << cand.size())) {
    item_type cur;
    REP(j, cand.size()) {
      if (i & (1 << j)) {
        cur.V += items[cand[j]].V;
        cur.W += items[cand[j]].W;
      }
    }
    vec.EB(cur);
  }
  sort(ALL(vec), [](const auto &l, const auto &r) {
    if (l.W == r.W)
      return l.V > r.V;
    return l.W < r.W;
  });

  LL cur_v = -1;
  VT<item_type> res;
  FORE(e, vec) {
    if (chmax(cur_v, e.V)) {
      res.EB(e);
    }
  }
  memoize[cand.back()] = true;
  return memo[cand.back()] = res;
}

LL calc(const VI &cand, const LL L) {
  VI left, right;
  REP(i, cand.size()) {
    if (cand[i] <= (1 << 9)) {
      left.EB(cand[i]);
    } else {
      right.EB(cand[i]);
    }
  }

  auto l_v = calc2(left);
  while (l_v.size() && l_v.back().W > L) {
    l_v.pop_back();
  }
  auto r_v = calc2(right);
  while (r_v.size() && r_v.back().W > L) {
    r_v.pop_back();
  }

  LL ans = 0;
  FORE(e, l_v) chmax(ans, e.V);
  FORE(e, r_v) chmax(ans, e.V);
  if (r_v.size() == 0) {
    return ans;
  }

  FORE(e, l_v) {
    const auto V = e.V;
    const auto W = e.W;

    if (r_v[0].W + W > L) {
      continue;
    }
    int ng = r_v.size(), ok = 0;
    while (ng - ok > 1) {
      const int mid = (ng + ok) / 2;
      if (r_v[mid].W + W <= L) {
        ok = mid;
      } else {
        ng = mid;
      }
    }
    chmax(ans, V + r_v[ok].V);
  }
  return ans;
}

void naive() {
  REP(_, Q) {
    const auto query = queries[_];
    VI cand;
    {
      auto tmp = query.v;
      do {
        cand.EB(tmp);
        if (tmp == 0)
          tmp = -1;
        else
          tmp = par(tmp);
      } while (tmp >= 0);
      REVERSE(cand);

      const auto ans = calc(cand, query.L);
      printf("%lld\\n", ans);
    }
  }
}

int main(void) {
  scanf("%d", &N);
  REP(i, N) { scanf("%lld%lld", &items[i].V, &items[i].W); }
  scanf("%d", &Q);
  REP(i, Q) {
    scanf("%d%lld", &queries[i].v, &queries[i].L);
    queries[i].v--;
  }

  naive();
}