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

#include <bits/stdc++.h>

// headers {{{
using namespace std;
// using namespace rel_ops;

const double pi = acos(-1.0);
const double eps = 1e-11;
const int INF = 0x7FFFFFFF;
template <class T> inline bool checkmin(T &a, T b) {
  return b < a ? a = b, 1 : 0;
}
template <class T> inline bool checkmax(T &a, T b) {
  return b > a ? a = b, 1 : 0;
}
template <class T> inline T sqr(T x) { return x * x; }
template <class T> inline bool bit(T a, int b) { return (a >> b) & 1; }
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(long long n) { return __builtin_popcountll(n); }
typedef long long int64;
typedef unsigned long long uint64;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VII;
#define MP(A, B) make_pair(A, B)
#define PB(X) push_back(X)
#define eb(args...) emplace_back(args)
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ff first
#define ss second
#define sz(g) int(g.size())
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '{';
  string sep;
  for (const auto &x : v)
    os << sep << x, sep = ", ";
  return os << '}';
}
#ifdef LOCAL
#define dout cerr
void out() { dout << endl; }
template <typename A, typename... B> void out(A a, B... b) {
  dout << ' ' << a;
  out(b...);
}
#define dbg(args...) dout << __LINE__ << ":(" << #args << ") -> ", out(args)
template <class T> void show(T a, int n) {
  for (int i = 0; i < n; ++i)
    cout << a[i] << ' ';
  cout << endl;
}
template <class T> void show(T a, int r, int l) {
  for (int i = 0; i < r; ++i)
    show(a[i], l);
  cout << endl;
}
constexpr bool local = true;
#else
#define dbg(...)
#define dout                                                                   \\
  if (true)                                                                    \\
    ;                                                                          \\
  else                                                                         \\
    cerr
#define out(...)
#define show(...)
constexpr bool local = false;
#endif
// }}}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, Q;
  while (cin >> n) {
    VI v(n + 1), w(n + 1);
    rep(i, n) cin >> v[i + 1] >> w[i + 1];
    cin >> Q;
    VI q(Q), L(Q), ans(Q);
    rep(i, Q) cin >> q[i] >> L[i];
    map<int, VII> mp;
    rep(c, Q) {
      int x = q[c];
      VII s;
      auto getS = [&]() {
        VII v2, vec;
        int msk = 1 << sz(s);
        if (sz(s) == 0) {
          vec.eb(0, 0);
          return vec;
        }
        for (int i = 0; i < msk; i++) {
          int val = 0, wei = 0;
          for (int j = 0; j < sz(s); j++) {
            if (bit(i, j)) {
              val += s[j].ff;
              wei += s[j].ss;
            }
          }
          v2.eb(wei, val);
        }
        sort(v2.begin(), v2.end());
        int premax = -1;
        for (int i = 0; i < sz(v2); i++) {
          while (i + 1 < sz(v2) && v2[i + 1].ff == v2[i].ff)
            i++;
          if (v2[i].ss <= premax)
            continue;
          premax = v2[i].ss;
          vec.PB(v2[i]);
        }
        dbg(s, v2);
        dbg(vec);
        return vec;
      };
      int k = x;
      while (k >= 2048)
        k >>= 1;
      int key = k;
      if (!mp.count(key)) {
        while (k >= 1) {
          s.eb(v[k], w[k]);
          k >>= 1;
        }
        dbg(x, s);
        mp[key] = getS();
      }
      auto &vec = mp[key];
      s.clear();
      k = x;
      while (k >= 2048) {
        s.eb(v[k], w[k]);
        k >>= 1;
      }
      auto v2 = getS();
      int i = -1;
      int ans = 0;
      for (int j = sz(v2) - 1; j >= 0; j--) {
        while (i + 1 < sz(vec) && vec[i + 1].ff + v2[j].ff <= L[c])
          i++;
        if (i >= 0)
          checkmax(ans, v2[j].ss + vec[i].ss);
      }
      cout << ans << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

// headers {{{
using namespace std;
// using namespace rel_ops;

const double pi = acos(-1.0);
const double eps = 1e-11;
const int INF = 0x7FFFFFFF;
template <class T> inline bool checkmin(T &a, T b) {
  return b < a ? a = b, 1 : 0;
}
template <class T> inline bool checkmax(T &a, T b) {
  return b > a ? a = b, 1 : 0;
}
template <class T> inline T sqr(T x) { return x * x; }
template <class T> inline bool bit(T a, int b) { return (a >> b) & 1; }
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(long long n) { return __builtin_popcountll(n); }
typedef long long int64;
typedef unsigned long long uint64;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VII;
#define MP(A, B) make_pair(A, B)
#define PB(X) push_back(X)
#define eb(args...) emplace_back(args)
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ff first
#define ss second
#define sz(g) int(g.size())
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '{';
  string sep;
  for (const auto &x : v)
    os << sep << x, sep = ", ";
  return os << '}';
}
#ifdef LOCAL
#define dout cerr
void out() { dout << endl; }
template <typename A, typename... B> void out(A a, B... b) {
  dout << ' ' << a;
  out(b...);
}
#define dbg(args...) dout << __LINE__ << ":(" << #args << ") -> ", out(args)
template <class T> void show(T a, int n) {
  for (int i = 0; i < n; ++i)
    cout << a[i] << ' ';
  cout << endl;
}
template <class T> void show(T a, int r, int l) {
  for (int i = 0; i < r; ++i)
    show(a[i], l);
  cout << endl;
}
constexpr bool local = true;
#else
#define dbg(...)
#define dout                                                                   \\
  if (true)                                                                    \\
    ;                                                                          \\
  else                                                                         \\
    cerr
#define out(...)
#define show(...)
constexpr bool local = false;
#endif
// }}}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, Q;
  while (cin >> n) {
    VI v(n + 1), w(n + 1);
    rep(i, n) cin >> v[i + 1] >> w[i + 1];
    cin >> Q;
    VI q(Q), L(Q), ans(Q);
    rep(i, Q) cin >> q[i] >> L[i];
    map<int, VII> mp;
    rep(c, Q) {
      int x = q[c];
      VII s;
      auto getS = [&]() {
        VII v2, vec;
        int msk = 1 << sz(s);
        if (sz(s) == 0) {
          vec.eb(0, 0);
          return vec;
        }
        for (int i = 0; i < msk; i++) {
          int val = 0, wei = 0;
          for (int j = 0; j < sz(s); j++) {
            if (bit(i, j)) {
              val += s[j].ff;
              wei += s[j].ss;
            }
          }
          v2.eb(wei, val);
        }
        sort(v2.begin(), v2.end());
        int premax = -1;
        for (int i = 0; i < sz(v2); i++) {
          while (i + 1 < sz(v2) && v2[i + 1].ff == v2[i].ff)
            i++;
          if (v2[i].ss <= premax)
            continue;
          premax = v2[i].ss;
          vec.PB(v2[i]);
        }
        dbg(s, v2);
        dbg(vec);
        return vec;
      };
      int k = x;
      while (k >= 512)
        k >>= 1;
      int key = k;
      if (!mp.count(key)) {
        while (k >= 1) {
          s.eb(v[k], w[k]);
          k >>= 1;
        }
        dbg(x, s);
        mp[key] = getS();
      }
      auto &vec = mp[key];
      s.clear();
      k = x;
      while (k >= 512) {
        s.eb(v[k], w[k]);
        k >>= 1;
      }
      auto v2 = getS();
      int i = -1;
      int ans = 0;
      for (int j = sz(v2) - 1; j >= 0; j--) {
        while (i + 1 < sz(vec) && vec[i + 1].ff + v2[j].ff <= L[c])
          i++;
        if (i >= 0)
          checkmax(ans, v2[j].ss + vec[i].ss);
      }
      cout << ans << endl;
    }
  }
  return 0;
}