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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define all(x) (x).begin(), (x).end()

#ifdef LOCAL
#define dbg(x) cerr << #x " = " << x << endl;
#define idbg(x) #x " = " << x
#include "pretty_print.h"
#else
#define dbg(x)
#define idbg(x) ""
#endif

#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

template <typename T> T sqr(T x) { return x * x; }
template <typename T> T abs(T x) { return x < 0 ? -x : x; }
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> bool chmin(T &x, const T &y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <typename T> bool chmax(T &x, const T &y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

const int D = 12;
int n;
vector<int> v;
vector<int> w;
vector<vector<pair<int, int>>> cs;

void rec(int x, int d) {
  if (x > n) {
    return;
  }
  vector<pair<int, int>> &b = cs[x / 2];
  int m = b.size();
  int wx = w[x - 1];
  int vx = v[x - 1];
  for (int i = 0, j = 0; i < m || j < m;) {
    if (i < m && (j == m || b[i].first < b[j].first + wx)) {
      cs[x].push_back(b[i]);
      ++i;
    } else {
      cs[x].push_back({b[j].first + wx, b[j].second + vx});
      ++j;
    }
  }

  for (int i = 1; i < (int)cs[x].size(); ++i) {
    chmax(cs[x][i].second, cs[x][i - 1].second);
  }

  if (d < D) {
    rec(2 * x + 0, d + 1);
    rec(2 * x + 1, d + 1);
  }
}

int main(int /* argc */, char ** /* argv */) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef LOCAL
  assert(freopen("inp", "r", stdin));
  assert(freopen("out", "w", stdout));
#endif

  while (cin >> n && n) {
    v = vector<int>(n);
    w = vector<int>(n);
    for (int i = 0; i < n; ++i) {
      cin >> v[i] >> w[i];
      // v[i] = rand() % 10000;
      // w[i] = rand() % 10000;
    }
    cs = vector<vector<pair<int, int>>>((1 << D) + 1);
    cs[0].push_back(make_pair(0, 0));
    rec(1, 1);

    int q;
    cin >> q;
    // q = 1e+5;
    while (q--) {
      int x, l;
      cin >> x >> l;
      // x = rand() % (n / 2) + (n / 2);
      // l = rand() % 100000 + 1;
      vector<int> a;
      while (x) {
        a.push_back(x - 1);
        x >>= 1;
      }
      reverse(all(a));

      auto get = [&](int l, int r) {
        vector<pair<int, int>> cs;
        int k = r - l;
        for (int msk = 0; msk < (1 << k); ++msk) {
          int sv = 0;
          int sw = 0;
          for (int i = 0; i < k; ++i) {
            if (msk & (1 << i)) {
              sv += v[a[i + l]];
              sw += w[a[i + l]];
            }
          }
          cs.push_back({sw, sv});
        }
        return cs;
      };

      int m = a.size();
      int k = min(D, m);
      auto &b = cs[a[k - 1] + 1];

      int ans = 0;
      auto c = get(k, m);
      for (auto [sw, sv] : c) {
        int idx = upper_bound(all(b), make_pair(l - sw + 1, -1)) - b.begin();
        if (idx) {
          chmax(ans, b[idx - 1].second + sv);
        }
      }
      cout << ans << "\\n";
    }
  }

  cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec"
       << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define all(x) (x).begin(), (x).end()

#ifdef LOCAL
#include "pretty_print.h"
#else
#endif

#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

template <typename T> T sqr(T x) { return x * x; }
template <typename T> T abs(T x) { return x < 0 ? -x : x; }
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> bool chmin(T &x, const T &y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <typename T> bool chmax(T &x, const T &y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

int main(int /* argc */, char ** /* argv */) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef LOCAL
  assert(freopen("inp", "r", stdin));
  assert(freopen("out", "w", stdout));
#endif

  int n;
  while (cin >> n) {
    vector<int> v(n);
    vector<int> w(n);
    for (int i = 0; i < n; ++i) {
      cin >> v[i] >> w[i];
    }
    int q;
    cin >> q;
    while (q--) {
      int x, l;
      cin >> x >> l;
      vector<int> a;
      while (x) {
        a.push_back(x - 1);
        x >>= 1;
      }

      int m = a.size();
      int k = min((m + 2) / 2, m);
      vector<pair<ll, ll>> cs;
      for (int msk = 0; msk < (1 << k); ++msk) {
        ll sv = 0;
        ll sw = 0;
        for (int i = 0; i < k; ++i) {
          if (msk & (1 << i)) {
            sv += v[a[i]];
            sw += w[a[i]];
          }
        }
        cs.push_back({sw, sv});
      }
      sort(all(cs));
      for (int i = 1; i < (int)cs.size(); ++i) {
        chmax(cs[i].second, cs[i - 1].second);
      }

      ll ans = 0;
      for (int msk = 0; msk < (1 << (m - k)); ++msk) {
        ll sv = 0;
        ll sw = 0;
        for (int i = 0; i < (m - k); ++i) {
          if (msk & (1 << i)) {
            sv += v[a[i + k]];
            sw += w[a[i + k]];
          }
        }
        int idx =
            upper_bound(all(cs), make_pair(l - sw + 1, -1LL)) - cs.begin();
        if (idx) {
          chmax(ans, cs[idx - 1].second + sv);
        }
      }
      cout << ans << "\\n";
    }
  }

  cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec"
       << endl;
  return 0;
}