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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod = 1000000007, MAX = 300005, INF = 1 << 30;
int p[MAX], v[MAX], w[MAX];

vector<pair<int, int>> C[MAX];

vector<int> use[MAX];

int main() {

  std::ifstream in("text.txt");
  std::cin.rdbuf(in.rdbuf());
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  p[0] = -1;
  for (int i = 2; i <= N; i++) {
    p[i - 1] = i / 2 - 1;
  }

  for (int i = 0; i < N; i++) {
    cin >> v[i] >> w[i];
  }

  for (int i = 0; i < (1 << 10); i++) {
    for (int j = 0; j < 10; j++) {
      if (i & (1 << j))
        use[i].push_back(j);
    }
  }

  int Q;
  cin >> Q;

  for (int u = 0; u < min(N, 511); u++) {
    vector<int> A, B;
    int save = u;
    while (u != -1) {
      A.push_back(v[u]);
      B.push_back(w[u]);
      u = p[u];
    }
    u = save;
    int M = si(A);
    C[u].push_back(mp(0, 0));

    for (int bit = 0; bit < (1 << M); bit++) {
      int vsum = 0, wsum = 0;
      for (int i : use[bit]) {
        vsum += A[i];
        wsum += B[i];
      }
      C[u].push_back(mp(wsum, vsum));
    }

    sort(all(C[u]));
  }

  vector<int> A, B;

  while (Q--) {
    int u, L;
    cin >> u >> L;
    u--;

    if (u < min(N, 511)) {
      int ans = 0;
      for (int i = 0; i < si(C[u]); i++) {
        if (C[u][i].fi <= L)
          chmax(ans, C[u][i].se);
      }
      cout << ans << "\\n";
      continue;
    }

    while (u >= 511) {
      A.push_back(v[u]);
      B.push_back(w[u]);
      u = p[u];
    }

    int M = si(A);

    vector<pair<int, int>> D(1 << M);

    for (int bit = 0; bit < (1 << M); bit++) {
      int vsum = 0, wsum = 0;
      for (int i : use[bit]) {
        vsum += A[i];
        wsum += B[i];
      }
      D[bit] = (mp(L - wsum, vsum));
    }

    D.push_back(mp(0, 0));
    D.push_back(mp(L, 0));

    sort(all(D));

    for (int i = si(D) - 2; i >= 0; i--)
      chmax(D[i].se, D[i + 1].se);

    int ans = 0;

    int id = 0;

    for (int i = 0; i < si(C[u]); i++) {
      if (C[u][i].fi > L)
        break;
      while (id < si(D) && C[u][i].fi > D[id].fi)
        id++;
      chmax(ans, C[u][i].se + D[id].se);
    }

    cout << ans << "\\n";

    A.clear();
    B.clear();
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod = 1000000007, MAX = 300005, INF = 1 << 30;
int p[MAX], v[MAX], w[MAX];

vector<pair<int, int>> C[MAX];

int main() {

  std::ifstream in("text.txt");
  std::cin.rdbuf(in.rdbuf());
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  p[0] = -1;
  for (int i = 2; i <= N; i++) {
    p[i - 1] = i / 2 - 1;
  }

  for (int i = 0; i < N; i++) {
    cin >> v[i] >> w[i];
  }

  int Q;
  cin >> Q;

  for (int u = 0; u < min(N, 511); u++) {
    vector<int> A, B;
    int save = u;
    while (u != -1) {
      A.push_back(v[u]);
      B.push_back(w[u]);
      u = p[u];
    }
    u = save;
    int M = si(A);
    C[u].push_back(mp(0, 0));

    for (int bit = 0; bit < (1 << M); bit++) {
      int vsum = 0, wsum = 0;
      for (int i = 0; i < M; i++) {
        if (bit & (1 << i)) {
          vsum += A[i];
          wsum += B[i];
        }
      }
      C[u].push_back(mp(wsum, vsum));
    }

    sort(all(C[u]));
  }

  vector<int> A, B;
  vector<pair<int, int>> D;

  while (Q--) {
    int u, L;
    cin >> u >> L;
    u--;

    if (u < min(N, 511)) {
      int ans = 0;
      for (int i = 0; i < si(C[u]); i++) {
        if (C[u][i].fi <= L)
          chmax(ans, C[u][i].se);
      }
      cout << ans << "\\n";
      continue;
    }

    while (u >= 511) {
      A.push_back(v[u]);
      B.push_back(w[u]);
      u = p[u];
    }

    int M = si(A);

    D.push_back(mp(0, 0));
    D.push_back(mp(L, 0));

    for (int bit = 0; bit < (1 << M); bit++) {
      int vsum = 0, wsum = 0;
      for (int i = 0; i < M; i++) {
        if (bit & (1 << i)) {
          vsum += A[i];
          wsum += B[i];
        }
        if (wsum >= L)
          break;
      }
      if (wsum <= L)
        D.push_back(mp(L - wsum, vsum));
    }

    sort(all(D));

    for (int i = si(D) - 2; i >= 0; i--)
      chmax(D[i].se, D[i + 1].se);

    int ans = 0;

    int id = 0;

    for (int i = 0; i < si(C[u]); i++) {
      if (C[u][i].fi > L)
        break;
      while (id < si(D) && C[u][i].fi > D[id].fi)
        id++;
      chmax(ans, C[u][i].se + D[id].se);
    }

    cout << ans << "\\n";

    A.clear();
    B.clear();
    D.clear();
  }
}