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

/*{{{*/
#include "bits/stdc++.h"
using namespace std;

#define MT make_tuple
#define all(x) begin(x), end(x)
#define putchar(x) cout << (x)
static int fastio = []() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(17);
  return 0;
}();
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <typename... T> string format(const string &fmt, T &&...args) {
  size_t sz = snprintf(nullptr, 0, fmt.c_str(), args...) + 1;
  unique_ptr<char[]> buf(new char[sz]);
  snprintf(buf.get(), sz, fmt.c_str(), args...);
  return string(buf.get(), buf.get() + sz - 1);
}

template <class T> struct rge {
  T b, e;
  auto begin() const { return b; }
  auto end() const { return e; }
};
template <class T> rge<T> range(T i, T j) { return rge<T>{i, j}; }
template <class T> auto dud(T *x) -> decltype(cerr << *x, 0);
template <class T> char dud(...);

struct debug {
#ifdef LOCAL
  debug(int line) {
    if (line)
      cerr << "LINE(" << line << ") -> ";
  }
  template <class T>
  typename enable_if<sizeof dud<T>(0) != 1, debug &>::type operator<<(T i) {
    cerr << boolalpha << i;
    return *this;
  }
  template <class T>
  typename enable_if<sizeof dud<T>(0) == 1, debug &>::type operator<<(T i) {
    return *this << range(begin(i), end(i));
  }
  template <class T, class U> debug &operator<<(pair<T, U> d) {
    return *this << "(" << d.first << ", " << d.second << ")";
  }
  debug &operator<<(tuple<> &) { return *this << "()"; };
  template <class... T> debug &operator<<(tuple<T...> d) {
    *this << "(";
    debug_tuple<sizeof...(T), 0>(d);
    return *this << ")";
  }
  template <size_t L, size_t I, class T> void debug_tuple(const T &t) {
    *this << (I == 0 ? "" : ", ") << get<I>(t);
    if (I + 1 < L)
      debug_tuple<L, (I + 1) % L>(t);
  }
  template <class T> debug &operator<<(rge<T> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
      *this << (it != d.b ? ", " : "") << *it;
    return *this << "]";
  }
  debug &operator<<(ostream &(*pf)(std::ostream &)) {
    cerr << pf;
    return *this;
  }
#else
  debug(int) {}
  template <class T> debug &operator<<(T &&) { return *this; }
  debug &operator<<(ostream &(*)(std::ostream &)) { return *this; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ << ": " << (__VA_ARGS__) << "] "

#define debug0() debug(__LINE__)
#define debug1(x) debug(0)
#define GET_MACRO(_0, _1, NAME, ...) NAME
#define debug(...) GET_MACRO(_0, ##__VA_ARGS__, debug1, debug0)(__VA_ARGS__)
/*}}}*/
using LL = long long;

const LL LLINF = 0x3f3f3f3f3f3f3f3f;
const int S = 9;

int N;
vector<int> V, W;
vector<LL> ans;
vector<vector<pair<int, int>>> queries;
vector<pair<LL, LL>> ps;
LL tv[2][1 << 11] = {}, tw[2][1 << 11] = {};

int lg(int n) { return 31 - __builtin_clz(n); }

void dfs(int v) {
  if (v > N) {
    return;
  }

  int k = lg(v);
  int t = k >= S ? k - S : k;

  int m = min(k + 1, S);
  int n = k + 1 - m;

  debug() << "A" << endl;

  for (int mask = 0; mask < (1 << t); ++mask) {
    tv[k >= S][mask | (1 << t)] = tv[k >= S][mask] + V[v];
    tw[k >= S][mask | (1 << t)] = tw[k >= S][mask] + W[v];
  }

  debug() << "B" << endl;

  if (k < S) {
    ps.clear();
    auto tmp = ps;
    for (int mask = 0; mask < (1 << m); ++mask) {
      tmp.emplace_back(tw[0][mask], -tv[0][mask]);
    }
    sort(all(tmp));
    for (int u = 0; u < int(tmp.size()); ++u) {
      if (u == 0 or tmp[u].second < -ps.back().second) {
        ps.emplace_back(tmp[u].first, -tmp[u].second);
      }
    }
  }

  debug() << "C" << endl;

  for (auto &q : queries[v]) {
    int id, L;
    tie(id, L) = q;

    ans[id] = 0;
    for (int mask = 0; mask < (1 << n); ++mask) {
      auto it = upper_bound(all(ps), make_pair(L - tw[1][mask], LLINF));
      if (it != ps.begin()) {
        ans[id] = max(ans[id], tv[1][mask] + prev(it)->second);
      }
    }
  }

  debug() << "D" << endl;

  dfs(v * 2);
  dfs(v * 2 + 1);
}

void solve(int) {
  cin >> N;
  V.resize(N + 1);
  W.resize(N + 1);
  queries.resize(N + 1);

  for (int i = 1; i <= N; ++i) {
    cin >> V[i] >> W[i];
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; ++i) {
    int v, L;
    cin >> v >> L;
    queries[v].emplace_back(i, L);
  }

  ans.resize(Q);
  dfs(1);

  for (int i = 0; i < Q; ++i) {
    cout << ans[i] << endl;
  }
}

int main() {
  int T = 1;
  // cin >> T;
  for (int i = 1; i <= T; ++i) {
    solve(i);
  }
  return 0;
}


Error: TLE

Fixed submission:

/*{{{*/
#include "bits/stdc++.h"
using namespace std;

#define MT make_tuple
#define all(x) begin(x), end(x)
#define putchar(x) cout << (x)
static int fastio = []() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(17);
  return 0;
}();
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <typename... T> string format(const string &fmt, T &&...args) {
  size_t sz = snprintf(nullptr, 0, fmt.c_str(), args...) + 1;
  unique_ptr<char[]> buf(new char[sz]);
  snprintf(buf.get(), sz, fmt.c_str(), args...);
  return string(buf.get(), buf.get() + sz - 1);
}

template <class T> struct rge {
  T b, e;
  auto begin() const { return b; }
  auto end() const { return e; }
};
template <class T> rge<T> range(T i, T j) { return rge<T>{i, j}; }
template <class T> auto dud(T *x) -> decltype(cerr << *x, 0);
template <class T> char dud(...);

struct debug {
#ifdef LOCAL
  debug(int line) {
    if (line)
      cerr << "LINE(" << line << ") -> ";
  }
  template <class T>
  typename enable_if<sizeof dud<T>(0) != 1, debug &>::type operator<<(T i) {
    cerr << boolalpha << i;
    return *this;
  }
  template <class T>
  typename enable_if<sizeof dud<T>(0) == 1, debug &>::type operator<<(T i) {
    return *this << range(begin(i), end(i));
  }
  template <class T, class U> debug &operator<<(pair<T, U> d) {
    return *this << "(" << d.first << ", " << d.second << ")";
  }
  debug &operator<<(tuple<> &) { return *this << "()"; };
  template <class... T> debug &operator<<(tuple<T...> d) {
    *this << "(";
    debug_tuple<sizeof...(T), 0>(d);
    return *this << ")";
  }
  template <size_t L, size_t I, class T> void debug_tuple(const T &t) {
    *this << (I == 0 ? "" : ", ") << get<I>(t);
    if (I + 1 < L)
      debug_tuple<L, (I + 1) % L>(t);
  }
  template <class T> debug &operator<<(rge<T> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
      *this << (it != d.b ? ", " : "") << *it;
    return *this << "]";
  }
  debug &operator<<(ostream &(*pf)(std::ostream &)) {
    cerr << pf;
    return *this;
  }
#else
  debug(int) {}
  template <class T> debug &operator<<(T &&) { return *this; }
  debug &operator<<(ostream &(*)(std::ostream &)) { return *this; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ << ": " << (__VA_ARGS__) << "] "

#define debug0() debug(__LINE__)
#define debug1(x) debug(0)
#define GET_MACRO(_0, _1, NAME, ...) NAME
#define debug(...) GET_MACRO(_0, ##__VA_ARGS__, debug1, debug0)(__VA_ARGS__)
/*}}}*/
using LL = long long;

const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL LLINF = 0x3f3f3f3f3f3f3f3f;
const int MAX_N = 100005;

int lg(int n) { return 31 - __builtin_clz(n); }

void solve(int) {
  int N;
  cin >> N;
  vector<int> V(N + 1), W(N + 1);

  for (int i = 1; i <= N; ++i) {
    cin >> V[i] >> W[i];
  }

  size_t S = 9;

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; ++i) {
    int v, L;
    cin >> v >> L;
    vector<int> vs;
    while (v >= 1) {
      vs.push_back(v);
      v >>= 1;
    }

    int m = min(S, vs.size());
    vector<pair<LL, LL>> tmp, ps;
    for (int mask = 0; mask < (1 << m); ++mask) {
      LL tw = 0, tv = 0;
      for (int j = 0; j < m; ++j) {
        if (mask >> j & 1) {
          tw += W[vs[j]];
          tv += V[vs[j]];
        }
      }
      tmp.emplace_back(tw, -tv);
    }
    sort(all(tmp));
    for (int u = 0; u < int(tmp.size()); ++u) {
      if (u == 0 or tmp[u].second < -ps.back().second) {
        ps.emplace_back(tmp[u].first, -tmp[u].second);
      }
    }
    // debug() << imie(ps) << endl;

    LL ans = 0;
    int n = vs.size() - m;
    for (int mask = 0; mask < (1 << n); ++mask) {
      LL tw = 0, tv = 0;
      for (int j = 0; j < n; ++j) {
        if (mask >> j & 1) {
          tw += W[vs[j + m]];
          tv += V[vs[j + m]];
        }
      }
      auto it = prev(upper_bound(all(ps), make_pair(L - tw, LLINF)));
      // debug() << imie(tw) imie(tv) imie(*it) << endl;
      ans = max(ans, tv + it->second);
    }
    cout << ans << endl;
  }
}

int main() {
  int T = 1;
  // cin >> T;
  for (int i = 1; i <= T; ++i) {
    solve(i);
  }
  return 0;
}