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> pi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int)x.size()
#define f first
#define s second
const int MAXN = 266100;
const int MAXK = 19;
const int INF = 1e9;
int N, Q, a, b, c, d, e, f;
int H = 9;

vpi V[MAXN];
pi T[MAXN];
vpi queries[MAXN];
int ans[MAXN];
vpi tmp;

inline void fill(int i) {
  int par = i / 2;
  if (i >= (1 << H) && par < (1 << H))
    par = 0;
  // cerr<<"Node "<<i<<" parent "<<par<<'\\n';
  vpi A, B, C;
  pi cw = T[i];
  // cerr<<"I "<<cw.f<<' '<<cw.s<<'\\n';
  for (auto t : V[par]) {
    B.pb(mp(t.f + cw.f, t.s + cw.s));
    A.pb(t.f, t.s);
  }
  reverse(ALL(A));
  reverse(ALL(B));
  while (SZ(A) && SZ(B)) {
    if (A.back().f < B.back().f) {
      C.pb(A.back());
      A.pop_back();
    } else {
      C.pb(B.back());
      B.pop_back();
    }
  }
  while (SZ(A)) {
    C.pb(A.back());
    A.pop_back();
  }
  while (SZ(B)) {
    C.pb(B.back());
    B.pop_back();
  }
  // for (auto i:C){
  // 	cerr<<i.f<<' '<<i.s<<'\\n';
  // }
  for (auto t : C) {
    if (SZ(V[i])) {
      if (V[i].back().f == t.f) {
        V[i].back().s = max(V[i].back().s, t.s);
        continue;
      } else if (V[i].back().s >= t.s) {
        continue;
      }
    }
    V[i].pb(t);
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> N;
  int layers = 0;
  int r = N;
  while (r) {
    r /= 2;
    layers++;
  }
  H = min(H, layers);
  for (int i = 1; i <= N; ++i) {
    cin >> a >> b;
    T[i] = mp(b, a);
  }
  cin >> Q;
  for (int i = 0; i < Q; ++i) {
    ans[i] = -1;
    cin >> a >> b;
    queries[a].pb(b, i);
  }
  V[0].pb(0, 0);
  c = 1;
  for (int i = 1; i <= N; ++i) {
    if (i < (1 << H)) {
      fill(i);
      // for (auto x:V[i])cerr<<x.f<<' '<<x.s<<'\\n';
      while (SZ(queries[i])) {
        int l = queries[i].back().f;
        int t = queries[i].back().s;
        queries[i].pop_back();
        pair<int, int> x = *(--lb(ALL(V[i]), mp(l, INF)));
        ans[t] = x.s;
      }
    } else {
      fill(i);
      int par = i;
      while (par >= (1 << H))
        par /= 2;
      // cerr<<i<<' '<<par<<'\\n';
      for (auto it : queries[i]) {
        int l = it.f;
        int t = it.s;
        int ind = SZ(V[par]) - 1;
        for (int x = 0; x < SZ(V[i]); ++x) {
          while (ind > -1 && V[par][ind].f + V[i][x].f > l) {
            --ind;
          }
          if (ind == -1)
            break;
          ans[t] = max(ans[t], V[par][ind].s + V[i][x].s);
        }
      }
    }
  }

  for (int i = 0; i < Q; ++i) {
    cout << ans[i] << '\\n';
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int)x.size()
#define f first
#define s second
const int MAXN = 266100;
const int MAXK = 19;
const int INF = 1e9;
int N, Q, a, b, c, d, e, f;
int H = 9;

vpi V[MAXN];
pi T[MAXN];
vpi queries[MAXN];
int ans[MAXN];
vpi tmp;

inline void fill(int i) {
  vpi A;
  int J = min(SZ(tmp), H);
  for (int bm = 0; bm < (1 << J); ++bm) {
    pair<int, int> cur = mp(0, 0);
    for (int w = 0; w < J; ++w)
      if ((1 << w) & bm) {
        cur.f += tmp[w].f;
        cur.s += tmp[w].s;
      }
    if (cur.f > 100000)
      continue;
    A.pb(cur);
  }
  sort(ALL(A));
  for (auto t : A) {
    if (SZ(V[i])) {
      if (V[i].back().f == t.f) {
        V[i].back().s = t.s;
      } else if (V[i].back().s >= t.s) {
        continue;
      }
    }
    V[i].pb(t);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> N;
  int layers = 0;
  int r = N;
  while (r) {
    r /= 2;
    layers++;
  }
  H = min(H, layers);
  for (int i = 1; i <= N; ++i) {
    cin >> a >> b;
    T[i] = mp(b, a);
  }
  cin >> Q;
  for (int i = 0; i < Q; ++i) {
    ans[i] = -1;
    cin >> a >> b;
    queries[a].pb(b, i);
  }
  V[0].pb(0, 0);
  c = 1;
  for (int i = 1; i <= N; ++i) {
    tmp.clear();
    if (i < (1 << H)) {
      int cur = i;
      for (int j = 0; j < H; ++j) {
        tmp.pb(T[cur]);
        cur = cur / 2;
        if (cur == 0)
          break;
      }
      fill(i);
      while (SZ(queries[i])) {
        int l = queries[i].back().f;
        int t = queries[i].back().s;
        queries[i].pop_back();
        pair<int, int> x = *(--lb(ALL(V[i]), mp(l, INF)));
        ans[t] = x.s;
      }
    } else if (SZ(queries[i])) {
      int cur = i;
      for (int j = 0; j < H; ++j) {
        tmp.pb(T[cur]);
        cur = cur / 2;
        if (cur < (1 << H))
          break;
      }
      fill(i);
      int par = cur;
      for (auto it : queries[i]) {
        int l = it.f;
        int t = it.s;
        int ind = SZ(V[par]) - 1;
        for (int x = 0; x < SZ(V[i]); ++x) {
          while (ind > -1 && V[par][ind].f + V[i][x].f > l) {
            --ind;
          }
          if (ind == -1)
            break;
          ans[t] = max(ans[t], V[par][ind].s + V[i][x].s);
        }
      }
    }
  }

  for (int i = 0; i < Q; ++i) {
    cout << ans[i] << '\\n';
  }
}