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

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;

template <class T> void read(T &x) {
  int f = 0;
  x = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    f |= (ch == '-');
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - '0';
  if (f)
    x = -x;
}

const int N = (1 << 18) + 5;
int v[N], w[N], a[N], b[N];
int x, y, n, Q, cnt, nA, nB;
vector<pii> AA[N];
pii A[N], B[N];

void dfs(int l, int r, int V, int W, pii A[], int &nA) {
  if (l > r) {
    A[++nA] = mp(W, V);
    return;
  }
  dfs(l + 1, r, V, W, A, nA);
  dfs(l + 1, r, V + a[l], W + b[l], A, nA);
}

int main() {
  read(n);
  rep(i, 1, n) {
    read(v[i]);
    read(w[i]);
  }
  REP(i, min(n + 1, 1024)) {
    int x = i;
    cnt = 0;
    for (; x; x >>= 1) {
      a[++cnt] = v[x];
      b[cnt] = w[x];
    }
    nA = 0;
    dfs(1, cnt, 0, 0, A, nA);
    sort(A + 1, A + nA + 1);
    AA[i].resize(nA);
    rep(j, 1, nA) { AA[i][j - 1] = A[j]; }
  }
  read(Q);
  rep(i, 1, Q) {
    read(x);
    read(y);
    cnt = 0;
    int z = x;
    for (; x; x >>= 1) {
      a[++cnt] = v[x];
      b[cnt] = w[x];
    }
    reverse(a + 1, a + cnt + 1);
    reverse(b + 1, b + cnt + 1);
    per(t, cnt, cnt / 2 + 1) { z = z >> 1; }
    nA = AA[z].size();
    REP(i, nA) { A[i + 1] = AA[z][i]; }
    nB = 0;
    dfs(cnt / 2 + 1, cnt, 0, 0, B, nB);
    sort(B + 1, B + nB + 1);
    rep(i, 1, nA) { A[i].se = max(A[i].se, A[i - 1].se); }
    rep(i, 1, nB) { B[i].se = max(B[i].se, B[i - 1].se); }
    int p = nA, ans = 0;
    rep(i, 1, nB) {
      while (p && A[p].fi + B[i].fi > y) {
        p--;
      }
      if (A[p].fi + B[i].fi <= y) {
        ans = max(ans, A[p].se + B[i].se);
      }
    }
    printf("%d\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;

template <class T> void read(T &x) {
  int f = 0;
  x = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    f |= (ch == '-');
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - '0';
  if (f)
    x = -x;
}

const int N = (1 << 18) + 5;
int v[N], w[N], a[N], b[N];
int x, y, n, Q, cnt, nA, nB;
pii A[N], B[N];

void dfs(int l, int r, int V, int W, pii A[], int &nA) {
  if (l > r) {
    A[++nA] = mp(W, V);
    return;
  }
  dfs(l + 1, r, V, W, A, nA);
  dfs(l + 1, r, V + a[l], W + b[l], A, nA);
}

int main() {
  read(n);
  rep(i, 1, n) {
    read(v[i]);
    read(w[i]);
  }
  read(Q);
  rep(i, 1, Q) {
    read(x);
    read(y);
    cnt = 0;
    for (; x; x >>= 1) {
      a[++cnt] = v[x];
      b[cnt] = w[x];
    }
    nA = nB = 0;
    dfs(1, cnt / 2, 0, 0, A, nA);
    dfs(cnt / 2 + 1, cnt, 0, 0, B, nB);
    sort(A + 1, A + nA + 1);
    sort(B + 1, B + nB + 1);
    rep(i, 1, nA) { A[i].se = max(A[i].se, A[i - 1].se); }
    rep(i, 1, nB) { B[i].se = max(B[i].se, B[i - 1].se); }
    int p = nA, ans = 0;
    rep(i, 1, nB) {
      while (p && A[p].fi + B[i].fi > y) {
        p--;
      }
      if (A[p].fi + B[i].fi <= y) {
        ans = max(ans, A[p].se + B[i].se);
      }
    }
    printf("%d\\n", ans);
  }

  return 0;
}