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

#pragma GCC optimize("O3")
#pragma GCC target("sse4")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MOD = 1000000007;
const char nl = '\\n';
const int MX = 100001; // check the limits, dummy
const int K = 9;
set<pi> ops[1 << (K + 1)];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int N;
  cin >> N;
  int V[N + 1], W[N + 1];
  F0R(i, N) cin >> V[i + 1] >> W[i + 1];

  FOR(r, 1, min(N + 1, (1 << (K + 1)))) {
    vpi vals;
    int cur = r;
    while (cur > 0) {
      pi X = {W[cur], V[cur]};
      vals.pb(X);
      cur /= 2;
    }
    F0R(m, (1 << sz(vals))) {
      int v = 0, w = 0;
      F0R(i, sz(vals)) {
        if (m & (1 << i)) {
          w += vals[i].f;
          v += vals[i].s;
        }
      }
      //            cout << r << " " << m << " " << v << " " << w << nl;
      auto it = ops[r].ub({w, -1});
      if (it != ops[r].begin()) {
        it--;
        if (it->s >= v) {
          continue;
        }
      }
      it = ops[r].ub({w, -1});
      while (it != ops[r].end() && it->s <= v) {
        ops[r].erase(it);
        it = ops[r].ub({w, -1});
      }
      ops[r].ins({w, v});
    }
  }

  int Q;
  cin >> Q;

  F0R(q, Q) {
    int ve, L;
    cin >> ve >> L;
    vpi vals;
    int cur = ve;
    while (cur >= (1 << (K + 1))) {
      pi X = {W[cur], V[cur]};
      vals.pb(X);
      cur /= 2;
    }
    int ans = 0;
    F0R(m, 1 << sz(vals)) {
      int v = 0, w = 0;
      F0R(i, sz(vals)) {
        if (m & (1 << i)) {
          w += vals[i].f;
          v += vals[i].s;
        }
      }
      auto it = ops[cur].ub({L - w + 1, -1});
      if (it == ops[cur].begin())
        continue;
      it--;
      ans = max(ans, v + it->s);
    }
    cout << ans << nl;
  }

  return 0;
}

// read the question correctly (ll vs int)
// template by bqi343


Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("sse4")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MOD = 1000000007;
const char nl = '\\n';
const int MX = 100001; // check the limits, dummy
const int K = 10;
set<pi> ops[1 << (K + 1)];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int N;
  cin >> N;
  int V[N + 1], W[N + 1];
  F0R(i, N) cin >> V[i + 1] >> W[i + 1];

  FOR(r, 1, min(N + 1, (1 << (K + 1)))) {
    vpi vals;
    int cur = r;
    while (cur > 0) {
      pi X = {W[cur], V[cur]};
      vals.pb(X);
      cur /= 2;
    }
    F0R(m, (1 << sz(vals))) {
      int v = 0, w = 0;
      F0R(i, sz(vals)) {
        if (m & (1 << i)) {
          w += vals[i].f;
          v += vals[i].s;
        }
      }
      //            cout << r << " " << m << " " << v << " " << w << nl;
      auto it = ops[r].ub({w, -1});
      if (it != ops[r].begin()) {
        it--;
        if (it->s >= v) {
          continue;
        }
      }
      it = ops[r].ub({w, -1});
      while (it != ops[r].end() && it->s <= v) {
        ops[r].erase(it);
        it = ops[r].ub({w, -1});
      }
      ops[r].ins({w, v});
    }
  }

  int Q;
  cin >> Q;

  F0R(q, Q) {
    int ve, L;
    cin >> ve >> L;
    vpi vals;
    int cur = ve;
    while (cur >= (1 << (K + 1))) {
      pi X = {W[cur], V[cur]};
      vals.pb(X);
      cur /= 2;
    }
    int ans = 0;
    F0R(m, 1 << sz(vals)) {
      int v = 0, w = 0;
      F0R(i, sz(vals)) {
        if (m & (1 << i)) {
          w += vals[i].f;
          v += vals[i].s;
        }
      }
      auto it = ops[cur].ub({L - w + 1, -1});
      if (it == ops[cur].begin())
        continue;
      it--;
      ans = max(ans, v + it->s);
    }
    cout << ans << nl;
  }

  return 0;
}

// read the question correctly (ll vs int)
// template by bqi343