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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) int(a.size())
#define ls (x << 1)
#define rs (x << 1 | 1)
const int N = 3e5 + 10;
const int lim = 1023;

int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}

int n, v[N], w[N], sv, sw, q;
vector<pii> all[N], oth;

void dfs(int x) {
  if (x > lim || x > n)
    return;
  all[x].pb(mp(sv, sw));
  dfs(ls);
  dfs(rs);
  sv += v[x];
  sw += w[x];
  all[x].pb(mp(sv, sw));
  dfs(ls);
  dfs(rs);
  sv -= v[x];
  sw -= w[x];
}

vector<pii> build(vector<pii> vec) {
  sort(vec.begin(), vec.end());
  int mx = 0;
  vector<pii> ret;
  for (auto t : vec) {
    if (t.se > mx)
      mx = t.se, ret.pb(t);
  }
  return ret;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    w[i] = gi(), v[i] = gi();
  dfs(1);
  for (int i = 1; i <= lim && i <= n; i++)
    all[i] = build(all[i]);
  cin >> q;
  for (int i = 1; i <= q; i++) {
    int x = gi(), l = gi(), y = x;
    oth.clear();
    while (y > lim) {
      oth.pb(mp(v[y], w[y]));
      y >>= 1;
    }
    int ans = 0;
    for (int s = 0; s < (1 << sz(oth)); s++) {
      sv = 0;
      sw = 0;
      for (int j = 0; j < sz(oth); j++)
        if (s >> j & 1)
          sv += oth[j].fi, sw += oth[j].se;
      if (sv > l)
        continue;
      auto it =
          lower_bound(all[y].begin(), all[y].end(), mp(l - sv, 1234567890));
      if (it == all[y].begin())
        ans = max(ans, sw);
      else
        --it, ans = max(ans, sw + it->se);
    }
    cout << ans << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) int(a.size())
#define ls (x << 1)
#define rs (x << 1 | 1)
const int N = 3e5 + 10;

int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}

int n, v[N], w[N], sv, sw, q;
vector<pii> all[N], oth;

void dfs(int x) {
  if (x > 1023)
    return;
  all[x].pb(mp(sv, sw));
  dfs(ls);
  dfs(rs);
  sv += v[x];
  sw += w[x];
  all[x].pb(mp(sv, sw));
  dfs(ls);
  dfs(rs);
  sv -= v[x];
  sw -= w[x];
}

vector<pii> build(vector<pii> vec) {
  sort(vec.begin(), vec.end());
  int mx = 0;
  vector<pii> ret;
  for (auto t : vec) {
    if (t.se > mx)
      mx = t.se, ret.pb(t);
  }
  return ret;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    w[i] = gi(), v[i] = gi();
  dfs(1);
  for (int i = 1; i <= 1023 && i <= n; i++)
    all[i] = build(all[i]);
  cin >> q;
  for (int i = 1; i <= q; i++) {
    int x = gi(), l = gi(), y = x;
    while (y > 1023) {
      oth.pb(mp(v[y], w[y]));
      y >>= 1;
    }
    int ans = 0;
    for (int s = 0; s < (1 << sz(oth)); s++) {
      sv = 0;
      sw = 0;
      for (int j = 0; j < sz(oth); j++)
        if (s >> j & 1)
          sv += oth[j].fi, sw += oth[j].se;
      auto it =
          lower_bound(all[y].begin(), all[y].end(), mp(l - sv, 1234567890));
      if (it == all[y].begin())
        continue;
      --it;
      ans = max(ans, sw + it->se);
    }
    cout << ans << '\\n';
  }
  return 0;
}