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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define ALL(obj) (obj).begin(), (obj).end()
#define SPEED                                                                  \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false);

template <class T> using PQ = priority_queue<T>;
template <class T> using PQR = priority_queue<T, vector<T>, greater<T>>;

constexpr long long MOD = (long long)1e9 + 7;
constexpr long long MOD2 = 998244353;
constexpr long long HIGHINF = (long long)1e18;
constexpr long long LOWINF = (long long)1e15;
constexpr long double PI = 3.1415926535897932384626433L;

template <class T> void chmax(T &a, const T b) { a = max(a, b); }
template <class T> void chmin(T &a, const T b) { a = min(a, b); }
void YN(bool flg) { cout << (flg ? "YES" : "NO") << endl; }
void Yn(bool flg) { cout << (flg ? "Yes" : "No") << endl; }
void yn(bool flg) { cout << (flg ? "yes" : "no") << endl; }

int main() {
  SPEED
  int N;
  cin >> N;
  vector<int> V(N + 1), W(N + 1);
  for (int i = 1; i <= N; ++i) {
    cin >> V[i] >> W[i];
  }
  int Q;
  cin >> Q;
  string ans_str = "";

  // O(Q)
  int m = 1 << 11, g = 1 << 7;
  vector<int> wr(g);
  vector<int> vr(g);
  vector<vector<pair<int, int>>> sl(m);
  vector<vector<int>> wl(m);
  for (int n = 1; n < m; ++n) {
    int k = 2 * n;
    vector<int> l;
    while (k /= 2) {
      l.push_back(k);
    }
    int nl = l.size();
    // O(2^9*9)
    for (int i = 0; i < (1 << nl); ++i) {
      int ww = 0, vv = 0;
      for (int j = 0; j < nl; ++j) {
        if (!(i & (1 << j)))
          continue;
        ww += W[l[j]];
        vv += V[l[j]];
      }
      wl[n].push_back(ww);
      sl[n].push_back({ww, vv});
    }

    // O(2^9*9 log 2^9)
    sort(sl[n].begin(), sl[n].begin() + (1 << nl));
    sort(wl[n].begin(), wl[n].begin() + (1 << nl));
    for (int i = 1; i < (1 << nl); ++i) {
      chmax(sl[n][i].second, sl[n][i - 1].second);
    }
  }
  while (Q--) {
    int p;
    int Z;
    cin >> p >> Z;
    vector<int> r;
    int k = 2 * p;
    int l = 0;
    while (k /= 2) {
      if (k < m)
        chmax(l, k);
      else
        r.push_back(k);
    }
    int nr = r.size();
    for (int i = 0; i < g; ++i) {
      wr[i] = vr[i] = 0;
    }
    for (int i = 0; i < (1 << nr); ++i) {
      for (int j = 0; j < nr; ++j) {
        if (!(i & (1 << j)))
          continue;
        wr[i] += W[r[j]];
        vr[i] += V[r[j]];
      }
    }
    int ans = 0;
    for (int i = 0; i < (1 << nr); ++i) {
      int x = wr[i];
      if (x > Z)
        continue;
      int y = Z - x;
      int idx = upper_bound(wl[l].begin(), wl[l].end(), y) - wl[l].begin();
      idx--;
      int a = sl[l][idx].second;
      int b = vr[i];
      chmax(ans, a + b);
    }
    ans_str += to_string(ans) + "\\n";
  }
  cout << ans_str;

  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define ALL(obj) (obj).begin(), (obj).end()
#define SPEED                                                                  \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false);

template <class T> using PQ = priority_queue<T>;
template <class T> using PQR = priority_queue<T, vector<T>, greater<T>>;

constexpr long long MOD = (long long)1e9 + 7;
constexpr long long MOD2 = 998244353;
constexpr long long HIGHINF = (long long)1e18;
constexpr long long LOWINF = (long long)1e15;
constexpr long double PI = 3.1415926535897932384626433L;

template <class T> void chmax(T &a, const T b) { a = max(a, b); }
template <class T> void chmin(T &a, const T b) { a = min(a, b); }
void YN(bool flg) { cout << (flg ? "YES" : "NO") << endl; }
void Yn(bool flg) { cout << (flg ? "Yes" : "No") << endl; }
void yn(bool flg) { cout << (flg ? "yes" : "no") << endl; }

int main() {
  SPEED
  int N;
  cin >> N;
  vector<int> V(N + 1), W(N + 1);
  for (int i = 1; i <= N; ++i) {
    cin >> V[i] >> W[i];
  }
  int Q;
  cin >> Q;
  string ans_str = "";

  // O(Q)
  int m = 1 << 11, g = 1 << 7;
  vector<int> wr(g);
  vector<pair<int, int>> sr(g);
  vector<vector<int>> vl(m);
  vector<vector<int>> wl(m);
  for (int n = 1; n < m; ++n) {
    int k = 2 * n;
    vector<int> l;
    while (k /= 2) {
      l.push_back(k);
    }
    int nl = l.size();
    // O(2^9*9)
    for (int i = 0; i < (1 << nl); ++i) {
      int ww = 0, vv = 0;
      for (int j = 0; j < nl; ++j) {
        if (!(i & (1 << j)))
          continue;
        ww += W[l[j]];
        vv += V[l[j]];
      }
      wl[n].push_back(ww);
      vl[n].push_back(vv);
    }
  }
  while (Q--) {
    int p;
    int Z;
    cin >> p >> Z;
    vector<int> r;
    int k = 2 * p;
    int l = 0;
    while (k /= 2) {
      if (k < m)
        chmax(l, k);
      else
        r.push_back(k);
    }
    int nr = r.size();
    for (int i = 0; i < g; ++i) {
      wr[i] = sr[i].first = sr[i].second = 0;
    }
    // O(2^9*9)
    for (int i = 0; i < (1 << nr); ++i) {
      for (int j = 0; j < nr; ++j) {
        if (!(i & (1 << j)))
          continue;
        wr[i] += W[r[j]];
        sr[i].first += W[r[j]];
        sr[i].second += V[r[j]];
      }
    }
    // O(2^9*9 log 2^9)
    sort(sr.begin(), sr.begin() + (1 << nr));
    sort(wr.begin(), wr.begin() + (1 << nr));
    for (int i = 1; i < (1 << nr); ++i) {
      chmax(sr[i].second, sr[i - 1].second);
    }
    int ans = 0;
    for (int i = 0; i < vl[l].size(); ++i) {
      int x = wl[l][i];
      if (x > Z)
        continue;
      int y = Z - x;
      int idx = upper_bound(wr.begin(), wr.begin() + (1 << nr), y) - wr.begin();
      idx--;
      int a = vl[l][i];
      int b = sr[idx].second;
      chmax(ans, a + b);
    }
    ans_str += to_string(ans) + "\\n";
  }
  cout << ans_str;

  return 0;
}