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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define sq(x) ((x) * (x))
#define err(x) cerr << "[" #x "]  " << (x) << "\\n";
#define errp(x)                                                                \\
  cerr << "[" #x "]  {" << ((x).first) << ", " << ((x).second) << "}\\n";
#define errv(x)                                                                \\
  {                                                                            \\
    cerr << "[" #x "]  [";                                                     \\
    for (const auto &___ : (x))                                                \\
      cerr << ___ << ", ";                                                     \\
    cerr << "]\\n";                                                             \\
  }
#define errvn(x, n)                                                            \\
  {                                                                            \\
    cerr << "[" #x "]  [";                                                     \\
    for (auto ___ = 0; ___ < (n); ++___)                                       \\
      cerr << (x)[___] << ", ";                                                \\
    cerr << "]\\n";                                                             \\
  }
#define errf() cerr << "\\n\\n";

#pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast, unroll-loops", "omit-frame-pointer","inline")
// #pragma GCC option("arch=native","tune=native","no-zero-upper")
// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native,avx2")

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int getrnd(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); }

ll getrndll(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rng); }

template <typename T1, typename T2> inline bool relax(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T1, typename T2> inline bool strain(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

// :::::::::::::::::::::::::::::::

const int N = (1 << 18) + 3, C = 1 << 9, M = 1e5 + 3;

ll a[N], w[N];
ll dp[C][M];

void solve() {
  int n, q;
  cin >> n;

  for (int i = 1; i <= n; ++i)
    cin >> a[i] >> w[i];

  for (int i = 1; i < min(C, n + 1); ++i) {
    for (int j = w[i]; j < M; ++j)
      dp[i][j] = max({dp[i / 2][j], dp[i / 2][j - w[i]] + a[i], dp[i][j - 1]});
    for (int j = 1; j < w[i]; ++j)
      dp[i][j] = max(dp[i][j - 1], dp[i / 2][j]);
  }
  //    for (int i = 0; i < min(C, n + 1); ++i)
  //        for (int j = 0; j < 10; ++j)
  //            cout << dp[i][j] << " \\n"[j == 10- 1];

  cin >> q;
  while (q--) {
    int v, l;
    cin >> v >> l;

    ll res = 0;
    vector<int> scope;
    for (int i = v;; i /= 2) {
      scope.push_back(i);
      if (i < C)
        break;
    }
    assert(!scope.empty());

    int m = (int)scope.size() - 1;
    for (int msk = 0; msk < (1 << m); ++msk) {
      ll ww = 0, p = 0;
      for (int i = 0; i < m; ++i)
        if ((msk >> i) & 1)
          ww += w[scope[i]], p += a[scope[i]];
      if (ww <= l) {
        strain(res, dp[scope[m]][l - ww] + p);
      }
    }
    strain(res, dp[scope[m]][l]);

    cout << res << '\\n';
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
  srand(time(0));

  int t = 1;
  //    cin >> t;
  while (t--)
    solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define sq(x) ((x) * (x))
#define err(x) cerr << "[" #x "]  " << (x) << "\\n";
#define errp(x)                                                                \\
  cerr << "[" #x "]  {" << ((x).first) << ", " << ((x).second) << "}\\n";
#define errv(x)                                                                \\
  {                                                                            \\
    cerr << "[" #x "]  [";                                                     \\
    for (const auto &___ : (x))                                                \\
      cerr << ___ << ", ";                                                     \\
    cerr << "]\\n";                                                             \\
  }
#define errvn(x, n)                                                            \\
  {                                                                            \\
    cerr << "[" #x "]  [";                                                     \\
    for (auto ___ = 0; ___ < (n); ++___)                                       \\
      cerr << (x)[___] << ", ";                                                \\
    cerr << "]\\n";                                                             \\
  }
#define errf() cerr << "\\n\\n";

#pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast, unroll-loops", "omit-frame-pointer","inline")
// #pragma GCC option("arch=native","tune=native","no-zero-upper")
// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native,avx2")

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int getrnd(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); }

ll getrndll(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rng); }

template <typename T1, typename T2> inline bool relax(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T1, typename T2> inline bool strain(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

// :::::::::::::::::::::::::::::::

const int N = (1 << 18) + 3, C = (1 << 9) + 3;

int scope[20], a[N], w[N];
pair<int, ll> p1[C], p2[C];
ll mx[C];

void solve() {
  int n, q;
  //    cin >> n;
  scanf("%d", &n);

  for (int i = 1; i <= n; ++i) {
    //        cin >> a[i] >> w[i];
    scanf("%d%d", a + i, w + i);
  }

  //    cin >> q;
  scanf("%d", &q);
  while (q--) {
    int v, l;
    //        cin >> v >> l;
    scanf("%d%d", &v, &l);

    int m = 0;
    for (int i = v; i >= 1; i /= 2)
      scope[m++] = i;

    int lh = m / 2, rh = m - lh;
    int p1sz = 0, p2sz = 0;
    for (int msk = 0; msk < (1 << lh); ++msk) {
      ll ww = 0, p = 0;
      for (int i = 0; i < lh; ++i) {
        if ((msk >> i) & 1)
          ww += w[scope[i]], p += a[scope[i]];
      }
      if (ww <= l)
        p1[p1sz++] = {ww, p};
    }
    for (int msk = 0; msk < (1 << rh); ++msk) {
      ll ww = 0, p = 0;
      for (int i = 0; i < rh; ++i)
        if ((msk >> i) & 1)
          ww += w[scope[i + lh]], p += a[scope[i + lh]];
      if (ww <= l)
        p2[p2sz++] = {ww, p};
    }

    sort(p1, p1 + p1sz);
    sort(p2, p2 + p2sz);
    reverse(p2, p2 + p2sz);

    mx[0] = p1[0].second;
    for (int i = 1; i < p1sz; ++i)
      mx[i] = max(mx[i - 1], p1[i].second);

    ll res = 0;
    int pos = 0;
    for (int i = 0; i < p2sz; ++i) {
      int curw = p2[i].first;
      ll price = p2[i].second;

      while (pos < p1sz && p1[pos].first <= l - curw)
        ++pos;
      if (pos > 0)
        strain(res, price + mx[pos - 1]);
    }
    //        cout << res << '\\n';
    printf("%lld\\n", res);
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
  srand(time(0));

  int t = 1;
  //    cin >> t;
  while (t--)
    solve();

  return 0;
}