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

#include <bits/stdc++.h>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
#define rer(i, a, b) for (int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef complex<double> comp;
void Debug() { cerr << '\\n'; }
template <class FIRST, class... REST> void Debug(FIRST arg, REST... rest) {
  cerr << arg << " ";
  Debug(rest...);
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  if (!v.empty()) {
    rep(i, 0, sz(v) - 1) out << v[i] << ", ";
    out << v.back();
  }
  out << "]";
  return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &v) {
  out << "(" << v.first << ", " << v.second << ")";
  return out;
}
const int MAX_N = 500010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = (1 << 30) - 1;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
mt19937 rng; // use it by rng() % mod, shuffle(all(vec), rng)
///////////////////////////////////////////////////////////////////////////////////////////////////

const int B = (1 << 9) - 1;
int V[MAX_N], W[MAX_N];
int N, Q;
vector<pi> query[MAX_N]; // L, query num
int dp[MAX_N];
int ans[MAX_N];

vector<pi> vw[20];

int dep(int v) {
  int res = 1;
  while (v != 0) {
    res++;
    v = (v - 1) / 2;
  }
  return res;
}

void loop(int v, int d) {
  if (v >= N)
    return;
  if (d < 9) {
    rep(i, 0, 100000 + 1) dp[i] = -inf;
    rep(bit, 0, (1 << (d + 1))) {
      int at = v;
      int val = 0, weight = 0;
      rep(i, 0, (d + 1)) {
        if ((bit & (1 << i))) {
          val += V[at];
          weight += W[at];
        }
        at = (at - 1) / 2;
      }
      if (weight <= 100000)
        MAX(dp[weight], val);
    }
    rep(i, 0, 100000) { MAX(dp[i + 1], dp[i]); }
    // debug(vi(dp, dp + 10));
  } else {
    vw[d] = vw[d - 1];
    rep(i, 0, sz(vw[d - 1])) {
      vw[d].pb(pi(vw[d - 1][i].fst + V[v], vw[d - 1][i].sec + W[v]));
    }
  }
  rep(i, 0, sz(query[v])) {
    int idx = query[v][i].sec;
    int l = query[v][i].fst;
    int res = 0;
    rep(j, 0, sz(vw[d])) {
      // debug(vw[d], query[v][i]);
      if (l - vw[d][j].sec >= 0) {
        MAX(res, vw[d][j].fst + dp[l - vw[d][j].sec]);
      }
    }
    ans[idx] = res;
  }
  loop(v * 2 + 1, d + 1);
  loop(v * 2 + 2, d + 1);
}

void solve() {
  cin >> N;
  rep(i, 0, N) { cin >> V[i] >> W[i]; }
  cin >> Q;
  rep(i, 0, Q) {
    int v, l;
    cin >> v >> l;
    v--;
    query[v].pb(pi(l, i));
  }
  rep(i, 0, 20) { vw[i].pb(pi(0, 0)); }
  loop(0, 0);
  rep(i, 0, Q) { cout << ans[i] << "\\n"; }
}

uint32_t rd() {
  uint32_t res;
#ifdef __MINGW32__
  asm volatile("rdrand %0" : "=a"(res)::"cc");
#else
  res = std::random_device()();
#endif
  return res;
}

int main() {
#ifndef LOCAL
  ios::sync_with_stdio(false);
  cin.tie(0);
#endif
  cout << fixed;
  cout.precision(20);
  cerr << fixed;
  cerr.precision(6);
  rng.seed(rd());
#ifdef LOCAL
  // freopen("in.txt", "wt", stdout); //for tester
  if (!freopen("in.txt", "rt", stdin))
    return 1;
#endif
  solve();
  cerr << "Time: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
#define rer(i, a, b) for (int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef complex<double> comp;
void Debug() { cerr << '\\n'; }
template <class FIRST, class... REST> void Debug(FIRST arg, REST... rest) {
  cerr << arg << " ";
  Debug(rest...);
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  if (!v.empty()) {
    rep(i, 0, sz(v) - 1) out << v[i] << ", ";
    out << v.back();
  }
  out << "]";
  return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &v) {
  out << "(" << v.first << ", " << v.second << ")";
  return out;
}
const int MAX_N = 500010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = (1 << 30) - 1;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
mt19937 rng; // use it by rng() % mod, shuffle(all(vec), rng)
///////////////////////////////////////////////////////////////////////////////////////////////////

const int B = (1 << 9) - 1;
int V[MAX_N], W[MAX_N];
int N, Q;
vector<pi> query[MAX_N]; // L, query num
int dp[MAX_N];
int ans[MAX_N];

vector<pi> vw[20];

int dep(int v) {
  int res = 1;
  while (v != 0) {
    res++;
    v = (v - 1) / 2;
  }
  return res;
}

void loop(int v, int d) {
  if (v >= N)
    return;
  if (v < 9) {
    rep(i, 0, 100000 + 1) dp[i] = -inf;
    rep(bit, 0, (1 << (d + 1))) {
      int at = v;
      int val = 0, weight = 0;
      rep(i, 0, (d + 1)) {
        if ((bit & (1 << i))) {
          val += V[at];
          weight += W[at];
        }
        at = (at - 1) / 2;
      }
      if (weight <= 100000)
        MAX(dp[weight], val);
    }
    rep(i, 0, 100000) { MAX(dp[i + 1], dp[i]); }
    // debug(vi(dp, dp + 10));
  } else {
    vw[d] = vw[d - 1];
    rep(i, 0, sz(vw[d - 1])) {
      vw[d].pb(pi(vw[d - 1][i].fst + V[v], vw[d - 1][i].sec + W[v]));
    }
  }
  rep(i, 0, sz(query[v])) {
    int idx = query[v][i].sec;
    int l = query[v][i].fst;
    int res = 0;
    rep(j, 0, sz(vw[d])) {
      // debug(vw[d], query[v][i]);
      if (l - vw[d][j].sec >= 0) {
        MAX(res, vw[d][j].fst + dp[l - vw[d][j].sec]);
      }
    }
    ans[idx] = res;
  }
  loop(v * 2 + 1, d + 1);
  loop(v * 2 + 2, d + 1);
}

void solve() {
  cin >> N;
  rep(i, 0, N) { cin >> V[i] >> W[i]; }
  cin >> Q;
  rep(i, 0, Q) {
    int v, l;
    cin >> v >> l;
    v--;
    query[v].pb(pi(l, i));
  }
  rep(i, 0, 20) { vw[i].pb(pi(0, 0)); }
  loop(0, 0);
  rep(i, 0, Q) { cout << ans[i] << "\\n"; }
}

uint32_t rd() {
  uint32_t res;
#ifdef __MINGW32__
  asm volatile("rdrand %0" : "=a"(res)::"cc");
#else
  res = std::random_device()();
#endif
  return res;
}

int main() {
#ifndef LOCAL
  ios::sync_with_stdio(false);
  cin.tie(0);
#endif
  cout << fixed;
  cout.precision(20);
  cerr << fixed;
  cerr.precision(6);
  rng.seed(rd());
#ifdef LOCAL
  // freopen("in.txt", "wt", stdout); //for tester
  if (!freopen("in.txt", "rt", stdin))
    return 1;
#endif
  solve();
  cerr << "Time: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
  return 0;
}