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

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

#define rep(i, m, n) for (int(i) = (int)(m); i < (int)(n); ++i)
#define rep2(i, m, n) for (int(i) = (int)(n)-1; i >= (int)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define FOR(i, c)                                                              \\
  for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\\n'
using ll = long long;
using ull = unsigned long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
// constexpr long long MOD = (ll) 1e9 + 7;
constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;
typedef vector<ll> Array;
typedef vector<Array> Matrix;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

struct Edge {
  ll to, rev;
  long double cap;
  Edge(ll _to, long double _cap, ll _rev) {
    to = _to;
    cap = _cap;
    rev = _rev;
  }
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;

void add_edge(Graph &G, ll from, ll to, long double cap, bool revFlag,
              long double revCap) {
  G[from].push_back(Edge(to, cap, (ll)G[to].size()));
  if (revFlag)
    G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

int v[303030];
int w[303030];
const int max_l = 100000;

void solve() {
  int n;
  scanf("%d", &n);
  REP(i, n) scanf("%d %d", &v[i], &w[i]);

  vvec<P> dp(1024);
  dp[0].push_back(make_pair(0LL, 0LL));
  dp[0].push_back(make_pair(w[0], v[0]));

  rep(i, 1, min(1024, n)) {
    int p = (i + 1) / 2 - 1;
    dp[i] = dp[p];
    for (auto [pw, pv] : dp[p]) {
      if (pw + w[i] > max_l)
        break;
      dp[i].push_back(make_pair((ll)pw + w[i], pv + v[i]));
    }
    sort(all(dp[i]));
  }

  auto check = [&](ll max_w, int p, vec<int> &o) {
    int n2 = o.size();

    vec<P> wv2;

    for (ll bit = 0; bit < (1LL << n2); bit++) {
      ll ww = 0, vv = 0;
      bool flag = true;
      REP(i, n2) {
        if (bit & (1LL << i)) {
          ww += w[o[i]];
          vv += v[o[i]];
          if (ww > max_w) {
            flag = false;
            break;
          }
        }
      }
      if (flag)
        wv2.push_back(make_pair(ww, vv));
    }
    sort(all(wv2));

    ll ans = 0LL;
    int t = 0;
    ll ma = -INF;

    REP2(i, dp[p].size()) {
      while (t < wv2.size() && dp[p][i].first + wv2[t].first <= max_w) {
        ma = max(ma, wv2[t++].second);
      }
      ans = max(ans, ma + dp[p][i].second);
    }
    printf("%lld\\n", ans);
  };

  int q;
  scanf("%d", &q);
  REP(i, q) {
    int vv, max_w;
    scanf("%d %d", &vv, &max_w);
    if (vv <= 1024) {
      vv--;
      ll ans = 0;
      for (auto j : dp[vv]) {
        if (j.first > max_w)
          break;
        chmax(ans, j.second);
      }
      printf("%lld\\n", ans);
      continue;
    }

    vec<int> o;
    int p = 0;
    while (vv) {
      if (vv <= 1024) {
        p = vv - 1;
        break;
      }
      o.push_back(vv - 1);
      vv /= 2;
    }
    check(max_w, p, o);
  }
}

int main() {

  solve();
  // ll t;cin>>t;REP(i,t) solve();

  return 0;
}


Error: TLE

Fixed submission:

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

#define rep(i, m, n) for (int(i) = (int)(m); i < (int)(n); ++i)
#define rep2(i, m, n) for (int(i) = (int)(n)-1; i >= (int)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define FOR(i, c)                                                              \\
  for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\\n'
using ll = long long;
using ull = unsigned long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
// constexpr long long MOD = (ll) 1e9 + 7;
constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;
typedef vector<ll> Array;
typedef vector<Array> Matrix;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

struct Edge {
  ll to, rev;
  long double cap;
  Edge(ll _to, long double _cap, ll _rev) {
    to = _to;
    cap = _cap;
    rev = _rev;
  }
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;

void add_edge(Graph &G, ll from, ll to, long double cap, bool revFlag,
              long double revCap) {
  G[from].push_back(Edge(to, cap, (ll)G[to].size()));
  if (revFlag)
    G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

int v[303030];
int w[303030];
void solve() {
  int n;
  scanf("%d", &n);
  REP(i, n) scanf("%d %d", &v[i], &w[i]);

  auto check = [&](ll W, vec<ll> o) {
    ll n2 = o.size();
    vector<P> wv1, wv2;
    for (ll bit = 0; bit < (1LL << (n2 / 2)); bit++) {
      ll ww = 0, vv = 0;
      bool flag = true;
      REP(i, n2 / 2) {
        if ((bit & (1LL << i)) != 0) {
          ww += w[o[i]];
          vv += v[o[i]];
          if (ww > W) {
            flag = false;
            break;
          }
        }
      }
      if (flag)
        wv1.push_back(make_pair(ww, vv));
    }

    for (ll bit = 0; bit < (1LL << (n2 - n2 / 2)); bit++) {
      ll ww = 0, vv = 0;
      bool flag = true;
      REP(i, n2 - n2 / 2) {
        if ((bit & (1LL << i)) != 0) {
          ww += w[o[i + n2 / 2]];
          vv += v[o[i + n2 / 2]];
          if (ww > W) {
            flag = false;
            break;
          }
        }
      }
      if (flag)
        wv2.push_back(make_pair(ww, vv));
    }
    sort(all(wv1));
    sort(all(wv2));

    ll ans = 0LL;
    ll t = 0, ma = 0;
    REP2(i, wv1.size()) {
      while (t < wv2.size() && wv1[i].first + wv2[t].first <= W) {
        ma = max(ma, wv2[t++].second);
      }
      ans = max(ans, ma + wv1[i].second);
    }
    printf("%lld\\n", ans);
  };

  int q;
  scanf("%d", &q);
  REP(i, q) {
    int vv, max_w;
    scanf("%d %d", &vv, &max_w);
    vec<ll> o;
    while (vv) {
      o.push_back(vv - 1);
      vv /= 2;
    }
    // cout<<i<<en;
    check(max_w, o);
  }
}

int main() {

  solve();
  // ll t;cin>>t;REP(i,t) solve();

  return 0;
}