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

// khodaya khodet komak kon
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 300000 + 10;
const ll MOD = 1000000000 + 7;
const ll INF = 1000000010;
const ll LOG = 18;

int n, q, W[N], V[N], SM[N], SM2[N], ADD[N], mark[N], dp[100010], ans[100010];
vector<pair<pii, int>> Q[N];
vi pth;

void DFS(int v) {
  if (v > n)
    return;
  pth.pb(v);
  if (v <= 1024) {
    memset(dp, 0, sizeof dp);
    for (int mask = 1; mask < (1 << (pth.size())); mask++) {
      SM[mask] = (SM[mask ^ (mask & (-mask))] + V[pth[ADD[mask & (-mask)]]]);
      SM2[mask] = (SM2[mask ^ (mask & (-mask))] + W[pth[ADD[mask & (-mask)]]]);
      if (SM2[mask] < 100010)
        dp[SM2[mask]] = max(dp[SM2[mask]], SM[mask]);
    }
    for (int i = 1; i < 100010; i++)
      dp[i] = max(dp[i], dp[i - 1]);
    for (auto u : Q[v]) {
      ans[u.S] = max(ans[u.S], dp[u.F.F] + u.F.S);
    }
  }
  DFS(v * 2);
  DFS(v * 2 + 1);
  pth.pop_back();
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  scanf("%d", &n);
  int z = 0, p = 1;
  for (int i = 0; i < 18; i++)
    ADD[p] = z, z++, p *= 2;
  //	cout << "YES " << n << endl;
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", V + i, W + i);
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int v, L;
    scanf("%d %d", &v, &L);
    int vv = v;
    vi F;
    while (vv > 1024) {
      F.pb(vv);
      vv /= 2;
    }
    for (int mask = 1; mask < (1 << (F.size())); mask++) {
      SM[mask] = (SM[mask ^ (mask & (-mask))] + V[F[ADD[mask & (-mask)]]]);
      SM2[mask] = (SM2[mask ^ (mask & (-mask))] + W[F[ADD[mask & (-mask)]]]);
      if (SM2[mask] <= L)
        Q[vv].pb({{L - SM2[mask], SM[mask]}, i});
    }
    Q[vv].pb({{L, 0}, i});
  }
  DFS(1);
  for (int i = 1; i <= q; i++)
    printf("%d\\n", ans[i]);

  return 0;
}


Error: TLE

Fixed submission:

// khodaya khodet komak kon
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 300000 + 10;
const ll MOD = 1000000000 + 7;
const ll INF = 1000000010;
const ll LOG = 18;

int n, q, W[N], V[N], SM[600], SM2[600], ADD[N], mark[N];
pii num[600], num2[600];

inline int solve(int v, int L) {
  int res = 0;
  vector<int> F, S;
  int cnt = 0;
  int vv = v;
  while (vv != 1) {
    cnt++;
    vv /= 2;
  }
  cnt++;
  int cnt2 = 0;
  while (v != 0) {
    cnt2++;
    if (cnt2 <= cnt / 2)
      F.pb(v);
    else
      S.pb(v);
    v /= 2;
  }
  int P = 0, P2 = 0;
  SM[0] = SM2[0] = 0;
  num[++P] = {0, 0};
  num2[++P2] = {0, 0};
  for (int mask = 1; mask < (1 << (F.size())); mask++) {
    SM[mask] = (SM[mask ^ (mask & (-mask))] + V[F[ADD[mask & (-mask)]]]);
    SM2[mask] = (SM2[mask ^ (mask & (-mask))] + W[F[ADD[mask & (-mask)]]]);
    if (SM2[mask] <= L && mark[SM2[mask]] < SM[mask])
      num[++P] = {SM2[mask], SM[mask]}, mark[SM2[mask]] = SM[mask];
  }
  for (int i = 1; i <= P; i++)
    mark[num[i].F] = 0;
  SM2[0] = SM[0] = 0;
  for (int mask = 1; mask < (1 << (S.size())); mask++) {
    SM[mask] = (SM[mask ^ (mask & (-mask))] + V[S[ADD[mask & (-mask)]]]);
    SM2[mask] = (SM2[mask ^ (mask & (-mask))] + W[S[ADD[mask & (-mask)]]]);
    if (SM2[mask] <= L && mark[SM2[mask]] < SM[mask])
      num2[++P2] = {SM2[mask], SM[mask]}, mark[SM2[mask]] = SM[mask];
  }
  for (int i = 1; i <= P2; i++)
    mark[num2[i].F] = 0;
  sort(num + 1, num + P + 1);
  sort(num2 + 1, num2 + P2 + 1);
  //	num.resize(unique(all(num)) - num.begin());
  //	num2.resize(unique(all(num2)) - num2.begin());
  int mx = 0, pnt = 1;
  for (int i = P; i >= 1; i--) {
    while (pnt != P2 && num2[pnt + 1].F + num[i].F <= L)
      pnt++, mx = max(mx, num2[pnt].S);
    res = max(res, num[i].S + mx);
  }
  return res;
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  scanf("%d", &n);
  int z = 0, p = 1;
  for (int i = 0; i < 18; i++)
    ADD[p] = z, z++, p *= 2;
  //	cout << "YES " << n << endl;
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", V + i, W + i);
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int v, L;
    scanf("%d %d", &v, &L);
    printf("%d\\n", solve(v, L));
  }

  return 0;
}