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

#include <bits/stdc++.h>

#include <execinfo.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.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 bound = 2048;
void getS(const VII &s, VII &vec) {
  int msk = 1 << sz(s);
  VII v2(msk);
  if (sz(s) == 0) {
    vec.eb(0, 0);
  }
  rep(i, sz(s)) {
    v2[1 << i].ff = s[i].ss; // weight
    v2[1 << i].ss = s[i].ff; // val
  }
  for (int i = 1; i < msk; i++) {
    int lb = i & (-i);
    v2[i].ff = v2[lb].ff + v2[i ^ lb].ff;
    v2[i].ss = v2[lb].ss + v2[i ^ lb].ss;
    /*
    int val = 0, wei = 0;
    int j = i;
    while (j > 0) {
        int b = __builtin_ffs(j) - 1;
        // dbg(i, j, b);
        val += s[b].ff;
        wei += s[b].ss;
        v2.eb(wei, val);
        j -= 1<<b;
    }
    */
  }
  dbg(s);
  dbg(v2);
  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(vec);
};

void handler(int sig) {
  void *array[10];
  size_t size;

  // get void*'s for all entries on the stack
  size = backtrace(array, 10);

  // print out all the frames to stderr
  fprintf(stderr, "Error: signal %d:\\n", sig);
  backtrace_symbols_fd(array, size, STDERR_FILENO);

  exit(1);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  signal(SIGSEGV, handler);
  signal(SIGABRT, handler);
  int n, Q;
  while (cin >> n) {
    VI v(n + 1), w(n + 1);
    rep(i, n) {
      cin >> v[i + 1] >> w[i + 1];
      // v[i+1] = rand() + 1;
      // w[i+1] = rand() + 1;
    }
    cin >> Q;
    VI q(Q), L(Q), ans(Q);
    rep(i, Q) {
      // q[i]  = rand() % n + 1;
      // L[i] = rand() + 1;
      cin >> q[i] >> L[i];
    }
    map<int, VII> mp;
    VII s, v2;
    rep(c, Q) {
      int x = q[c];
      int kx = x;
      while (kx >= bound)
        kx >>= 1;
      if (!mp.count(kx)) {
        int tx = kx;
        s.clear();
        while (tx >= 1) {
          s.eb(v[tx], w[tx]);
          tx >>= 1;
        }
        // dbg(x, s);
        getS(s, mp[kx]);
      }
      s.clear();
      auto &vec = mp[kx];
      int k = x;
      while (k >= bound) {
        s.eb(v[k], w[k]);
        k >>= 1;
      }
      v2.clear();
      getS(s, v2);
      int i = -1;
      int ans = 0;
      dbg(v2);
      dbg(vec);
      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 << '\\n';
    }
  }
  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 bound = 2048;
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];
      // v[i+1] = rand() + 1;
      // w[i+1] = rand() + 1;
    }
    cin >> Q;
    VI q(Q), L(Q), ans(Q);
    rep(i, Q) {
      // q[i]  = rand() % n + 1;
      // L[i] = rand() + 1;
      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;
          int j = i;
          while (j > 0) {
            int b = __builtin_ffs(j) - 1;
            // dbg(i, j, b);
            val += s[b].ff;
            wei += s[b].ss;
            v2.eb(wei, val);
            j -= 1 << b;
          }
        }
        v2.eb(0, 0);
        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 kx = x;
      while (kx >= bound)
        kx >>= 1;
      if (!mp.count(kx)) {
        int tx = kx;
        while (tx >= 1) {
          s.eb(v[tx], w[tx]);
          tx >>= 1;
        }
        // dbg(x, s);
        mp[kx] = getS();
      }
      auto &vec = mp[kx];
      s.clear();
      int k = x;
      while (k >= bound) {
        s.eb(v[k], w[k]);
        k >>= 1;
      }
      auto v2 = getS();
      int i = -1;
      int ans = 0;
      dbg(s, sz(v2));
      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 << '\\n';
    }
  }
  return 0;
}