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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx")

using namespace std;

#define REP(i, N) for (int i = 0; i < (int)N; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
#define ALL(x) (x).begin(), (x).end()
#define INF (1 << 30)
#define LLINF (1LL << 62)
#define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__)

constexpr int MOD = 1000000007;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

inline int popcount(ll x) { return __builtin_popcountll(x); }
inline int div2num(ll x) { return __builtin_ctzll(x); }
inline bool bit(ll x, int b) { return (x >> b) & 1; }

constexpr int SZ = 100000;
constexpr int prs = 10;
ll pre[1 << prs][SZ + 1];

int main() {
  int N;
  cin >> N;
  vector<int> V(N + 1), W(N + 1);
  REP(i, N) cin >> V[i + 1] >> W[i + 1];
  FOR(i, 1, min(N + 1, 1 << prs)) {
    if (i != 1)
      copy(pre[i / 2], pre[i / 2] + SZ + 1, pre[i]);
    for (int j = SZ; j >= W[i]; --j)
      pre[i][j] = max(pre[i][j], pre[i][j - W[i]] + V[i]);
    REP(j, SZ) pre[i][j + 1] = max(pre[i][j + 1], pre[i][j]);
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int v, L;
    cin >> v >> L;
    if (v < (1 << prs)) {
      cout << pre[v][L] << "\\n";
    } else {
      int bi = 32 - __builtin_clz(v) - prs;
      ll ans = 0;
      REP(b, 1 << bi) {
        int w = 0;
        ll va = 0;
        REP(i, bi) {
          if (bit(b, i)) {
            w += W[v >> i];
            va += V[v >> i];
          }
          if (w > L)
            break;
        }
        if (w <= L)
          ans = max(ans, pre[v >> bi][L - w] + va);
      }
      cout << ans << "\\n";
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx")

using namespace std;

#define REP(i, N) for (int i = 0; i < (int)N; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
using ll = long long;

constexpr int SZ = 100000;
constexpr int prs = 10;
ll pre[1 << prs][SZ + 1];

int main() {
  int N;
  cin >> N;
  vector<int> V(N + 1), W(N + 1);
  REP(i, N) cin >> V[i + 1] >> W[i + 1];
  FOR(i, 1, min(N + 1, 1 << prs)) {
    if (i != 1)
      copy(pre[i / 2], pre[i / 2] + SZ + 1, pre[i]);
    for (int j = SZ; j >= W[i]; --j)
      pre[i][j] = max(pre[i][j], pre[i][j - W[i]] + V[i]);
    REP(j, SZ) pre[i][j + 1] = max(pre[i][j + 1], pre[i][j]);
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int v, L;
    cin >> v >> L;
    if (v < (1 << prs)) {
      cout << pre[v][L] << "\\n";
    } else {
      int lvl = 32 - __builtin_clz(v) - prs;
      ll ans = 0;
      REP(b, 1 << lvl) {
        int w = 0;
        ll va = 0;
        REP(i, lvl) {
          if ((b >> i) & 1) {
            w += W[v >> i];
            va += V[v >> i];
          }
          if (w > L)
            break;
        }
        if (w <= L)
          ans = max(ans, pre[v >> lvl][L - w] + va);
      }
      cout << ans << "\\n";
    }
  }
  return 0;
}